Javatpoint Logo
Javatpoint Logo

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.


PDFBox Working with Attachments
Class Discription
PDSimpleFileSpecification It is a simple string reference to a file. The PDSimpleFileSpecification class does not allow for any parameters to be set.
PDComplexFileSpecification It is more feature rich class and allows for advanced settings for reference to 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.

Instantiate PDDocumentNameDictionary

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

Example-

Output:


PDFBox Working with Attachments





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA