How to Add Attachments to your PDFs with Foxit PDF SDK (Java)
With Foxit PDF SDK, attachments refer to documents rather than file attachment annotations. This allows whole files to be embedded in a document, much like email attachments. PDF SDK provides application APIs to access functions such as loading attachments, getting attachments, inserting/removing attachments, and accessing properties of attachments.
Example:
Contents
How to export the embedded attachment file from a PDF and save it as a single file
import com.foxit.sdk.PDFException; import com.foxit.sdk.common.Library; import com.foxit.sdk.pdf.Attachments; import com.foxit.sdk.pdf.FileSpec; import com.foxit.sdk.pdf.PDFDoc; import com.foxit.sdk.pdf.objects.PDFNameTree; ... PDFNameTree empty_nametree = new PDFNameTree(); { // Get information of attachments. Attachments attachments = new Attachments(doc, empty_nametree); int count = attachments.getCount(); for (int i = 0; i < count; i++) { String key = attachments.getKey(i); FileSpec file_spec = attachments.getEmbeddedFile(key); if (!file_spec.isEmpty()) { String name = file_spec.getFileName(); if (file_spec.isEmbedded()) { String export_file_path = output_path + name; file_spec.exportToFile(export_file_path); } } } }
How to remove all the attachments of a PDF
import com.foxit.sdk.PDFException; import com.foxit.sdk.common.Library; import com.foxit.sdk.pdf.Attachments; import com.foxit.sdk.pdf.PDFDoc; import com.foxit.sdk.pdf.objects.PDFNameTree; ... // Assuming PDFDoc doc has been loaded. PDFNameTree empty_nametree = new PDFNameTree(); { // Get information of attachments. Attachments attachments = new Attachments(doc, empty_nametree); attachments.removeAllEmbeddedFiles(); } ...
Updated on April 23, 2019