使用状态替换文本
Contents
[
Hide
]
使用自定义文本状态替换文本
- 将源 PDF 绑定到
PdfContentEditor对象。 - 创建并配置一个
TextState使用所需的颜色和字体大小。 - 将 replace-text 范围设置为
ReplaceAll。 - 调用
replaceText(...)使用搜索文本、替换文本和已配置的TextState。 - 保存更新后的 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();
}
}