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.
Hi John,
Dim doc
As New Document(dataDir + “Test.pdf”)<o:p></o:p>
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,
Hi John,
squierj:
The annotation count property is 7 but when I loop through the collection there are only 4 annotations.
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).1- Widget2- Popup3- Text4- FreeText5- Attachment
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.
Hi John,