When you want to emphasize a particular point in a PowerPoint presentation, you can highlight it with a bright color to help the audience catch it at first glance. In this article, we will explain how to highlight text in a PowerPoint presentation in Java using Spire.Presentation for Java.
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.presentation</artifactId> <version>9.11.3</version> </dependency> </dependencies>
Highlight Text in PowerPoint in Java
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 using TextHighLightingOptions.setWholeWordsOnly() and TextHighLightingOptions.setCaseSensitive() methods.
- Highlight a specific text in the shape using IAutoShape.getTextFrame().highLightText() method.
- Save the result file using Presentation.saveToFile() method.
- Java
import com.spire.presentation.*; import java.awt.*; public class HighlightTextInPPT { public static void main(String []args) throws Exception { //Create an instance of Presentation class Presentation presentation = new Presentation(); //Load a PowerPoint file presentation.loadFromFile("Input.pptx"); //Loop through all slides for (int i = 0; i < presentation.getSlides().getCount(); i++) { //Get the current slide ISlide slide = presentation.getSlides().get(i); //Loop through the shapes on the slide for (int j = 0; j < slide.getShapes().getCount(); j++) { //Check if the current shape is of IAutoShape type if (slide.getShapes().get(j) instanceof IAutoShape) { //Typecast the shape to IAutoShape IAutoShape shape = (IAutoShape)slide.getShapes().get(j); //Create an instance of TextHighLightingOptions class TextHighLightingOptions options = new TextHighLightingOptions(); //Set text highlighting options options.setCaseSensitive(true); options.setWholeWordsOnly(true); //Highlight specific text within the shape with color shape.getTextFrame().highLightText("Spire", Color.YELLOW, options); } } } //Save the result file presentation.saveToFile("HighlightText.pptx", FileFormat.PPTX_2013); } }
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.