本文将介绍如何使用Spire.Presentation for Java在PowerPoint文档中查找和高亮指定文本。
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 {
//创建Presentation实例
Presentation presentation = new Presentation();
//加载PowerPoint文档
presentation.loadFromFile("Input.pptx");
//获取第一张幻灯片上的第一个形状
IAutoShape shape = (IAutoShape)presentation.getSlides().get(0).getShapes().get(0);
//设置高亮选项
TextHighLightingOptions options = new TextHighLightingOptions();
options.setWholeWordsOnly(true);
options.setCaseSensitive(true);
//使用hightLightText方法高亮指定文本
shape.getTextFrame().highLightText("Spire", Color.yellow, options);
//保存结果文档
presentation.saveToFile("HighlightSpecifiedText.pptx", FileFormat.PPTX_2013);
}
}
结果文档: