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.
fromgroupdocs.watermarkimportWatermarkerfromgroupdocs.watermark.options.pdfimportPdfLoadOptionsdefadd_attachment():withWatermarker("./document.pdf",PdfLoadOptions())aswatermarker:content=watermarker.get_content()withopen("./sample.docx","rb")asf: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.
Iterate the attachments collection to read each attachment’s metadata and content.
fromgroupdocs.watermarkimportWatermarkerfromgroupdocs.watermark.options.pdfimportPdfLoadOptionswithWatermarker("./document.pdf",PdfLoadOptions())aswatermarker:content=watermarker.get_content()print("Attachments:",len(content.attachments))forattachmentincontent.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 diskwithopen(f"./{attachment.name}","wb")asf:f.write(attachment.content)
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.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.