Missing Annotations in Annotation collection

I have a simple one page pdf that has multiple annotations on it. I am attempting to get all of the annotations by looping through the Page’s annotation collection. I am attaching the pdf in question.


The pdf has 1 formfield, 1 Sticky Note with a comment and that comment has a reply, 1 Text Comment and 1 File Attachment.
Dim doc As Document = New Document(“C:\gems\userdata\PDFTemplates\Test.pdf”)

For pageNumber As Integer = 1 To doc.Pages.Count
For Each annot As Annotation In doc.Pages(pageNumber).Annotations
’ Do Stuff…
Next
Next

When I check doc.Pages(pageNumber).Annotations.Count it tells me there are 7 annotations in the collection but when looping through them I only get 4:
1. WidgetAnnotation - Appears to be the formfield
2. TextAnnotation - This is the text I added to the sticky note
3. PopupAnnotation - The sticky note itself
4. TextAnnotaion - This is the reply to Text #2

Any ideas where the other text comment and file attachment are?

I also tried using the Aspose.Pdf.Facades.PdfAnnotationEditor.ExtractAnnotations at that didn’t work either. I also looked in doc.EmbeddedFiles for the file itself but it was empty.

Hi John,

Thank for contacting support.

In order to get/download the files from the comments inside PDF document you need to find FileAttachment type of Annotation. Whereas the comments can be get/read by extracting TextAnnotation from the PDF document. Please check the following code snippet where I managed to get/download the attachment and comments from the PDF document which you have shared.

Dim doc As New Document(dataDir + “Test.pdf”)

For Each annotation As Annotation In doc.Pages(1).Annotations

If annotation.AnnotationType = AnnotationType.FileAttachment Then

Dim annot As FileAttachmentAnnotation = DirectCast(annotation, FileAttachmentAnnotation)

Dim fileContent As Byte() = New Byte(annot.File.Contents.Length - 1) { }

annot.File.Contents.Read(fileContent, 0, fileContent.Length)

Dim fileStream As New FileStream(dataDir + annot.File.Name + "_out" + ".txt", FileMode.Create)

fileStream.Write(fileContent, 0, fileContent.Length)

fileStream.Close()

ElseIf annotation.AnnotationType = AnnotationType.Text Then

Console.Write(annotation.Contents.ToString() + Environment.NewLine)

End If

Next

Please try above code snippet to get/download file attachments and comments from the PDF. In case if you need any further assistance please feel free to contact us.

Best Regards,

Hi Asad Ali,


Thank you for your reply. I tried your code but the problem seems to be that the FileAttachment type of annotation doesn’t seem to be in the annotation collection. The annotation count property is 7 but when I loop through the collection there are only 4 annotations.


Hi John,


Thanks for writing back.

squierj:
The annotation count property is 7 but when I loop through the collection there are only 4 annotations.

I have again tested the scenario with the input file which you have shared and noticed that there are 5 following annotation types inside the document.

1- Widget
2- Popup
3- Text
4- FreeText
5- Attachment

As shared earlier that the comments inside the document can be found in TextAnnotation Type and you can find attachment in the comment by checking AttachmentAnnotation. Please note that there are total 7 annotations in the Annotations Collections of the document which are of 5 types. As you can see in my earlier shared code snippet that I have put a check on AnnotationType and done the required things (i.e download attachment and read comments).

However I have added following code lines into my code snippets to view the annotations so that you can have better understanding about the annotations inside your documents. I am also attaching an image of console output for your reference.

Dim i As Integer = 0
Dim doc As New Document(dataDir + “TestForAnnot.pdf”)
For Each annotation As Annotation In doc.Pages(1).Annotations
i += 1
Console.WriteLine("Iteration # : " + i.ToString())
Console.WriteLine("AnnotationType : " + annotation.AnnotationType.ToString())

In case if you have any further inquiry or any different requirements in this regard, please let us know.


Best Regards,

I found the issue. I was using an older version of aspose.pdf and apparently it wasn’t getting all of the annotation types. I upgraded to the latest version and all of the types now appear when looping through the collection.


Thank you so much for your help!

John

Hi John,


Thanks for your feedback. It is good to know that your issue has been resolved. Please keep using our API and in case of any inquiry, please feel free to ask. We will be more than happy to extend our support.


Best Regards,