使用状态替换文本
Contents
[
Hide
]
在 PDF 中更新文本时,您可能希望替换的内容突出显示。通过使用 TextState 对象,您可以定义颜色、字体大小等样式,然后将其应用于所有被替换的文本。
- 创建一个 PdfContentEditor 实例。
- 绑定输入的 PDF 文档。
- 定义具有自定义格式的 TextState。
- 配置替换范围。
- 替换整个文档中的文本。
- 保存更新后的 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_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.blue
text_state.font_size = 14
# Replace text with explicit text formatting
content_editor.replace_text_strategy.replace_scope = (
pdf_facades.ReplaceTextStrategy.Scope.REPLACE_ALL
)
content_editor.replace_text("software", "SOFTWARE", text_state)
# Save updated document
content_editor.save(outfile)