C#/VB.NET: Replace Text in PowerPoint

2022-12-16 08:14:00 Written by  support iceblue
Rate this item
(0 votes)

The replace feature in Microsoft PowerPoint allows you to search for a specific text and change its occurrences to a new text at once. This is extremely useful when you need to fix the same error in multiple places within a large PowerPoint document. In this article, you will learn how to programmatically replace text in PowerPoint documents in C# and VB.NET using Spire.Presentation for .NET.

Install Spire.Presentation for .NET

To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Presentation

Replace the First Occurrence of a Specific Text in PowerPoint in C# and VB.NET

To replace the first occurrence of a specific text in a PowerPoint document, you can loop through all slides in the document, then call the ISlide.ReplaceFirstText() method. The detailed steps are as follows:

  • Initialize an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Loop through all slides in the PowerPoint document.
  • Replace the first occurrence of a specific text with a new text using ISlide.ReplaceFirstText() method.
  • Save the result document using Presentation.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Presentation;

namespace ReplaceFirstTextOccurrenceInPPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();
            //Load a PowerPoint document
            ppt.LoadFromFile("Sample.pptx");

            //Loop through all slides in the document
            foreach (ISlide slide in ppt.Slides)
            {
                //Replace the first occurrence of "Spire.Presentation for .NET" with "E-iceblue Product"
                slide.ReplaceFirstText("Spire.Presentation for .NET", "E-iceblue Product", false);
                break;
            }

            //Save the result document
            ppt.SaveToFile("ReplaceFirstTextOccurrence.pptx", FileFormat.Pptx2013);

        }
    }
}

C#/VB.NET: Replace Text in PowerPoint

Replace All Occurrences of a Specific Text in PowerPoint in C# and VB.NET

To replace all occurrences of a specific text in a PowerPoint document, you can loop through all slides in the document, and then use the ISlide.ReplaceAllText() method. The detailed steps are as follows:

  • Initialize an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Loop through all slides in the PowerPoint document.
  • Replace all occurrences of a specific text with a new text using ISlide.ReplaceAllText() method.
  • Save the result document using Presentation.SaveToFile() method.
  • C#
  • VB.NET
*using Spire.Presentation;

namespace ReplaceAllTextOccurrencesInPPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();
            //Load a PowerPoint document
            ppt.LoadFromFile("Sample.pptx");

            //Loop through all slides in the document
            foreach (ISlide slide in ppt.Slides)
            {
                //Replace all occurrences of "Spire.Presentation for .NET" with "E-iceblue Product"
                slide.ReplaceAllText("Spire.Presentation for .NET", "E-iceblue Product", false);
            }

            //Save the result document
            ppt.SaveToFile("ReplaceAllTextOccurrences.pptx", FileFormat.Pptx2013);

        }
    }
}

C#/VB.NET: Replace Text in PowerPoint

Replace Text Using a Regular Expression in PowerPoint using C# and VB.NET

Spire.Presentation for .NET provides the IShape.ReplaceTextWithRegex() method that enables you to replace text matching a regular expression pattern. The detailed steps are as follows:

  • Initialize an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Loop through all slides in the PowerPoint document.
  • Loop through all shapes on each slide.
  • Replace text matching a regular expression pattern using IShape.ReplaceTextWithRegex() method.
  • Save the result document using Presentation.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Presentation;
using System.Text.RegularExpressions;

namespace ReplaceTextUsingRegexInPPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();
            //Load a PowerPoint document
            ppt.LoadFromFile("Sample1.pptx");

            //Loop through all slides in the document
            foreach (ISlide slide in ppt.Slides)
            {
                //Loop through all shapes on each slide
                foreach (IShape shape in slide.Shapes)
                {
                    //Replace text starting with # on the slide to "Monitor"
                    shape.ReplaceTextWithRegex(new Regex(@"\#\w+\b"), "Monitor");
                }
            }

            //Save the result document
            ppt.SaveToFile("ReplaceTextUsingRegex.pptx", FileFormat.Pptx2013);
        }
    }
}

C#/VB.NET: Replace Text in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Friday, 16 December 2022 01:21