pCOS Cookbook

cookbook

interactive/weblinks

Retrieve the bounding box and the URL of all Web links on the page.

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

/*
 * Retrieve bounding box and URL of all Web links on all pages.
 *
 * 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 com.pdflib.IpCOS;
import com.pdflib.cookbook.pcos.pcos_cookbook_example;

public class weblinks 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 objtype = p.pcos_get_string(doc, "type:pages[" + page
                + "]/Annots");
            if (objtype.equals("null"))
                continue;

            int anncount = (int) p.pcos_get_number(doc, "length:pages[" + page
                + "]/Annots");
            if (anncount == 0)
                continue;

            for (int ann = 0; ann < anncount; ann++) {
                String subtype = p.pcos_get_string(doc, "pages[" + page
                    + "]/Annots[" + ann + "]/Subtype");
                if (!subtype.equals("Link"))
                    continue;

                objtype = p.pcos_get_string(doc, "type:pages[" + page
                    + "]/Annots[" + ann + "]/A/URI");
                if (objtype.equals("string")) {
                    String url = p.pcos_get_string(doc, "pages[" + page
                        + "]/Annots[" + ann + "]/A/URI");
                    System.out.println("Page " + (page + 1) + ": " + url);

                    System.out.print("    link rectangle=[");
                    for (int j = 0; j < 4; j++) {
                        System.out.print(p.pcos_get_number(doc, "pages[" + page
                            + "]" + "/Annots[" + ann + "]/Rect[" + j + "]")
                                + (j<3 ? " " : ""));
                    }
                    System.out.println("]");
                }
            }
        }
    }

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

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