pCOS Cookbook

cookbook

pages/initialview

Determine the page at which Acrobat opens the document.

Download Java Code  Show Output  Show Input (articles.pdf) 

/*
 * Determine the page at which Acrobat opens the document
 *
 * Required software: any pCOS interface 
 *
 * Required data: PDF document
 */
package com.pdflib.cookbook.pcos.pages;


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

public class initialview 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"));
        
        /* get number of pages in the document */
        int n_pages = (int) p.pcos_get_number(doc, "length:pages");

        /* Determine the page at which Acrobat opens the document */

        final int pcos_ot_array = 5;
        final int pcos_ot_dict = 6;

        int pageid;
        String path = "";
        int firstpage = 1;
        int objtype = (int) p.pcos_get_number(doc, "type:/Root/OpenAction");

        if (objtype == pcos_ot_array) {
            /* Found an OpenAction with a destination array */
            path = "/Root/OpenAction[0]";
        }
        else if (objtype == pcos_ot_dict) {
            /*
             * Found an OpenAction with an action dictionary. Fetch the /D
             * destination and determine the corresponding page id.
             */
            path = "/Root/OpenAction/D[0]";
        }

        if (!path.isEmpty()) {
            objtype = (int) p.pcos_get_number(doc, "type:" + path);

            if (objtype == pcos_ot_dict) {
                pageid = (int) p.pcos_get_number(doc, "pcosid:" + path);

                /*
                 * Determine the page number of the opening page by comparing
                 * the pCOS ID of the OpenAction target with the ID of each
                 * page.
                 */
                for (int pageno = 1; pageno <= n_pages; ++pageno) {
                    /* The index into the "pages" array is 0-based */
                    if (pageid == (int) p.pcos_get_number(doc,
                        "pcosid:pages[" + (pageno - 1) + "]")) {
                        firstpage = pageno;
                        break;
                    }
                }
            }
        }

       
        /* Acrobat will open the document at page "firstpage" */
        System.out.println("  Document opens at page " + firstpage);
        System.out.println();
    }

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

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