Attachments in 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.Watermark 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-watermark -- dnx GroupDocs.Watermark.Mcp --yes
Folders, licensing and more →

A PDF can carry embedded file attachments. Watermarker.get_content() returns a PdfContent whose attachments collection lets you add, extract, and remove them — and you can even open an attachment in its own Watermarker to watermark it.

Add an attachment

The example embeds a Word document into the PDF as an attachment.

from groupdocs.watermark import Watermarker
from groupdocs.watermark.options.pdf import PdfLoadOptions

def add_attachment():
    with Watermarker("./document.pdf", PdfLoadOptions()) as watermarker:
        content = watermarker.get_content()
        with open("./sample.docx", "rb") as f:
            data = f.read()
        content.attachments.add(data, "sample.docx", "Attached Word document")
        watermarker.save("./output.pdf")

if __name__ == "__main__":
    add_attachment()

document.pdf and sample.docx are the sample files used in this example. Download document.pdf and sample.docx.

Binary file (PDF, 1.0 MB)

Download full output

Extract attachments

Iterate the attachments collection to read each attachment’s metadata and content.

from groupdocs.watermark import Watermarker
from groupdocs.watermark.options.pdf import PdfLoadOptions

with Watermarker("./document.pdf", PdfLoadOptions()) as watermarker:
    content = watermarker.get_content()
    print("Attachments:", len(content.attachments))
    for attachment in content.attachments:
        info = attachment.get_document_info()
        print(f"- name={attachment.name!r} type={info.file_type} size={len(attachment.content)} bytes")
        # Save the attachment's bytes to disk
        with open(f"./{attachment.name}", "wb") as f:
            f.write(attachment.content)

The sample document.pdf already carries the studio’s rate card as an attachment, so this prints:

Attachments: 1
- name='rate-card.xlsx' type=Xlsx (.xlsx) - Spreadsheet size=152978 bytes

Use content.attachments.remove_at(index) or content.attachments.remove(attachment) to delete an attachment, and open an attachment in its own Watermarker (from its content bytes) to add a watermark to the attached document itself.