• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Add, Delete & Get Attachment from a PDF Document in Java Applications  RSS feed

Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This technical tip shows how to Add, Delete & Get Attachment in a PDF Document using Aspose.Pdf for Java. In order to add attachment in a PDF document, you need to create a FileSpecification object with the file, which needs to be added, and the file description. After that the FileSpecification object can be added to EmbeddedFiles collection of Document object using add(..) method of EmbeddedFiles collection. The attachments of the PDF document can found in the EmbeddedFiles collection of the Document object. In order to delete all the attachments, you only need to call the delete(..) method of the EmbeddedFiles collection and then save the updated file using save method of the Document object.

Add attachment in a PDF document.
//open document

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");

//setup new file to be added as attachment

com.aspose.pdf.FileSpecification fileSpecification = new com.aspose.pdf.FileSpecification("sample.txt", "Sample text file");

//add attachment to document's attachment collection


// Save updated document containing table object


Delete all the attachments from the PDF document.
//open document

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");

//delete all attachments


//save updated file


Get an individual attachment from the PDF document.
//open document

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");

//get particular embedded file

com.aspose.pdf.FileSpecification fileSpecification = pdfDocument.getEmbeddedFiles().get_Item(1);

//get the file properties

System.out.printf("Name: - " + fileSpecification.getName());

System.out.printf("\nDescription: - " + fileSpecification.getDescription());

System.out.printf("\nMime Type: - " + fileSpecification.getMIMEType());

// get attachment form PDF file

try {

    InputStream input = fileSpecification.getContents();

    File file = new File(fileSpecification.getName());

    // create path for file from pdf


    // create and extract file from pdf

    java.io.FileOutputStream output = new java.io.FileOutputStream(fileSpecification.getName(), true);

    byte[] buffer = new byte[4096];

    int n = 0;

    while (-1 != (n = input.read(buffer)))

    output.write(buffer, 0, n);

    // close InputStream object



} catch (IOException e) {



// close Document object


Overview: Aspose.Pdf for Java
Aspose.Pdf is a Java PDF component to create PDF documents without using Adobe Acrobat. It supports Floating box, PDF form field, PDF attachments, security, Foot note & end note, Multiple columns document, Table of Contents, List of Tables, Nested tables, Rich text format, images, hyperlinks, JavaScript, annotation, bookmarks, headers, footers and many more. Now you can create PDF by API, XML and XSL-FO files. It also enables you to converting HTML, XSL-FO and Excel files into PDF.

More about Aspose.Pdf for Java

Contact Information
Aspose Pty Ltd

Suite 163, 79 Longueville Road

Lane Cove, NSW, 2066


Aspose – Your File Format Experts


Phone: 888.277.6734

Fax: 866.810.9465
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!