Заменить текст на странице
Contents
[
Hide
]
Замена текста является распространённым требованием при обновлении PDF‑документов. С помощью PdfContentEditor, вы можете искать определённый текст и заменять его новым содержимым. Свойство ‘replace_text_strategy’ позволяет контролировать, сколько вхождений будет заменено.
- Создайте экземпляр PdfContentEditor.
- Привяжите входной PDF‑документ.
- Настройте стратегию замены текста.
- Замените целевой текст.
- Сохраните обновлённый 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)