import java.io.*;
import com.aspose.pdf.Document;
import com.aspose.pdf.FileSpecification;
public class ExtractAttachment {
public static void main(String[] args) throws IOException {
Document pdfDocument = new Document(“C:\\Users\\shyam\\Desktop\\Eml\\AttachmentPdf.pdf”);
// Get particular embedded file
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();
System.out.println(fileSpecification.getName());
File file = new File(fileSpecification.getName());
// Create path for file from pdf
file.getParentFile().mkdirs();
// Create and extract file from pdf
FileOutputStream output = new FileOutputStream(file, false);
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer)))
output.write(buffer, 0, n);
File f = new File(fileSpecification.getName());
pdfDocument.save("C:\\Users\\shyam\\Desktop\\Eml\\" +f.getName());
System.out.println("file Extracted");
// Close InputStream object
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
// Close Document object
pdfDocument.dispose();
}
}
/////// Problem Statement////////
Sir, we are using the Aspose Sample code for Extracting Attachment from pdf file. The attachment file successfully extracted but problem is that extracted file is in non-readable format( when we want to open Extracted Attachment file then it is showing an error:- " File format is not supported").
image.png (1.3 KB)