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

Contents
[ ]

PDF 内のテキストを置き換える際に、色やフォントサイズなどの書式を変更する必要がある場合もあります。TextState を使用すると、スタイルプロパティを定義して置換時に適用できます。これにより、変更されたテキストを強調表示したり、文書全体で一貫したフォーマットを適用したりできます。

  1. を作成 PDF コンテンツエディター インスタンス。
  2. 入力 PDF ドキュメントをバインドします。
  3. カスタムフォーマットで TextState を定義します。
  4. 交換戦略を設定します。
  5. 特定のページのテキストを置換します。
  6. 更新した 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_with_state(infile, outfile):
    # Create PdfContentEditor object
    content_editor = pdf_facades.PdfContentEditor()
    # Bind document to PdfContentEditor
    content_editor.bind_pdf(infile)

    text_state = ap.text.TextState()
    text_state.foreground_color = ap.Color.red
    text_state.font_size = 12

    # Replace text on a specific page with explicit text formatting
    content_editor.replace_text_strategy.replace_scope = (
        pdf_facades.ReplaceTextStrategy.Scope.REPLACE_ALL
    )
    content_editor.replace_text("software", 1, "SOFTWARE PAGE 1", text_state)
    # Save updated document
    content_editor.save(outfile)