ページ上のテキストを置換

Contents
[ ]

PDF ドキュメントを更新する場合、テキスト置換は一般的な要件です。を使用する PDF コンテンツエディター、特定のテキストを検索して新しいコンテンツに置き換えることができます。「replace_text_strategy」プロパティを使用すると、置換されるオカレンスの数を制御できます。

  1. PDF コンテンツエディターのインスタンスを作成します。
  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_on_page(infile, outfile):
    # Create PdfContentEditor object
    content_editor = pdf_facades.PdfContentEditor()
    # Bind document to PdfContentEditor
    content_editor.bind_pdf(infile)
    # Replace text on page 1
    content_editor.replace_text_strategy.replace_scope = (
        pdf_facades.ReplaceTextStrategy.Scope.REPLACE_FIRST
    )
    content_editor.replace_text("PDF", "Page 1 Replaced Text", 14)
    # Save updated document
    content_editor.save(outfile)