Getting PDF attachments from the 'comments'

Hi all,

I'm able to read the annotations from a file, and I can see that one of the annotations (annotation.contents) is a filename for the 'attachment' in the PDF file. How can I get the actual attachment?

Should I be doing something with: ((Aspose.Pdf.Annotations.FileAttachmentAnnotation)annotation).File.Name ?

Hi Rudy,


Thanks for contacting support.

In order to get/download the attached file(s) from the PDF document you need to use the EmbeddedFiles Collection of the Document’s object. This collection contains FileSpecification objects in it. You can retrieve an attached file’s information or file itself from FileSpecification object. Please check “Working With Attachments” article in our API documentation. In case of any further assistance please feel free to contact us.


Best Regards,
Hi Asad,

Thanks for your response. I ran the GetAllAttachments example for a PDF I have, but it does not see any of the 'attachments'. I say 'attachments' in quote because it looks like more of a comment. When I run the "GetAllAnnotationsFromPage" example, it finds the attachment link. But I don't know where to go from there.


Hi Rudy,

Thanks for your feedback.

rmenjivar:

When I run the “GetAllAnnotationsFromPage” example, it finds the attachment link. But I don’t know where to go from there.

It seems that the document from which you are trying to get attachment has FileAttachmentAnnotation in it. However in order to get/download a file from FileAttachmentAnnotation please check following code snippet.

Document doc = new Document(dataDir + “input.pdf”)
foreach (Annotation annotation in doc.Pages[1].Annotations)
{
    if (annotation.AnnotationType == AnnotationType.FileAttachment)
    {
        FileAttachmentAnnotation annot = (FileAttachmentAnnotation)annotation;
        byte[] fileContent = new byte[annot.File.Contents.Length];
        annot.File.Contents.Read(fileContent, 0,
        fileContent.Length);
        FileStream fileStream = new FileStream(dataDir + annot.File.Name + "_out" + ".txt",
        FileMode.Create);
        fileStream.Write(fileContent, 0, fileContent.Length);
        fileStream.Close();
    }
}

Please check in the above code that I have extracted content of the file from attachment and then save it to the disk using FileStream object. Please try using above approach to get the attachment from your document. In case if you still face any issue please share your input document so that we can test the scenario in our environment and share information accordingly.

Best Regards,

Thanks again Asad, this worked!

Hi Rudy,


Thanks for your feedback. It is good to know that your problem has been resolved by suggested code snippet. Please keep using our API and in case of any other query please feel free to let us know. We will be happy to extend our support.


Best Regards,