PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
Compatibility
J2SE J2EE Windows Linux Mac (OS X)

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, false);

    // Open a PDF document for editing 
    PdfDocument doc = new PdfDocument();
    doc.load("sample_doc.pdf");

    // 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.save("sample_doc_with_attachment_annotation.pdf");
    doc.close();
}

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
    PdfDocument doc = new PdfDocument();
    doc.load("sample_doc.pdf");
    
    // 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.save("sample_doc_with_document_level_attachment.pdf");
    doc.close();
}

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

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

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