How to Add, Delete or Get Attachments from PDF Document in Java and Android Apps
by Lara Sheen .NET & Java DeveloperThis technical tip shows how to work with attachment in a PDF document using Aspose.Pdf. Developers can add, delete, get attachment from a PDF file. 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 EmbeddedFiles collection contains all the attachments added in the PDF file. You can get the FileSpecification object from the EmbeddedFiles Collection of the Document object. The EmbeddedFiles Collection contains all the attachments of the PDF document. You can either loop through all of these attachments using for loop, or get an individual attachment by specifying its index value.
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.
The following code snippet shows you how to add attachment in a PDF document.
//open document
com.aspose.pdf.Document pdfDocument = new
com.aspose.pdf.Document("/mnt/sdcard/input.pdf");
//setup new file to be added as attachment
com.aspose.pdf.FileSpecification fileSpecification = new
com.aspose.pdf.FileSpecification("/mnt/sdcard/sample.txt",
"Sample text file");
//add attachment to document's attachment collection
pdfDocument.getEmbeddedFiles().add(fileSpecification);
// Save updated document containing table object
pdfDocument.save("/mnt/sdcard/output.pdf");
The following code snippet shows you how to delete all the attachments from the PDF document.
//open document
com.aspose.pdf.Document pdfDocument = new
com.aspose.pdf.Document("/mnt/sdcard/input.pdf");
//delete all attachments
pdfDocument.getEmbeddedFiles().delete();
//save updated file
pdfDocument.save("/mnt/sdcard/output.pdf");
The following code snippet shows you how to get an individual attachment from the PDF document.
//open document
com.aspose.pdf.Document pdfDocument = new
com.aspose.pdf.Document("/mnt/sdcard/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("/mnt/sdcard/"+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();
Sponsor Ads
Created on Dec 31st 1969 18:00. Viewed 0 times.