Replace Text Regex

Contents
[ ]

Regular expressions allow flexible text replacement based on patterns instead of fixed strings. By enabling regex support in ‘replace_text_strategy’, you can match dynamic content such as numbers, dates, or formatted strings.

  1. Create a PdfContentEditor instance.
  2. Bind the input PDF document.
  3. Configure replacement strategy to use regex.
  4. Replace matching patterns across the entire document.
  5. Save the updated PDF document.
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
import sys
from os import path

sys.path.append(path.join(path.dirname(__file__), ".."))

from config import set_license, initialize_data_dir


def replace_text_regex(infile, outfile):
    # Create PdfContentEditor object
    content_editor = pdf_facades.PdfContentEditor()
    # Bind document to PdfContentEditor
    content_editor.bind_pdf(infile)
    # Replace text in the whole document
    content_editor.replace_text_strategy.replace_scope = (
        pdf_facades.ReplaceTextStrategy.Scope.REPLACE_ALL
    )
    content_editor.replace_text_strategy.is_regular_expression_used = True
    content_editor.replace_text(r"\d{4}", "[NUMBER]")
    # Save updated document
    content_editor.save(outfile)