Important alert: (current site time 7/15/2013 5:08:48 AM EDT)
 

article

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

Email
Submitted on: 7/4/2013 9:12:05 PM
By: aspose_seo 
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 609
 
     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. 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.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				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
pdfDocument.getEmbeddedFiles().add(fileSpecification);
// Save updated document containing table object
pdfDocument.save("output.pdf");
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
pdfDocument.getEmbeddedFiles().delete();
//save updated file
pdfDocument.save("output.pdf");
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
file.getParentFile().mkdirs();
// 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
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
// close Document object
pdfDocument.dispose();
More about Aspose.Pdf for Java
- Homepage of Aspose.Pdf is a Java: http://www.aspose.com/java/pdf-component.aspx 
- Download Aspose.Pdf is a Java: http://www.aspose.com/community/files/72/java-components/aspose.pdf-for-java/default.aspx 


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.