pCOS Cookbook

cookbook

pages/page_initialview

Determine the page at which Acrobat opens the document.

Download Java Code     Show Output     Show Input PDF

package com.pdflib.cookbook.pcos.pages;


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

/**
 * Determine the page at which Acrobat opens the document
 * <p>
 * Required software: any pCOS interface 
 * <br>
 * Required data: PDF document
 * 
 * @version $Id: initialview.java,v 1.1 2016/06/24 12:22:26 stm Exp $
 */
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,
        String full_rcs_file_name, String revision) {
        super(argv, readable_name, search_path, full_rcs_file_name, revision);
    }

    public static void main(String argv[]) {
        initialview example = new initialview(argv, "Initial view", SEARCH_PATH,
            "$RCSfile: initialview.java,v $", "$Revision: 1.1 $");
        example.execute();
    }
}