Replace Text With State

Replace text with a custom text state

  1. Bind the source PDF to the PdfContentEditor facade.
  2. Create and configure a TextState with the required color and font size.
  3. Set the replace-text scope to ReplaceAll.
  4. Call replaceText(...) with the search text, replacement text, and configured TextState.
  5. 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();
    }
}