How to add attachment to PDF document

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Merger MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-merger -- dnx GroupDocs.Merger.Mcp --yes
Folders, licensing and more →

PDF documents look the same on every device, and they can also keep related files together: other PDF documents, Word documents, Excel spreadsheets, PowerPoint presentations, images, audio or video files can be attached to a PDF document, and they move with it wherever it goes.

How to add attachment to PDF document

  1. Create a Merger with the path to the PDF document.
  2. Call merger.import_document() with a PdfAttachmentOptions that names the file to attach.
  3. Call merger.save() with the path of the result.

The following example attaches a PowerPoint presentation to a PDF document:

from groupdocs.merger import Merger
from groupdocs.merger.domain.options import PdfAttachmentOptions

def add_attachment_to_pdf_document():
    with Merger("./input.pdf") as merger:
        # Attach the presentation to the PDF document
        attachment_options = PdfAttachmentOptions("./attachment.ppt")
        merger.import_document(attachment_options)
        merger.save("./output.pdf")

if __name__ == "__main__":
    add_attachment_to_pdf_document()

input.pdf is a sample file used in this example. Click here to download it.

attachment.ppt is a sample file used in this example. Click here to download it.

Binary file (PDF, 456 KB)

Download full output

Explanation

  • PdfAttachmentOptions("./attachment.ppt"): names the file to attach. To attach content you already hold in memory, pass its bytes and its extension instead: PdfAttachmentOptions(data, ".ppt").
  • merger.import_document(attachment_options): attaches the file to the loaded PDF document. Its pages do not change.

API reference

See also