Replace Text With State
Contents
[
Hide
]
Replace text with a custom text state
- Bind the source PDF to the
PdfContentEditorfacade. - Create and configure a
TextStatewith the required color and font size. - Set the replace-text scope to
ReplaceAll. - Call
replaceText(...)with the search text, replacement text, and configuredTextState. - Save the updated PDF document.
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();
}
}