Find and replace text in PowerPoint slides in C# and VB.NET

With the GemBox.Presentation library, you can find text using a string or a regular expression and replace it with new content, as shown in the following example.

using GemBox.Presentation;
using System.Linq;

// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

var presentation = PresentationDocument.Load("FindAndReplace.pptx");

var slide = presentation.Slides[0];

slide.TextContent.Replace("companyName", "Acme Corporation");

var items = new string[][]
{
new string[] { "January", "$14.2M", "$1.6M", "$12.5M", "$2.3M" },
new string[] { "February", "$15.2M", "$0.6M", "$11.3M", "$4.3M" },
new string[] { "March", "$17.2M", "$0M", "$12.1M", "$52.3M" },
new string[] { "April", "$7.2M", "$1.6M", "$7.2M", "$0M" },
new string[] { "May", "$3.2M", "$1.6M", "$0M", "$3.2M" }
};

var table = slide.Content.Drawings.OfType<GraphicFrame>().First().Table;

// Clone the row with custom text tags (e.g. '@month', '@revenue', etc.).
for (int i = 0; i < items.Length - 1; i++)
table.Rows.AddClone(table.Rows[1]);

// Replace custom text tags with data.
for (int i = 0; i < items.Length; i++)
{
var row = table.Rows[i + 1];
var item = items[i];
row.TextContent.Replace("@month", item[0]);
row.TextContent.Replace("@revenue", item[1]);
row.TextContent.Replace("@cashExpense", item[2]);
row.TextContent.Replace("@operatingIncome", item[3]);
row.TextContent.Replace("@operatingExpense", item[4]);
}

// Find all text content with "0M" value in the table.
var zeroValueRanges = table.TextContent.Find("0M");

presentation.Save("Find And Replace.pptx");
Imports GemBox.Presentation
Imports System.Linq

Module Program

Sub Main()

' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")

Dim presentation = PresentationDocument.Load("FindAndReplace.pptx")

Dim slide = presentation.Slides(0)

slide.TextContent.Replace("companyName", "Acme Corporation")

Dim items As String()() =
{
new String() {"January", "$14.2M", "$1.6M", "$12.5M", "$2.3M"},
new String() {"February", "$15.2M", "$0.6M", "$11.3M", "$4.3M"},
new String() {"March", "$17.2M", "$0M", "$12.1M", "$52.3M"},
new String() {"April", "$7.2M", "$1.6M", "$7.2M", "$0M"},
new String() {"May", "$3.2M", "$1.6M", "$0M", "$3.2M"}
}

Dim table = slide.Content.Drawings.OfType(Of GraphicFrame).First().Table

' Clone the row with custom text tags (e.g. '@month', '@revenue', etc.).
For i = 0 to items.Length - 2
table.Rows.AddClone(table.Rows(1))
Next

' Replace custom text tags with data.
for i = 0 to items.Length - 1
Dim row = table.Rows(i + 1)
Dim item = items(i)
row.TextContent.Replace("@month", item(0))
row.TextContent.Replace("@revenue", item(1))
row.TextContent.Replace("@cashExpense", item(2))
row.TextContent.Replace("@operatingIncome", item(3))
row.TextContent.Replace("@operatingExpense", item(4))
Next

' Find all text content with "0M" value in the table.
Dim zeroValueRanges = table.TextContent.Find("0M")

presentation.Save("Find And Replace.pptx")
End Sub
End Module
PowerPoint slide with the replaced text
Screenshot of a PowerPoint slide whose text was replaced programmatically

The text content of a presentation document is represented with by the TextRange class, which is exposed on the following content model classes:

You can also use the TextRange class to achieve the following tasks:

Next steps

GemBox.Presentation is a .NET library that enables you to read, write, edit, convert, and print presentation files from your .NET applications using one simple API.