PDFBox Working with Attachments
A PDF document can contain references to an external file via the file system or a URL to a remote location. It is also possible to embed a binary file into a PDF document.
An Apache PDFBox provides following classes that can be used in referencing a file.
In PDFBox, it is also possible to embed a file directly into a PDF document. This can be performed by using the EmbeddedFile attribute instead of setting the file attribute of the PDComplexFileSpecification class.
Embed File into PDF Document
PDF documents can contain file attachments. The file attachments are accessed from the Document->File Attachments menu. PDFBox allows us to add attachments in a PDF document and also extracted that attachments from PDF documents. In PDFBox, attachments are part of the named tree that is attached to the document catalogue.
Following are the step by step procedure to add attachments to PDF file.
Load Existing Document
We can load the existing PDF document by using the static load () method. This method accepts a file object as a parameter. We can also invoke it using the class name PDDocument of the PDFBox.
In this, attachments are stored as a part of the "names" dictionary in the document catalogue.
Retrieve Existing Attachments and Add New Attachments
Create the file specification, which holds the embedded file