ストリームから添付ファイルの注釈を追加

Contents
[ ]

添付ファイルに注釈を付けると、ファイルをインタラクティブなアイコンとしてPDFページに埋め込むことができます。ストリームベースのアプローチを使用すると、物理的なファイルパスに頼らずにファイルを動的に添付できます。この方法では、不透明度などの注釈の外観のカスタマイズもサポートされています。

  1. PDF コンテンツエディターオブジェクトを作成します。
  2. 入力 PDF をバインドします。
  3. 添付ファイルをストリームとして読み取ります。
  4. 添付ファイルの注釈を追加します。
  5. 更新したドキュメントを保存します。
import aspose.pdf.facades as pdf_facades
import aspose.pydrawing as apd
from io import BytesIO
import sys
from os import path

sys.path.append(path.join(path.dirname(__file__), ".."))

from config import set_license, initialize_data_dir


def add_file_attachment_annotation_from_stream(infile, attachment_file, outfile):
    # Create PdfContentEditor object
    content_editor = pdf_facades.PdfContentEditor()
    # Bind document to PdfContentEditor
    content_editor.bind_pdf(infile)

    with open(attachment_file, "rb") as source_stream:
        attachment_stream = BytesIO(source_stream.read())

    # Create file attachment annotation using stream+opacity overload
    content_editor.create_file_attachment(
        apd.Rectangle(130, 520, 20, 20),
        "Attachment annotation from stream",
        attachment_stream,
        path.basename(attachment_file),
        1,
        "Tag",
        0.75,
    )
    # Save updated document
    content_editor.save(outfile)