简单替换文本

Contents
[ ]

在文档中更新重复值时,简单的文本替换非常有用。使用 PdfContentEditor, 您可以定义替换范围,并在所有页面上全局替换文本。

  1. 创建一个 PdfContentEditor 实例。
  2. 绑定输入的 PDF 文档。
  3. 为所有出现配置替换范围。
  4. 替换目标文本。
  5. 保存更新后的 PDF 文档。
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_simple(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("33", "XXXIII ")
    # Save updated document
    content_editor.save(outfile)