Embed Files As Attachments To PDF Using Java

Learn to create file attachments in PDF documents using PDFOne (for Java™).
By V. Subhash

There are two ways for creating PDF file attachments. One is to create a document-level file attachment. The other is to create a file attachment annotation.

File Attachment Annotations

Like all annotations, file attachment annotations are bound to a page. A viewer application, such as Adobe Reader, will use a paperclip, tag, graph or pushpin icon to display a file attachment annotation on a page . When users click these icons, the viewer application will show a prompt and offer to launch or save the embedded file.

Here is the code to create a page-level PDF file attachment.

public static void main(String[] args)
   throws IOException, PdfException  {

    // Create a file attachment annotation and set its properties
    PdfFileAttachmentAnnot fa =
        new PdfFileAttachmentAnnot(10,10,Color.RED);
    fa.setIcon(PdfFileAttachmentAnnot.ICON_PAPERCLIP);
    fa.setTitle("PDF File Attachments Demo");
    fa.setSubject("Embed file using a file attachment annotation");
    fa.setContents("Double-click the icon for the file attachment");
    fa.setFilePath("sample_file.txt");
    fa.setRect(10,10,20,40);


    // Add an optional popup window for the annotation
    // Note: A file attachment annotation does not usually 
    // have a regular popup window.
    PdfPopUpAnnot popup = new PdfPopUpAnnot();
    popup.setRect(40,80,170,120);
    fa.setPopup(popup, flase);

    // Open a PDF document for editing 
    PdfReader reader =
        PdfReader.fileReader(
            "sample_doc.pdf", // Input file
            "sample_doc_with_attachment_annotation.pdf"); // Output file
    PdfDocument doc = new PdfDocument(reader);

    // Add file attachment to page #1
    doc.addAnnotation(fa,1);
    
    doc.setOpenAfterSave(true);

    // Make viewer applications show attachments panel  
    doc.setPageMode(PdfPageMode.USEATTACHMENTS);
 
    // Save file and clean up
    doc.write();
    reader.dispose();
}

Document-Level Attachments

These attachments are not bound to a page. They are document-level resources in a PDF document. A viewer application such as Adobe Reader will show these files in a special attachments panel.*

public static void main(String[] args) 
  throws IOException, PdfException  {
    // Open a PDF document for editing
    PdfReader reader = 
        PdfReader.fileReader(
            "sample_doc.pdf", // Input file 
            "sample_doc_with_document_level_attachment.pdf"); // Output file
    PdfDocument doc = new PdfDocument(reader);
    
    // Add file attachment
    doc.attachDocument("sample_file.txt");

    doc.setOpenAfterSave(true);

    // Make viewer applications show attachments panel  
    doc.setPageMode(PdfPageMode.USEATTACHMENTS);
 
    // Save file and clean up
    doc.write();
    reader.dispose();
}

* - Please note that a file embedded using a file attachment annotation will also be displayed in the attachments panel.

Privacy | Legal | Feedback | Newsletter © 2002-2010 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows users are advised to use Microsoft ClearType Tuning for optimal experience. Linux and other users can enable font smoothing, as supported by their OS. Also, please use the latest version of a standards-compliant browser such as Opera, FireFox, Chrome or Safari.