pCOS Cookbook

cookbook

interactive/named_destinations

Print the names of all destinations which are defined in the document.

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

/*
 * List the named destinations which are defined in the document.
 * For each valid destination its destination page, type and parameters are listed.
 * <p>
 * Required software: pCOS interface 3 (PDFlib+PDI/PPS 7.x, TET 2.2,
 * PLOP 3.x) <br>
 * Required data: PDF document
 */
package com.pdflib.cookbook.pcos.interactive;

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

public class named_destinations extends pcos_cookbook_example {

    /* This is where the data files are. Adjust as necessary. */
    private final static String SEARCH_PATH = "../input";

    private final static String KEY = "names/Dests";
    
    // Convenience method for retrieving an object which may have type "number" or "null"
    private String get_number_or_null(IpCOS p, int doc, String path) throws Exception
    {
    	String objtype = p.pcos_get_string(doc, "type:" + path);
    	
    	if (objtype.equals("number"))
    		return (String) p.pcos_get_string(doc, path);
    	else
    		return "unchanged";	// treat everything else as "null" which means "unchanged"
    }

    public void example_code(IpCOS p, int doc) throws Exception {

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

        int destinationCount = (int) p.pcos_get_number(doc, "length:" + KEY);
    	int usedDestinationCount = 0;

        for (int i = 0; i < destinationCount; ++i) {
        	/* The first element in the destination array contains the
        	 * destination page's object number. The "destpage" pseudo object
        	 * conveniently converts this to a page number.
        	 */
            int destpage = (int) p.pcos_get_number(doc, KEY + "[" + i + "]/destpage");

            /* Ignore destinations without any valid destination page. These
             * probably referred to pages which have been deleted
             */
            if (destpage == -1)
                continue;
            
            usedDestinationCount++;
            System.out.print("named destination '" + p.pcos_get_string(doc, KEY + "[" + i + "].key") + "' ");
            System.out.print("with destination page " + destpage);

            /* Read the fitting type of the destination and analyze the corresponding parameters */            
            String fittype = p.pcos_get_string(doc, KEY + "[" + i + "].val/D[1]");

            if (fittype.equals("XYZ")) {
            	System.out.print("\n    fit with zoom (/XYZ):" +
            			" left=" + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[2]") +
            			" top="  + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[3]") +
            			" zoom=" + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[4]"));
            
            } else if (fittype.equals("Fit")) {
            	System.out.print("\n    fit full page (/Fit)");
            
	        } else if (fittype.equals("FitH")) {
	        	System.out.print("\n    fit horizontally (/FitH)" +
            			" top="  + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[2]"));
	        
		    } else if (fittype.equals("FitV")) {
		    	System.out.print("\n    fit vertically (/FitV)" +
            			" left=" + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[2]"));
		    
			} else if (fittype.equals("FitR")) {
                System.out.print("\n    fit rectangle (/FitR) [" +
                	get_number_or_null(p, doc, KEY + "[" + i + "].val/D[2]") + ", " +
                	get_number_or_null(p, doc, KEY + "[" + i + "].val/D[3]") + ", " +
                	get_number_or_null(p, doc, KEY + "[" + i + "].val/D[4]") + ", " +
                	get_number_or_null(p, doc, KEY + "[" + i + "].val/D[5]") + "]");		    
			
			} else if (fittype.equals("FitB")) {
				System.out.print("\n    fit bounding box (/FitB)");
			
			} else if (fittype.equals("FitBH")) {
				System.out.print("\n    fit bounding box horizontally (/FitBH)" +
            			" top="  + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[2]"));
			
			} else if (fittype.equals("FitBV")) {
				System.out.print("\n    fit bounding box vertically (/FitBV)" +
            			" left=" + get_number_or_null(p, doc, KEY + "[" + i + "].val/D[2]"));
			}
		    System.out.println();
        }
        
        System.out.print(destinationCount + " named destinations found");
        System.out.println(" (" + usedDestinationCount + " used)");
    }

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

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