Text replacement on the presentation slides

By using Spire.Presentation for .NET, developers can easily modify the texts on the presentation slides. In this topic, we are going to demonstrate how to use Spire.Presentation to replace the specific texts in a placeholder in C#.

Firstly, view the original sample document that the text "Spire.Presentation for .NET" will be replaced later.

Text replacement on the presentation slides

Step 1: Create an instance of Dictionary<string,string> and add an item for the instance.

Dictionary<string, string> TagValues = new Dictionary<string, string>();
TagValues.Add("Spire.Presentation for .NET", "Spire.PPT");

Step 2: Create a presentation instance and load the document from file.

Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx", FileFormat.Pptx2010);

Step 3: Call ReplaceTags event to replace all the texts on the first slide.

ReplaceTags(presentation.Slides[0], TagValues);

Step 4: Save the document to file and lance to process it.

presentation.SaveToFile("Result.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("Result.pptx");

The following ReplaceTags() method shows how to replace the text on the first presentation slide:

public void ReplaceTags(Spire.Presentation.ISlide pSlide, Dictionary<string, string> TagValues)
{
    foreach (IShape curShape in pSlide.Shapes)
    {
        if (curShape is IAutoShape)
        {
            foreach (TextParagraph tp in (curShape as IAutoShape).TextFrame.Paragraphs)
            {
                foreach (var curKey in TagValues.Keys)
                {
                    if (tp.Text.Contains(curKey))
                    {
                        tp.Text = tp.Text.Replace(curKey, TagValues[curKey]);
                    }
                }
            }
        }
    }
}

Effective screenshot after the replacing the text on the presentation slide:

Text replacement on the presentation slides

Full codes of how to replace the text on the presentation slides:

public ReplaceText()
{
    {
        Dictionary<string, string> TagValues = new Dictionary<string, string>();
        TagValues.Add("Spire.Presentation for .NET", "Spire.PPT");

        Presentation presentation = new Presentation();

        presentation.LoadFromFile("Sample.pptx", FileFormat.Pptx2010);

        ReplaceTags(presentation.Slides[0], TagValues);

        presentation.SaveToFile("Result.pptx", FileFormat.Pptx2010);
        System.Diagnostics.Process.Start("Result.pptx");
    }
}
public void ReplaceTags(Spire.Presentation.ISlide pSlide, Dictionary<string, string> TagValues)
{
    foreach (IShape curShape in pSlide.Shapes)
    {
        if (curShape is IAutoShape)
        {
            foreach (TextParagraph tp in (curShape as IAutoShape).TextFrame.Paragraphs)
            {
                foreach (var curKey in TagValues.Keys)
                {
                    if (tp.Text.Contains(curKey))
                    {
                        tp.Text = tp.Text.Replace(curKey, TagValues[curKey]);
                    }
                }
            }
        }
    }
}