Reading PDF Annotations Using iText

I am trying to get the contents of a PDF annotation to a string so that I can store this information in a search database.

Does anyone know how to do this using iText / iTextSharp?

+6
c # pdf itext
source share
2 answers

Yes, but the specifics really depend on what types of annotations you are talking about.

Generally:

PdfDictionary pageDict = myPdfReader.getPageN(firstPageIsOne); PdfArray annotArray = pageDict.getAsArray(PdfName.ANNOTS); for (int i = 0; i < annotArray.size(); ++i) { PdfDictionary curAnnot = annotArray.getAsDict(i); int someType = myCodeToGetAnAnnotsType(curAnnot); if (someType == THIS_TYPE) { writeThisType(curAnnot); } else if (someType == THAT_TYPE) { writeThatType(curAnnot); } } 

For more information, see the PDF Specification , in particular, annotation descriptions: "Chapter 12.5.6 Annotation Types".

If you can tell us what types you are interested in, I can help.

+2
source share

In the future, contact anyone who finds this question through Google, like me ...

If what you want to do is find the name and contents of the annotations with a note entry. You can do it (partly based on answer from Mark)

 PdfReader reader = new PdfReader(somePDF); PdfDictionary pageDict = reader.GetPageN(1); PdfArray annotArray = pageDict.GetAsArray(PdfName.ANNOTS); for (int i = 0; i < annotArray.Size; ++i) { PdfDictionary curAnnot = annotArray.GetAsDict(i); PdfString name = curAnnot.GetAsString(PdfName.T); PdfString contents = curAnnot.GetAsString(PdfName.CONTENTS); if (!string.IsNullOrWhiteSpace(name?.ToString())) { Console.WriteLine(name); } if (!string.IsNullOrWhiteSpace(contents?.ToString())) { Console.WriteLine(contents); } } 

In addition, to determine what you are looking for, you can open the PDF file in a text editor and search / annotate, and you will quickly find your annotation object.

+1
source share

All Articles