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

6 comments:

Ben said...

You are wonderful. Thank you.

Anonymous said...

I can not claim complete originality for all things in this study. [url=http://www.mulberryhandbagssale.co.uk]http://www.mulberryhandbagssale.co.uk[/url You may do something once a day, once a week, once a month. [url=http://www.goosecoatsale.ca]canada goose women parka[/url] Llgvxudsb
[url=http://www.pandorajewelryvip.co.uk]pandora store[/url] Nrsnuoaxo [url=http://www.officialcanadagooseparkae.com]canada goose outlet[/url] lozdlgwri

Anonymous said...

But for many developers, all of this came to a halt this week when Roku removed 25 channels with foreign-language content, effectively shutting down the entire international section of its channel store.. http://www.morenorthface.com Reggie acknowledges that they trying to get more units into stores as fast as possible while still trying to make the Wii U sound like an item so popular that it is flying off shelves.. http://www.fitghdhair.com It those so-called regular users that are the key tothis deal, all those folks that have never played a Zynga game before, or any mobile game for that matter. http://www.wellnorthface.com He said a PR person should look presentable they should represent the company. ghd online Radical fats want to tear up the rulebook and spit on everything they been told about clothes.

Anonymous said...

What i do not realize is if truth be told how
you're no longer really much more neatly-liked than you may be right now. You are so intelligent. You already know thus significantly in the case of this topic, produced me in my view consider it from so many numerous angles. Its like women and men aren't interested unless it is one thing
to do with Girl gaga! Your own stuffs outstanding.
All the time handle it up!

Here is my web site; online graduate certificates

Unknown said...

Smile, smile, smile at your mind as often as possible. Your smiling will considerably reduce your mind's tearing tension. See the link below for more info.


#tearing
www.ufgop.org

jorgeefrrr828 said...

It's best to participate in a contest for top-of-the-line blogs on the web. I'll advocate this site! casino play