PDFlib
BASKET
PDFlib

interactive/annotations

Retrieve the contents of all annotations.

Download Java Code     Show Output     Show Input PDF

package com.pdflib.cookbook.pcos.interactive;


import java.text.DecimalFormat;

import java.text.DecimalFormatSymbols;

import java.util.Locale;


import com.pdflib.pCOS;

import com.pdflib.pCOSException;

import com.pdflib.cookbook.pcos.pcos_cookbook_example;


/**

 * Retrieve the contents of all annotations.

 * <p>

 * Required software: pCOS interface 3 (pCOS 3.x, PDFlib+PDI/PPS 7.x, TET 2.2,

 * PLOP 3.x) <br>

 * Required data: PDF document

 *

 * @version $Id: annotations.java,v 1.9 2010/10/28 07:38:48 stm Exp $

 */

public class annotations extends pcos_cookbook_example {


    /* This is where the data files are. Adjust as necessary. */

    private final static String SEARCH_PATH = "../input";


    public void example_code(pCOS p, String filename) throws pCOSException,

        Exception {

        /* Open the PDF document */

        int doc = p.open_document(filename, "");

        if (doc == -1)

            throw new Exception("Error: " + p.get_errmsg());


        System.out.println("File name: " + p.pcos_get_string(doc, "filename"));


        int pagecount = (int) p.pcos_get_number(doc, "length:pages");


        for (int page = 0; page < pagecount; page++) {

            String annotations_path = "pages[" + page + "]/annots";


            int anncount = (int) p.pcos_get_number(doc, "length:"

                + annotations_path);


            if (anncount > 0) {

                System.out.println("Page " + (page + 1));


                for (int ann = 0; ann < anncount; ann++) {

                    print_annotation(p, doc, annotations_path, ann);

                }

            }

        }


        p.close_document(doc);

    }


    /**

     * Print information about a single annotation on a page.

     *

     * @param p

     *            pCOS object

     * @param doc

     *            document handle

     * @param annotations_path

     *            the pCOS path for the "annotations" array on the page

     *            ("pages[n]/annotations", with n = 0, 1, 2, ...)

     * @param ann

     *            index of the annotation in the "annotations" array of the

     *            page object

     *           

     * @throws pCOSException

     */

    private void print_annotation(pCOS p, int doc, String annotations_path,

                                            int ann) throws pCOSException {

        /*

         * pCOS path for the annotation in question: "pages[n]/annotations[m]"

         * with n = 0, 1, 2, ... and m = 0, 1, 2, ...

         */

        String annotation_path = annotations_path + "[" + ann + "]";

       

        /*

         * Print the type of the annotation.

         */

        String subtype = p.pcos_get_string(doc, annotation_path + "/Subtype");

        System.out.print("Subtype " + subtype + ", ");


        /*

         * Print the rectangle for the annotation.

         */

        System.out.print("Rectangle ");

        String rect_path = annotation_path + "/Rect";

        if (p.pcos_get_string(doc, "type:" + rect_path).equals("array")

                && (int) p.pcos_get_number(doc, "length:" + rect_path) == 4) {

            System.out.print("[");


            DecimalFormat format = new DecimalFormat();

            format.setMinimumFractionDigits(0);

            format.setMaximumFractionDigits(2);

            format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));


            for (int i = 0; i < 4; i += 1) {

                if (i > 0) {

                    System.out.print(" ");

                }

                System.out.print(format.format(p.pcos_get_number(doc, rect_path

                    + "[" + i + "]")));

            }

           

            System.out.print("], ");

        }

        else {

            System.out.print("<not available>, ");

        }


        /*

         * Print the contents of the annotation if they are available.

         */

        String objtype = p.pcos_get_string(doc, "type:" + annotation_path

            + "/Contents");

        if (objtype.equals("string")) {

            String contents = p.pcos_get_string(doc, annotation_path

                + "/Contents");

            System.out.println("contents = '" + contents + "'");

        }

        else {

            System.out.println("no contents found for annotation " + ann);

        }

    }


    public annotations(String[] argv, String readable_name, String search_path,

        String full_rcs_file_name, String revision) {

        super(argv, readable_name, search_path, full_rcs_file_name, revision);

    }


    public static void main(String argv[]) {

        annotations example = new annotations(argv, "Annotations", SEARCH_PATH,

            "$RCSfile: annotations.java,v $", "$Revision: 1.9 $");

        example.execute();

    }

}