pCOS Cookbook

cookbook

interactive/annotations updated

Retrieve the contents of all annotations.

Download Java Code  Show Output  Show Input (pCOS-path-reference.pdf) 

/*
 * Retrieve the contents of all annotations.
 * 
 * If present, the annotation name (/NM) is emitted and the connection to
 * a corresponding Popup annotation is reported. The parent annotation
 * corresponding to a Popup can be identified via its name or pCOS ID.
 * 
 * See the following Cookbooks for retrieving additional information for
 * annotations:
 * - The topic "weblinks" demonstrates how to retrieve the target URL for
 *   Web links
 * - The topic "named_destinations" demonstrates how to retrieve the target
 *   page and location for named destinations.
 * - The topic "link_destinations" deals with various kinds of link
 *   destinations.
 *
 * Required software: pCOS interface 8 (PDFlib+PDI/PPS 9, TET 4.1, PLOP 5.0)
 * Required data: PDF document
 */
package com.pdflib.cookbook.pcos.interactive;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import com.pdflib.IpCOS;
import com.pdflib.cookbook.pcos.pcos_cookbook_example;

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(IpCOS p, int doc) throws Exception {
        
        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);
                }
            }
        }
    }

    /**
     * Print information about a single annotation on a page.
     * 
     * @param p
     *            IpCOS 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 Exception
     */
    private void print_annotation(IpCOS p, int doc, String annotations_path,
                                            int ann) throws Exception {
        /*
         * 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 + "]";
        
        /**
         * Separator to use as paragraph break. Text in PDF annotations uses
         * U+000D as paragraph separator which is impractical in many environments,
         * therefore we replace it.
         */
        final String SEPARATOR = "\n";
        
        String objtype;

        /*
         * 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 pCOS ID of the annotation.
         */
    	System.out.println("\n\tpcosid=" + p.pcos_get_string(doc, "pcosid:" + annotation_path));
        
        /*
         * Print the annotation name if present.
         */
        objtype = p.pcos_get_string(doc, "type:" + annotation_path + "/NM");
        if (objtype.equals("string")) {
            String name = p.pcos_get_string(doc, annotation_path + "/NM");
            System.out.print("\n\tName = '" + name + "'");
        }
        
        /*
         * Check whether the annotation has a corresponding Popup annotation.
         */
        objtype = p.pcos_get_string(doc, "type:" + annotation_path + "/Popup");
        if (objtype.equals("dict")) {
            System.out.print("\n\tPopup annotation with pcosid=" +
            		p.pcos_get_string(doc, "pcosid:" + annotation_path + "/Popup")
            		+ " attached");
        }
        
        /*
         * Check the parent annotation of a Popup annotation.
         */
        objtype = p.pcos_get_string(doc, "type:" + annotation_path + "/Parent");
        if (objtype.equals("dict")) {
            System.out.print("\n\tPopup references parent annotation ");

            objtype = p.pcos_get_string(doc, "type:" + annotation_path + "/Parent/NM");
            if (objtype.equals("name")) {
            	/* Emit the annotation name if present... */
            	System.out.print("'" + p.pcos_get_string(doc, annotation_path + "/Parent/NM")  + "'");
            } else {
            	/* ...otherwise its pCOS ID */
            	System.out.print("with pcosid=" +
            			p.pcos_get_string(doc, "pcosid:" + annotation_path + "/Parent"));
            }
        }

        /*
         * Print the contents of the annotation if available.
         */
        objtype = p.pcos_get_string(doc, "type:" + annotation_path + "/Contents");
        if (objtype.equals("string")) {
            String contents = p.pcos_get_string(doc, annotation_path + "/Contents");
            contents = contents.replace("\r", SEPARATOR);
            System.out.print("\n\tContents = '" + contents + "'");
        }
        
        System.out.print("\n \n");
    }

    public annotations(String[] argv, String readable_name, String search_path) {
        super(argv, readable_name, search_path);
    }

    public static void main(String argv[]) {
        annotations example = new annotations(argv, "Annotations", SEARCH_PATH);
        example.execute();
    }
}