Example usage for com.itextpdf.text.pdf PdfArray listIterator

List of usage examples for com.itextpdf.text.pdf PdfArray listIterator

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfArray listIterator.

Prototype

public ListIterator<PdfObject> listIterator() 

Source Link

Document

Returns the list iterator for the array.

Usage

From source file:commentextractor.CommentExtractorApp.java

License:GNU General Public License

static String extractComments(String filename, int first, int last) {
    StringBuffer output = null;/*from w  w  w .  ja v  a  2s  . c om*/
    try {
        PdfReader reader = new PdfReader(filename);

        if (last >= reader.getNumberOfPages() || (last == -1)) {
            last = reader.getNumberOfPages();
        }

        output = new StringBuffer(1024);

        for (int i = first; i <= last; i++) {

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

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

            annotsArray = page.getAsArray(PdfName.ANNOTS);
            for (ListIterator<PdfObject> iter = annotsArray.listIterator(); iter.hasNext();) {
                PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(iter.next());
                PdfString content = (PdfString) PdfReader.getPdfObject(annot.get(PdfName.CONTENTS));
                if (content != null) {
                    output.append("----------\n");
                    output.append("Page " + i);
                    output.append("\n");
                    output.append(content.toUnicodeString().replaceAll("\r", "\r\n"));
                    output.append("\n");
                }
            }
        }
    } catch (Exception e) {
        Logger.getLogger(CommentExtractorApp.class.getName()).log(Level.SEVERE, null, e);
    }
    return new String(output);
}