How to remove attachments from a PDF

Hi,

I have generated a PDF using Aspose which contains some attachments files provided as link.

Now I need to split the PDF from the attachments. How can I achieve that ?

The list of attachments I can get by using the PDF Extractor but how can I get a pdf (from the main PDF) without the attached files.

The PDF Extractor is also not working. A PDF contains 5 attchements and I need to extract all attachments file. I am trying as follows:

PdfExtractor extractor = new PdfExtractor();
extractor.bindPdf(path + "Attach.pdf");
extractor.extractAttachment();
ArrayList names = extractor.getAttachNames();
ByteArrayOutputStream[] tempStreams = extractor.getAttachment();
for (int i=0; i<tempStreams.length; i++)
{
String name = (String)names.get(i);
FileOutputStream fos = new FileOutputStream(path + name);
ByteArrayInputStream bais = new ByteArrayInputStream(tempStreams[i].toByteArray());
byte[] tempBytes = new byte[4096];
for (; ; ) {
int numofBytes = bais.read(tempBytes);
if (numofBytes < 1) {
break;
}
fos.write(tempBytes);
}
}

But I am not getting the list of attachments and ONLY getting the first attchement. ByteArrayOutputStream[] tempStreams always returns 1 but it should return 5.

I am using java 2.7.0 Aspose and 3.7.0 PDF Kit.

Hi Sriman,

You may use deleteAttachments method of PdfContentEditor class to remove attachments and spare the PDF file. Also, regarding the issue you’re having while extracting the attachments, please try to use the latest version of Aspose.Pdf.Kit for Java.

If you still find the same problem then please share the input PDF file with us as well, so we could further investigate the issue.

We’re sorry for the inconvenience.
Regards,

Hi,

I tried to extract the attachments using the new Jars pdf 2.9.0. and pdf kit 3.9.0. Still I am facing the same issue. i.e its giving me only the first attachment.

The code template is same as my previous post.

Here I am attaching the test file.

Hi Sriman,

I have tested this issue at my end using Aspose.Pdf.Kit for Java 3.9.0, but couldn’t notice any such problem. The code not only returned correct attachment count, but also extracted all the attachments as well. Could you please double check at your end that the Aspose.Pdf.Kit for Java version is 3.9.0. Also, please make sure that the CLASSPATH is set to this latest version as well. If you still find that you’re using the latest version and face the same problem then please share some more details regarding your environment like OS, Java version etc. We’ll further investigate and guide you accordingly.

We’re sorry for the inconvenience.
Regards,


Hi, I tried again but no success.

ArrayList names = extractor.getAttachNames();

This above code returrns me the correct list of files.

But

ByteArrayOutputStream[] tempStreams = extractor.getAttachment();

the above line returns me only one attachment details.i.e the ByteArrayOutputStream[] contains only the first attachment.

I am using Java 1.6 and OS is WIndows 2007

If it works for you then Could you please share the code template ?

Hi Sriman,

I have attached the sample file with this post. I have tested the issue in the environment similar to you and it still works. I would really appreciate if you could check the version number of the component. You can check the version using META-INF folder in the package.

Regards,

Hey shahzad,

Thanks. It worked. But I got a strange behaviour here.

If you set the licence it works fine but if you are not setting the licence then you will get only the first attachment.(No exception or error for licence but returning only one attachment. STRANGE)

2nd Issue is : After extracting the file if I am saving it on disk. The excel file is getting corrupted but if u open this from the PDF its fine.

Attaching the extracted excel from PDF.

Hi Sriman,

Regarding the first issue you mentioned, I would like to share that in evaluation mode there are certain limitations on the component use. So it is always advised to use the licensed version to get complete results. Moreover, you may try the 30 days temporary license as well, if you haven’t yet purchased the license.

As far as the second issue is concerned, I have reproduced this problem at my end and logged it as PDFKITJAVA-30064 in our issue tracking system. Our team will investigate it in detail and you’ll be updated via this forum thread once it is resolved.

We’re sorry for the inconvenience.
Regards,