Python を使って PDF にテキストを追加

Python for .NET を使用して PDF ドキュメントにテキストを追加します。Aspose.PDF を使用して PDF ドキュメントをプログラム的に変更します。

Python for .NET ライブラリを使用して PDF 内のテキストを操作する方法

PDF ファイルにテキストを追加するには、Aspose.PDF for Python API を使用します。これは、.NET 用の機能豊富で強力で使いやすいドキュメント操作 API です。NuGet パッケージマネージャーを開き、AsPose.pdf を検索してインストールします。パッケージマネージャーコンソールから以下のコマンドを使用することもできます。

Console

pip install aspose-pdf

Python 経由で PDF ファイルにテキストを追加


ご使用の環境でコードを試すには、Aspose.PDF for Python が必要です。

  1. Document のインスタンスで PDF をロードします。
  2. TextParagraph を作成し、そのプロパティを定義します。
  3. TextBuilder を使用してテキスト段落をページに追加します。
  4. ファイルをもう一度保存します。

PDF にテキストを追加-Python

このサンプルコードは、PDF ドキュメントにテキストを追加する方法を示しています-Python

import aspose.pdf as apdf

from os import path

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)

# Get particular page
page = document.pages[1]

# Create text fragment
text_fragment = apdf.text.TextFragment("Hello, world!")
text_fragment.position = apdf.text.Position(100, 600)

# Set text properties
text_fragment.text_state.font_size = 12
text_fragment.text_state.font = apdf.text.FontRepository.find_font("TimesNewRoman")
text_fragment.text_state.background_color = apdf.Color.light_gray
text_fragment.text_state.foreground_color = apdf.Color.red

# Create TextBuilder object
builder = apdf.text.TextBuilder(page)

# Append the text fragment to the PDF page
builder.append_text(text_fragment)

# Save resulting PDF document.
document.save(path_outfile )