使用状态替换文本

使用自定义文本状态替换文本

  1. 将源 PDF 绑定到 PdfContentEditor 对象。
  2. 创建并配置一个 TextState 使用所需的颜色和字体大小。
  3. 将 replace-text 范围设置为 ReplaceAll。
  4. 调用 replaceText(...) 使用搜索文本、替换文本和已配置的 TextState。
  5. 保存更新后的 PDF 文档。
public static void replaceTextWithState(Path inputFile, Path outputFile) {
    PdfContentEditor editor = new PdfContentEditor();
    try {
        editor.bindPdf(inputFile.toString());
        TextState textState = new TextState();
        textState.setForegroundColor(com.aspose.pdf.Color.getBlue());
        textState.setFontSize(14);
        editor.getReplaceTextStrategy().setReplaceScope(ReplaceTextStrategy.Scope.ReplaceAll);
        editor.replaceText("software", "SOFTWARE", textState);
        editor.save(outputFile.toString());
    } finally {
        editor.close();
    }
}