How to replace a particular image with text in a PowerPoint Presentation?
Syncfusion Presentation is a .NET PowerPoint Library used to create, read, edit and convert PowerPoint files to PDF and images programmatically without Microsoft Office or interop dependencies. Using this library, you can replace a particular image with text in PowerPoint Presentation using C#.
Steps to replace a particular image with text in PowerPoint using C#
- Create a new C# Console application project
- Install the Syncfusion.Presentation.Net.Core NuGet package as a reference to your project from Nuget.org.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without trail message.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.Presentation;
- Use the following code to replace a image with text in a PowerPoint.
C#
//Load or open an PowerPoint Presentation.
using FileStream inputStream = new(Path.GetFullPath(@"Template.pptx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Open an existing PowerPoint presentation.
using IPresentation pptxDoc = Presentation.Open(inputStream);
//Replace image with text.
ReplaceImageWithText(pptxDoc, "Image was here");
//Save output PowerPoint Presentation
using FileStream outputStream = new(Path.GetFullPath(@"Result.pptx"), FileMode.Create, FileAccess.ReadWrite);
pptxDoc.Save(outputStream);
- Use the following method to replace the image with same size text box in a PowerPoint.
C#
private void ReplaceImageWithText(IPresentation pptxDoc, string altText)
{
double shapeLeft, shapeTop, shapeWidth, shapeHeignt = 0;
//Iterate through the slides collection.
foreach (ISlide slide in pptxDoc.Slides)
{
//Iterate through the pictures collection and remove the picture named "Image".
foreach (IPicture picture in slide.Pictures)
{
if (picture.Description == "ImageID1")
{
shapeLeft = picture.Left;
shapeTop = picture.Top;
shapeWidth = picture.Width;
shapeHeignt = picture.Height;
slide.Pictures.Remove(picture);
//Insert a textbox with the alternate text in the bounds of picture.
slide.Shapes.AddTextBox(shapeLeft, shapeTop, shapeWidth, shapeHeignt).TextBody.AddParagraph(altText);
break;
}
}
}
}
You can download a complete working sample from GitHub.
By executing the program, you will get the PowerPoint Presentation as follows.
Take a moment to peruse the documentation, where you can find basic presentation document processing options along with features like clone and merge slides and encrypt and decrypt PowerPoint presentation and most importantly PDF and image conversion with code examples.
Explore more about the rich set of Syncfusion PowerPoint Framework features.
Conclusion
I hope you enjoyed learning how to replace a particular image with text in PPTX using C#.
You can refer to our .NET PowerPoint Library feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!