添加附件

Contents
[ ]

PDF 中的文件附件允许您直接在 PDF 中包含补充的文档、图像或其他资源。使用 PdfContentEditor, 您可以以编程方式将文件附加到特定页面,设置附件名称并提供描述。

  1. 创建 PdfContentEditor 对象。
  2. 绑定输入 PDF。
  3. 打开附件文件。
  4. 将附件添加到 PDF 中。
  5. 保存已更新的 Document。
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_attachment(infile, attachment_file, outfile):
    # Create PdfContentEditor object
    content_editor = pdf_facades.PdfContentEditor()
    # Bind document to PdfContentEditor
    content_editor.bind_pdf(infile)
    # Add attachment to page 1
    with open(attachment_file, "rb") as attachment_stream:
        content_editor.add_document_attachment(
            attachment_stream,
            path.basename(attachment_file),
            "This is a sample attachment for demonstration purposes.",
        )
    # Save updated document
    content_editor.save(outfile)