Thursday, April 29, 2010

How to extract comments from pdf using Itext

Hi Friends,

Here is the code of extracting comments from the PDF file using the itext.This extracts the comments of each page one by one and print them on the console.


/******************************************************************************/
package pdftest;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfString;
import java.util.ListIterator;

public class CommentsTest {

public static void main(String args[]) {
try {
System.out.println("Hi in the Comments Test");
PdfReader reader = new PdfReader("c:\\temp\\testattachments.pdf");

for (int i = 1; i <= reader.getNumberOfPages(); i++)
{

PdfDictionary page = reader.getPageN(i);
PdfArray annotsArray = null;

if(page.getAsArray(PdfName.ANNOTS)==null)
continue;

annotsArray = page.getAsArray(PdfName.ANNOTS);
for (ListIterator iter = annotsArray.listIterator(); iter.hasNext();)
{
PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(iter.next());
PdfString content = (PdfString) PdfReader.getPdfObject(annot.get(PdfName.CONTENTS));
if (content != null) {
System.out.println(content);
}
}
}


} catch (Exception e) {
e.printStackTrace();
}

}
}

/******************************************************************************/


Regards,
Vajahat Ali