Wednesday, 16 June 2021

Find and Highlight Text in PowerPoint documents using Java

This article will introduce how to find and highlight specified text in a PowerPoint slide using Java codes. It’s worth mentioning that I used a free third-party library called Free Spire.Presentation for Java. Before running codes, we need to add a Jar file called Spire.Presentantion.jar in the library to Intellij IDEA. You can download the package from the link, find the Jar file in the “lib” folder and add it to IDEA. Or directly refer to it by using the following Maven configurations.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Using the code

import com.spire.presentation.FileFormat;
import com.spire.presentation.IAutoShape;
import com.spire.presentation.Presentation;
import com.spire.presentation.TextHighLightingOptions;
import java.awt.*;

public class SearchAndHighlightText {
public static void main(String[] args) throws Exception {
//Create a Presentation instance
Presentation presentation = new Presentation();
//Load a PowerPoint document
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

//Get the first shape on the first slide
IAutoShape shape = (IAutoShape) presentation.getSlides().get(0).getShapes().get(0);

//Set highlight options
TextHighLightingOptions options = new TextHighLightingOptions();
options.setWholeWordsOnly(true);
options.setCaseSensitive(false);

//Highlight text
shape.getTextFrame().highLightText("nose", Color.yellow, options);

//Save the resulting document
presentation.saveToFile("output/HighlightSpecifiedText.pptx", FileFormat.PPTX_2013);
}
}

Output



No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...