Python で PDF にテキストスタンプを追加する方法
Contents
[
Hide
]
Python でテキストスタンプを追加する
使用できます テキストスタンプ PDF ファイルにテキストスタンプを追加するためのクラス。 テキストスタンプ クラスには、フォントサイズ、フォントスタイル、フォントの色など、テキストベースのスタンプの作成に必要なプロパティが用意されています。テキストスタンプを追加するには、必要なプロパティを使用して Document オブジェクトと TextStamp オブジェクトを作成する必要があります。その後、呼び出すことができます。 add_stamp () PDF にスタンプを追加するページのメソッド。次のコードスニペットは、PDF ファイルにテキストスタンプを追加する方法を示しています。これは PDF ページに注釈、透かし、ラベルを追加する場合に便利です。
- PDF ドキュメントを開きます。
- TextStamp オブジェクトを作成します。
- スタンプの背景動作を設定します。
- スタンプをページに配置します。
- 必要に応じてスタンプを回転させてください。
- テキストプロパティを設定します。
- スタンプをページに追加します。
- 変更した PDF ドキュメントを保存します。
import sys
import aspose.pdf as ap
from os import path
def add_text_stamp(input_file_name, output_file_name):
document = ap.Document(input_file_name)
# Create text stamp
text_stamp = ap.TextStamp("Sample Stamp")
# Set whether stamp is background
text_stamp.background = True
# Set origin
text_stamp.x_indent = 100
text_stamp.y_indent = 100
# Rotate stamp
text_stamp.rotate = ap.Rotation.ON90
# Set text properties
text_stamp.text_state.font = ap.text.FontRepository.find_font("Arial")
text_stamp.text_state.font_size = 14.0
text_stamp.text_state.font_style = (
ap.text.FontStyles.BOLD | ap.text.FontStyles.ITALIC
)
text_stamp.text_state.foreground_color = ap.Color.dark_green
# Add stamp to particular page
document.pages[1].add_stamp(text_stamp)
document.save(output_file_name)