Python を使用して PDF ドキュメントに注釈を付ける

Python を使用して、クラウド上で任意の PDF ドキュメントにプログラムで注釈を付けることができます。これは、画像、コメント、メモ、またはドキュメント内のその他の種類の外部コメントの形式で、既存のデータに関する任意の追加情報にすることができます。この記事では、Python の REST API を使用して PDF ドキュメントに注釈を付ける方法を学習します。

この記事では次のトピックについて説明します。

ドキュメント注釈 REST API および Python SDK

PDF ドキュメントに注釈を付けるには、GroupDocs.Annotation Cloud の Python SDK API を使用します。これにより、ドキュメント注釈ツールをオンラインでプログラム的に構築できます。すべての一般的な形式のビジネス ドキュメントに、注釈、透かしオーバーレイ、テキスト置換、墨消し、テキスト マークアップを追加できます。また、クラウド API の ドキュメント アノテーション ファミリー メンバー として、.NET、Java、PHP、Ruby、および Node.js SDK も提供します。

コンソールで次のコマンドを使用して、GroupDocs.Annotation Cloud を Python プロジェクトにインストールできます。

pip install groupdocs_annotation_cloud

手順と利用可能なコード例を開始する前に、ダッシュボード からクライアント ID とクライアント シークレットを取得してください。 ID とシークレットを取得したら、以下に示すようにコードを追加します。

client_id = "659fe7da-715b-4744-a0f7-cf469a392b73"
client_secret = "b377c36cfa28fa69960ebac6b6e36421"

configuration = groupdocs_annotation_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = ""

Python の REST API を使用して PDF ドキュメントに注釈を付ける

以下の簡単な手順に従って、PDF ドキュメントに注釈を追加できます。

  1. PDFファイルをクラウドにアップロード
  2. Python を使用して PDF ドキュメントに注釈を付ける
  3. 注釈付きファイルをダウンロード

ドキュメントをアップロードする

まず、次のコード サンプルを使用して PDF ファイルをクラウドにアップロードします。

# APIのインスタンスを作成する
file_api = groupdocs_annotation_cloud.FileApi.from_config(configuration)
my_storage = ""

# サンプルファイルをアップロードする
request = groupdocs_annotation_cloud.UploadFileRequest("sample.pdf", "C:\\Files\\sample.pdf", my_storage)
response = file_api.upload_file(request)

その結果、アップロードされた PDF ファイルは、クラウド上のダッシュボードの ファイル セクション で利用できるようになります。

Python を使用して PDF ドキュメントに注釈を付ける

プログラムで PDF ドキュメントに複数の注釈を追加するには、以下の手順に従ってください。

  • AnnotateApi のインスタンスを作成する
  • AnnotationInfo の最初のインスタンスを作成する
  • 最初のインスタンスの注釈プロパティ (位置、タイプ、テキストなど) を設定します。
  • AnnotationInfo の 2 番目のインスタンスを作成する
  • 2 番目のインスタンスの注釈プロパティ (位置、タイプ、テキストなど) を設定します。
  • AnnotationInfo の 3 番目のインスタンスを作成する
  • 3 番目のインスタンスの注釈プロパティ (位置、タイプ、テキストなど) を設定します。
  • FileInfo インスタンスを作成し、入力ファイルのパスを設定します。
  • AnnotateOptions のインスタンスを作成し、ファイル情報を AnnotateOptions に設定します。
  • 1 番目、2 番目、および 3 番目の注釈を AnnotateOptions に割り当てる
  • AnnotateRequest メソッドを呼び出してリクエストを作成する
  • AnnotateApi.annotate() メソッドを呼び出して結果を取得します

次のコード サンプルは、REST API を使用して PDF ドキュメントに注釈を付け、複数の注釈を追加する方法を示しています。

# APIを初期化する
api = groupdocs_annotation_cloud.AnnotateApi.from_config(configuration)

# 距離の注釈
a1 = groupdocs_annotation_cloud.AnnotationInfo()
a1.annotation_position = groupdocs_annotation_cloud.Point()
a1.annotation_position.x = 1
a1.annotation_position.y = 1
a1.box = groupdocs_annotation_cloud.Rectangle()
a1.box.x = 100
a1.box.y = 100
a1.box.width = 200
a1.box.height = 200
a1.page_number = 0
a1.pen_color = 1201033
a1.pen_style = "Solid"
a1.pen_width = 3
a1.opacity = 1
a1.type = "Distance"
a1.text = "This is 距離の注釈"
a1.creator_name = "Anonym A."

# エリアの注釈
a2 = groupdocs_annotation_cloud.AnnotationInfo()
a2.annotation_position = groupdocs_annotation_cloud.Point()
a2.annotation_position.x = 1
a2.annotation_position.y = 1
a2.box = groupdocs_annotation_cloud.Rectangle()
a2.box.x = 100
a2.box.y = 400
a2.box.width = 200
a2.box.height = 100
a2.page_number = 0
a2.pen_color = 1201033
a2.pen_style = "Solid"
a2.pen_width = 3
a2.opacity = 1
a2.type = "Area"
a2.text = "This is エリアの注釈"
a2.creator_name = "Anonym A."

# 矢印の注釈
a3 = groupdocs_annotation_cloud.AnnotationInfo()
a3.annotation_position = groupdocs_annotation_cloud.Point()
a3.annotation_position.x = 1
a3.annotation_position.y = 1
a3.box = groupdocs_annotation_cloud.Rectangle()
a3.box.x = 100
a3.box.y = 250
a3.box.width = 100
a3.box.height = 50
a3.page_number = 0
a3.pen_color = 1201033
a3.pen_style = "Solid"
a3.pen_width = 1
a3.opacity = 0.7
a3.type = "Arrow"
a3.text = "This is 矢印の注釈"
a3.creator_name = "Anonym A."

# 入力ファイル
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "sample.pdf"

# 注釈オプションを定義する
options = groupdocs_annotation_cloud.AnnotateOptions()
options.file_info = file_info
options.annotations = [a1, a2, a3]
options.output_path = "output.pdf"

# 注釈リクエストの作成
request = groupdocs_annotation_cloud.AnnotateRequest(options)
result = api.annotate(request)         

print("AddMultipleAnnotations: Multiple Annotations added: " + result['href'])
Python の REST API を使用して PDF ドキュメントに複数の注釈を追加する

Python の REST API を使用して PDF ドキュメントに複数の注釈を追加する

サポートされている注釈タイプの詳細については、ドキュメントの 注釈の追加 セクションを参照してください。

注釈付きファイルをダウンロードする

上記のコード サンプルは、注釈付き PDF ファイルをクラウド上に保存します。次のコードサンプルを使用してダウンロードできます。

# APIの初期化
file_api = groupdocs_annotation_cloud.FileApi.from_config(configuration)
my_storage = ""

# ダウンロードリクエストを作成する
request = groupdocs_annotation_cloud.DownloadFileRequest("output.pdf", my_storage)
response = file_api.download_file(request)

# ダウンロードしたファイルを作業ディレクトリに移動します
shutil.move(response, "C:\\Files\\")

Python を使用して TextField 注釈を追加する

以下の手順に従って、PDF ドキュメントにテキスト フィールドの注釈をプログラムで追加できます。

  • AnnotateApi のインスタンスを作成する
  • AnnotationInfo のインスタンスを作成する
  • 注釈の位置を定義する
  • 長方形の位置、高さ、幅を定義する
  • テキスト、高さ、幅などのさまざまな注釈プロパティを設定します。
  • 注釈タイプを TextField として設定します
  • FileInfo インスタンスを作成し、入力ファイルのパスを設定します。
  • AnnotateOptions のインスタンスを作成し、ファイル情報を AnnotateOptions に設定します。
  • 注釈を AnnotateOptions に割り当てる
  • AnnotateRequest メソッドを呼び出してリクエストを作成する
  • AnnotateApi.annotate() メソッドを呼び出して結果を取得します

次のコード サンプルは、REST API を使用して PDF ドキュメントにテキスト フィールドの注釈を追加する方法を示しています。前述の手順に従ってファイルをアップロードおよびダウンロードしてください。

# APIを初期化する
api = groupdocs_annotation_cloud.AnnotateApi.from_config(configuration)

# 注釈情報を提供する
a1 = groupdocs_annotation_cloud.AnnotationInfo()
a1.annotation_position = groupdocs_annotation_cloud.Point()
a1.annotation_position.x = 1
a1.annotation_position.y = 1
a1.box = groupdocs_annotation_cloud.Rectangle()
a1.box.x = 380
a1.box.y = 300
a1.box.width = 100
a1.box.height = 50
a1.page_number = 0
a1.font_color = 1201033
a1.font_size = 12
a1.opacity = 0.7
a1.type = "TextField"           
a1.text = "Text field text"
a1.creator_name = "Anonym A."

# 入力ファイルのパス
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "sample.pdf"

# 注釈オプションを定義する
options = groupdocs_annotation_cloud.AnnotateOptions()
options.file_info = file_info
options.annotations = [a1]
options.output_path = "output.pdf"

# 注釈リクエストの作成
request = groupdocs_annotation_cloud.AnnotateRequest(options)
result = api.annotate(request)         

print("AddTextFieldAnnotation: Text Field Annotation added: " + result['href'])
Python を使用してテキスト フィールドの注釈を追加する

Python を使用してテキスト フィールドの注釈を追加する

Python を使用して画像注釈を追加する

以下の手順に従って、プログラムで PDF ドキュメントに画像注釈を追加できます。

  • AnnotateApi のインスタンスを作成する
  • AnnotationInfo のインスタンスを作成する
  • Rectangle を定義し、その位置、高さ、幅を設定します
  • 位置、テキスト、高さ、幅などのさまざまな注釈プロパティを設定します。
  • 注釈タイプを画像として設定します
  • FileInfo インスタンスを作成し、入力ファイルのパスを設定します。
  • AnnotateOptions のインスタンスを作成し、ファイル情報を AnnotateOptions に設定します。
  • 注釈を AnnotateOptions に割り当てる
  • AnnotateRequest メソッドを呼び出してリクエストを作成する
  • AnnotateApi.annotate() メソッドを呼び出して結果を取得します

次のコード サンプルは、REST API を使用して PDF ドキュメントに画像注釈を追加する方法を示しています。前述の手順に従ってファイルをアップロードおよびダウンロードしてください。

# APIを初期化する
api = groupdocs_annotation_cloud.AnnotateApi.from_config(configuration)

# 注釈を定義する
a1 = groupdocs_annotation_cloud.AnnotationInfo()
a1.box = groupdocs_annotation_cloud.Rectangle()
a1.box.x = 300
a1.box.y = 300
a1.box.width = 200
a1.box.height = 100
a1.page_number = 0
a1.pen_color = 1201033
a1.pen_style = "Solid"
a1.pen_width = 1
a1.opacity = 0.7
a1.type = "Image"
a1.text = "This is Image annotation"
a1.creator_name = "Anonym A."
a1.image_path = "JohnSmith.png"

# 入力ファイルのパス
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "sample.pdf"

# 注釈を定義する options
options = groupdocs_annotation_cloud.AnnotateOptions()
options.file_info = file_info
options.annotations = [a1]
options.output_path = "output_img.pdf"

# 注釈リクエストの作成
request = groupdocs_annotation_cloud.AnnotateRequest(options)
result = api.annotate(request)         

print("AddImageAnnotation: Image Annotation added: " + result['href'])
Python を使用して画像注釈を追加する

Python を使用して画像注釈を追加する

以下の手順に従って、PDF ドキュメントにハイパーリンクの注釈をプログラムで追加できます。

  • AnnotateApi のインスタンスを作成する
  • AnnotationInfo のインスタンスを作成する
  • 注釈ポイントを定義し、各ポイントの位置を設定する
  • テキスト、高さ、幅などのさまざまな注釈プロパティを設定します。
  • 注釈タイプをリンクとして設定します
  • FileInfo インスタンスを作成し、入力ファイルのパスを設定します。
  • AnnotateOptions のインスタンスを作成し、ファイル情報を AnnotateOptions に設定します。
  • 注釈を AnnotateOptions に割り当てる
  • AnnotateRequest メソッドを呼び出してリクエストを作成する
  • AnnotateApi.annotate() メソッドを呼び出して結果を取得します

次のコード サンプルは、REST API を使用して PDF ドキュメントにハイパーリンク注釈を追加する方法を示しています。前述の手順に従ってファイルをアップロードおよびダウンロードしてください。

# APIを初期化する
api = groupdocs_annotation_cloud.AnnotateApi.from_config(configuration)

# 注釈情報を提供する
a1 = groupdocs_annotation_cloud.AnnotationInfo()
p1 = groupdocs_annotation_cloud.Point()
p1.x = 80
p1.y = 710
p2 = groupdocs_annotation_cloud.Point()
p2.x = 240
p2.y = 710
p3 = groupdocs_annotation_cloud.Point()
p3.x = 80
p3.y = 650
p4 = groupdocs_annotation_cloud.Point()
p4.x = 240
p4.y = 650
a1.points = [p1, p2, p3, p4]
a1.page_number = 0
a1.type = "Link"
a1.text = "This is Link annotation"
a1.creator_name = "Anonym A."
a1.url = "https://www.groupdocs.com/"

# 入力ファイルのパス
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "sample.pdf"

# 注釈オプションを定義する
options = groupdocs_annotation_cloud.AnnotateOptions()
options.file_info = file_info
options.annotations = [a1]
options.output_path = "output.pdf"

# 注釈リクエストの作成
request = groupdocs_annotation_cloud.AnnotateRequest(options)
result = api.annotate(request)         

print("AddLinkAnnotation: Link Annotation added: " + result['href'])
Python を使用してリンク注釈で注釈を付ける

Python を使用してリンク注釈で注釈を付ける

オンラインで試す

上記の API を使用して開発された、次の無料のオンライン PDF 注釈ツールをお試しください。 https://products.groupdocs.app/annotation/pdf

結論

この記事では、クラウド上の PDF ドキュメントにさまざまなタイプの注釈を追加する方法を学習しました。さらに、プログラムによって PDF ファイルをクラウドにアップロードし、注釈付きファイルをクラウドからダウンロードする方法を学習しました。 GroupDocs.Annotation Cloud API の詳細については、ドキュメント を使用して学習できます。また、ブラウザーを通じて直接 API を視覚化し操作できるようにする API リファレンス セクションも提供しています。不明な点がある場合は、フォーラム でお気軽にお問い合わせください。

関連項目