page 141

C#/VB.NET: Replace Text in PowerPoint

2022-12-16 08:14:00 Written by Koohji

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.

C#/VB.NET: Highlight Text in PowerPoint

2022-09-28 03:42:00 Written by Koohji

When creating a PowerPoint presentation, you may want to ensure that some important content in your presentation grabs the audience’s attention. A great way to make the content more prominent and noticeable is to highlight it with a bright color. This article will demonstrate how to highlight text in a PowerPoint presentation 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

Highlight Text in PowerPoint in C# and VB.NET

The following are the steps to highlight specific text in a PowerPoint document:

  • Initialize an instance of Presentation class.
  • Load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Loop through the slides in the presentation and the shapes on each slide.
  • Check if the current shape is of IAutoShape type.
  • If the result is true, typecast it to IAutoShape.
  • Initialize an instance of TextHighLightingOptions class, and set the text highlighting options such as whole words only and case sensitive through TextHighLightingOptions.WholeWordsOnly and TextHighLightingOptions.CaseSensitive properties.
  • Highlight a specific text in the shape using IAutoShape.TextFrame.HighLightText() method.
  • Save the result file using Presentation.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Presentation;
using System.Drawing;

namespace HighlightTextInPPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Presentation class
            Presentation presentation = new Presentation();
            //Load a PowerPoint file
            presentation.LoadFromFile(@"Sample1.pptx");

            //Loop through all slides 
            for (int i = 0; i < presentation.Slides.Count; i++)
            {
                //Get the current slide
                ISlide slide = presentation.Slides[i];
                //Loop through the shapes on the slide
                for (int j = 0; j < slide.Shapes.Count; j++)
                {
                    //Check if the current shape is of IAutoShape type
                    if (slide.Shapes[j] is IAutoShape)
                    {
                        //Typecast the shape to IAutoShape
                        IAutoShape shape = slide.Shapes[j] as IAutoShape;

                        //Create an instance of TextHighLightingOptions class
                        TextHighLightingOptions options = new TextHighLightingOptions();
                        //Set text highlighting options
                        options.CaseSensitive = true;
                        options.WholeWordsOnly = true;

                        //Highlight specific text within the shape with color
                        shape.TextFrame.HighLightText("Spire.Presentation", Color.LightYellow, options);
                    }

                }
            }

            //Save the result file
            presentation.SaveToFile("HighlightText.pptx", FileFormat.Pptx2013);
        }
    }
}

C#/VB.NET: Highlight 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.

Table of contents (TOC) makes the PDF documents more accessible and easier to navigate, especially for large files. This article demonstrates how to create table of contents (TOC) for a PDF document using Spire.PDF for Java.

import com.spire.pdf.*;
import com.spire.pdf.actions.PdfGoToAction;
import com.spire.pdf.annotations.*;
import com.spire.pdf.general.PdfDestination;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;

public class TableOfContent {
    public static void main(String[] args) throws Exception
    {
        //load PDF file
        PdfDocument doc = new PdfDocument("sample.pdf");
        int pageCount = doc.getPages().getCount();
        //Insert a new page as the first page to draw table of content
        PdfPageBase tocPage = doc.getPages().insert(0);
        //set title
        String title = "Table Of Contents";
        PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("Arial",  Font.BOLD,20));
        PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
        Point2D location = new Point2D.Float((float) tocPage.getCanvas().getClientSize().getWidth() / 2, (float) titleFont.measureString(title).getHeight());
        tocPage.getCanvas().drawString(title, titleFont, PdfBrushes.getCornflowerBlue(), location, centerAlignment);
        //draw TOC text
        PdfTrueTypeFont titlesFont = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN,14));
        String[] titles = new String[pageCount];
        for (int i = 0; i < titles.length; i++) {
            titles[i] = String.format("page%1$s", i + 1);
        }
        float y = (float)titleFont.measureString(title).getHeight() + 10;
        float x = 0;
        for (int i = 1; i <= pageCount; i++) {
            String text = titles[i - 1];
            Dimension2D titleSize = titlesFont.measureString(text);
            PdfPageBase navigatedPage = doc.getPages().get(i);
            String pageNumText = (String.valueOf(i+1));
            Dimension2D pageNumTextSize = titlesFont.measureString(pageNumText);
            tocPage.getCanvas().drawString(text, titlesFont, PdfBrushes.getCadetBlue(), 0, y);
            float dotLocation = (float)titleSize.getWidth() + 2 + x;
            float pageNumlocation = (float)(tocPage.getCanvas().getClientSize().getWidth() - pageNumTextSize.getWidth());
            for (float j = dotLocation; j < pageNumlocation; j++) {
                if (dotLocation >= pageNumlocation) {
                    break;
                }
                tocPage.getCanvas().drawString(".", titlesFont, PdfBrushes.getGray(), dotLocation, y);
                dotLocation += 3;
            }
            tocPage.getCanvas().drawString(pageNumText, titlesFont, PdfBrushes.getCadetBlue(), pageNumlocation, y);
            //add TOC action
            Rectangle2D titleBounds = new Rectangle2D.Float(0,y,(float)tocPage.getCanvas().getClientSize().getWidth(),(float)titleSize.getHeight());
            PdfDestination Dest = new PdfDestination(navigatedPage, new Point2D.Float(-doc.getPageSettings().getMargins().getTop(), -doc.getPageSettings().getMargins().getLeft()));
            PdfActionAnnotation action = new PdfActionAnnotation(titleBounds, new PdfGoToAction(Dest));
            action.setBorder(new PdfAnnotationBorder(0));
            ((PdfNewPage) ((tocPage instanceof PdfNewPage) ? tocPage : null)).getAnnotations().add(action);
            y += titleSize.getHeight() + 10;
        }

        //save the resultant file
        doc.saveToFile("addTableOfContent.pdf");
        doc.close();
    }
}

Output:

Create Table of Contents (TOC) in PDF in Java

page 141