Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"customers": [
{
"Name": "Nancy Davolio",
"Price": "$50",
"Photo": "Nancy.jpeg"
},
{
"Name": "Andrew Fuller",
"Price": "$100",
"Photo": "Andrew.jpeg"
},
{
"Name": "Janet Leverling",
"Price": "$55",
"Photo": "Janet.jpeg"
},
{
"Name": "Margaret Peacock",
"Price": "$60",
"Photo": "Margaret.jpeg"
},
{
"Name": "Steven Buchanan",
"Price": "$70",
"Photo": "Steven.jpeg"
}
]
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,56 +1,80 @@
using Syncfusion.Presentation;
using System.ComponentModel;
using System.Text.Json;
using Syncfusion.Presentation;

//Load or open an PowerPoint Presentation.
using FileStream inputStream = new(Path.GetFullPath(@"Data/UpdatePlaceholders.pptx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// Read customer data from JSON file
string jsonContent = File.ReadAllText(Path.GetFullPath(@"Data/customers.json"));
JsonDocument jsonDoc = JsonDocument.Parse(jsonContent);
JsonElement customersArray = jsonDoc.RootElement.GetProperty("customers");

using (IPresentation pptxDoc = Presentation.Open(inputStream))
// Loop through each customer in the JSON array
for (int customerIndex = 0; customerIndex < customersArray.GetArrayLength(); customerIndex++)
{
//Finds all the occurrences of a particular text in the PowerPoint presentation
ITextSelection[] textSelections1 = pptxDoc.FindAll("{CustomerName}", false, false);
foreach (ITextSelection textSelection in textSelections1)
{
//Gets the found text as a single text part
ITextPart textPart = textSelection.GetAsOneTextPart();
//Replaces the text
textPart.Text = "Nancy Dovolio";
}
ITextSelection[] textSelections2 = pptxDoc.FindAll("{Price}", false, false);
foreach (ITextSelection textSelection in textSelections2)
{
//Gets the found text as a single text part
ITextPart textPart = textSelection.GetAsOneTextPart();
//Replaces the text
textPart.Text = "$50";
}
// Iterate through all slides in the presentation
foreach (ISlide slide in pptxDoc.Slides)
JsonElement customer = customersArray[customerIndex];
string customerName = customer.GetProperty("Name").GetString();
string price = customer.GetProperty("Price").GetString();
string photo = customer.GetProperty("Photo").GetString();

// Load a fresh copy of the template for each customer
using FileStream inputStream = new(Path.GetFullPath(@"Data/UpdatePlaceholders.pptx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

using (IPresentation pptxDoc = Presentation.Open(inputStream))
{
// Iterate through each shape in the slide
for (int i = 0; i < slide.Shapes.Count; i++)
// Replace {CustomerName} placeholder
ITextSelection[] textSelections1 = pptxDoc.FindAll("{CustomerName}", false, false);
foreach (ITextSelection textSelection in textSelections1)
{
// Gets the found text as a single text part
ITextPart textPart = textSelection.GetAsOneTextPart();
// Replaces the text
textPart.Text = customerName;
}

// Replace {Price} placeholder
ITextSelection[] textSelections2 = pptxDoc.FindAll("{Price}", false, false);
foreach (ITextSelection textSelection in textSelections2)
{
IShape shape = slide.Shapes[i] as IShape;
// Gets the found text as a single text part
ITextPart textPart = textSelection.GetAsOneTextPart();
// Replaces the text
textPart.Text = price;
}

// Check for picture with specific alt text
if (shape.SlideItemType is SlideItemType.Placeholder && shape.PlaceholderFormat.Type == PlaceholderType.Picture)
// Iterate through all slides in the presentation
foreach (ISlide slide in pptxDoc.Slides)
{
// Iterate through each shape in the slide
for (int i = 0; i < slide.Shapes.Count; i++)
{
// Check if the picture has the specific alt text
if (shape.Description == "{ProfileImage}")
IShape shape = slide.Shapes[i] as IShape;

// Check for picture placeholder with specific alt text
if (shape.SlideItemType is SlideItemType.Placeholder &&
shape.PlaceholderFormat.Type == PlaceholderType.Picture &&
shape.Description == "{ProfileImage}")
{
// Build the image path using the photo filename from JSON
string imagePath = Path.GetFullPath($@"Data/{photo}");

// Replace the image
FileStream pictureStream = new FileStream(Path.GetFullPath(@"Data/Customer_profile.png"), FileMode.Open);
FileStream pictureStream = new FileStream(imagePath, FileMode.Open);
MemoryStream memoryStream = new MemoryStream();
pictureStream.CopyTo(memoryStream);
slide.Shapes.AddPicture(memoryStream, shape.Left, shape.Top, shape.Width, shape.Height);
slide.Shapes.Remove(shape);
// Close the picture stream

// Close the picture streams
pictureStream.Close();
memoryStream.Close();
break;
}
}
}

// Save each customer's output as a separate file
string sanitizedName = customerName.Replace(" ", "_");
using FileStream outputStream = new(Path.GetFullPath($@"Output/{sanitizedName}_Profile.pptx"), FileMode.Create, FileAccess.ReadWrite);

// Save the modified presentation
pptxDoc.Save(outputStream);
}
using FileStream outputStream = new(Path.GetFullPath(@"Output/Result.pptx"), FileMode.Create, FileAccess.ReadWrite);
// Save the modified presentation
pptxDoc.Save(outputStream);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Update placeholder text or images in a PowerPoint Presentation using C#
# update placeholder text or images in a PowerPoint Presentation using C#

The Syncfusion&reg; [.NET PowerPoint Library](https://www.syncfusion.com/document-processing/powerpoint-framework/net/powerpoint-library) (Presentation) enables you to create, read, and edit PowerPoint files programmatically without Microsoft Office or interop dependencies. Using this library, you can **Update placeholder text or images in a PowerPoint Presentation** using C#.
The Syncfusion&reg; [.NET PowerPoint Library](https://www.syncfusion.com/document-processing/powerpoint-framework/net/powerpoint-library) (Presentation) enables you to create, read, and edit PowerPoint files programmatically without Microsoft Office or interop dependencies. Using this library, you can **update placeholder text or images in a PowerPoint Presentation** using C#.

## Steps to find and replace text programmatically
## Steps to update placeholder text or images programmatically

Step 1: Create a new .NET Core console application project.

Expand All @@ -11,65 +11,92 @@ Step 2: Install the [Syncfusion.Presentation.Net.Core](https://www.nuget.org/pac
Step 3: Include the following namespaces in the Program.cs file.

```csharp
using System.Text.Json;
using Syncfusion.Presentation;
using System.IO;
```

Step 4: Add the following code snippet in Program.cs file to find and replace text in the PowerPoint Presentation.
Step 4: Add the following code snippet in Program.cs file to update placeholder text or images in the PowerPoint Presentation.

```csharp
//Load or open an PowerPoint Presentation.
using FileStream inputStream = new(Path.GetFullPath(@"Data/UpdatePlaceholders.pptx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

using (IPresentation pptxDoc = Presentation.Open(inputStream))
// Read customer data from JSON file
string jsonContent = File.ReadAllText(Path.GetFullPath(@"Data/customers.json"));
JsonDocument jsonDoc = JsonDocument.Parse(jsonContent);
JsonElement customersArray = jsonDoc.RootElement.GetProperty("customers");

// Loop through each customer in the JSON array
for (int customerIndex = 0; customerIndex < customersArray.GetArrayLength(); customerIndex++)
{
//Finds all the occurrences of a particular text in the PowerPoint presentation
ITextSelection[] textSelections1 = pptxDoc.FindAll("{CustomerName}", false, false);
foreach (ITextSelection textSelection in textSelections1)
{
//Gets the found text as a single text part
ITextPart textPart = textSelection.GetAsOneTextPart();
//Replaces the text
textPart.Text = "Nancy Dovolio";
}
ITextSelection[] textSelections2 = pptxDoc.FindAll("{Price}", false, false);
foreach (ITextSelection textSelection in textSelections2)
{
//Gets the found text as a single text part
ITextPart textPart = textSelection.GetAsOneTextPart();
//Replaces the text
textPart.Text = "$50";
}
// Iterate through all slides in the presentation
foreach (ISlide slide in pptxDoc.Slides)
JsonElement customer = customersArray[customerIndex];
string customerName = customer.GetProperty("Name").GetString();
string price = customer.GetProperty("Price").GetString();
string photo = customer.GetProperty("Photo").GetString();

// Load a fresh copy of the template for each customer
using FileStream inputStream = new(Path.GetFullPath(@"Data/UpdatePlaceholders.pptx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

using (IPresentation pptxDoc = Presentation.Open(inputStream))
{
// Iterate through each shape in the slide
for (int i = 0; i < slide.Shapes.Count; i++)
// Replace {CustomerName} placeholder
ITextSelection[] textSelections1 = pptxDoc.FindAll("{CustomerName}", false, false);
foreach (ITextSelection textSelection in textSelections1)
{
IShape shape = slide.Shapes[i] as IShape;
// Gets the found text as a single text part
ITextPart textPart = textSelection.GetAsOneTextPart();
// Replaces the text
textPart.Text = customerName;
}

// Check for picture with specific alt text
if (shape.SlideItemType is SlideItemType.Placeholder && shape.PlaceholderFormat.Type == PlaceholderType.Picture)
// Replace {Price} placeholder
ITextSelection[] textSelections2 = pptxDoc.FindAll("{Price}", false, false);
foreach (ITextSelection textSelection in textSelections2)
{
// Gets the found text as a single text part
ITextPart textPart = textSelection.GetAsOneTextPart();
// Replaces the text
textPart.Text = price;
}

// Iterate through all slides in the presentation
foreach (ISlide slide in pptxDoc.Slides)
{
// Iterate through each shape in the slide
for (int i = 0; i < slide.Shapes.Count; i++)
{
// Check if the picture has the specific alt text
if (shape.Description == "{ProfileImage}")
IShape shape = slide.Shapes[i] as IShape;

// Check for picture placeholder with specific alt text
if (shape.SlideItemType is SlideItemType.Placeholder &&
shape.PlaceholderFormat.Type == PlaceholderType.Picture &&
shape.Description == "{ProfileImage}")
{
// Build the image path using the photo filename from JSON
string imagePath = Path.GetFullPath($@"Data/{photo}");

// Replace the image
FileStream pictureStream = new FileStream(Path.GetFullPath(@"Data/Customer_profile.png"), FileMode.Open);
FileStream pictureStream = new FileStream(imagePath, FileMode.Open);
MemoryStream memoryStream = new MemoryStream();
pictureStream.CopyTo(memoryStream);
slide.Shapes.AddPicture(memoryStream, shape.Left, shape.Top, shape.Width, shape.Height);
slide.Shapes.Remove(shape);
// Close the picture stream

// Close the picture streams
pictureStream.Close();
memoryStream.Close();
break;
}
}
}

// Save each customer's output as a separate file
string sanitizedName = customerName.Replace(" ", "_");
using FileStream outputStream = new(Path.GetFullPath($@"Output/{sanitizedName}_Profile.pptx"), FileMode.Create, FileAccess.ReadWrite);

// Save the modified presentation
pptxDoc.Save(outputStream);
}
using FileStream outputStream = new(Path.GetFullPath(@"Output/Result.pptx"), FileMode.Create, FileAccess.ReadWrite);
// Save the modified presentation
pptxDoc.Save(outputStream);
}
```

More information about find and replace can be found in this [documentation](https://help.syncfusion.com/document-processing/powerpoint/powerpoint-library/net/working-with-find-and-replace) section.