pCOS Cookbook

cookbook

pages/page_size

Retrieve the MediaBox and Rotation key for all pages.

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

/*
 * Retrieve the MediaBox, CropBox (if available) and Rotation key for 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.pages;

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

public class page_size 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");

        // Loop over all pages
        for (int page = 0; page < pagecount; page++) {
            String objtype;

            System.out.println("Page " + (page + 1) + ": ");

            // Print visible page size
            System.out.println("    width="
                + p.pcos_get_number(doc, "pages[" + page + "]/width")
                + ", height="
                + p.pcos_get_number(doc, "pages[" + page + "]/height"));

            // Check whether optional Rotate key is present
            objtype = p.pcos_get_string(doc, "type:pages[" + page + "]/Rotate");
            if (objtype.equals("number")) {
                System.out.println("    Rotate="
                    + p.pcos_get_number(doc, "pages[" + page + "]/Rotate"));
            }

            // Print MediaBox which must always be present
            System.out.print("    MediaBox=[");

            for (int i = 0; i < 4; i++) {
                System.out.print(p.pcos_get_number(doc, "pages[" + page + "]/MediaBox[" + i + "]")
                    + (i<3 ? " " : ""));
            }
            System.out.println("]");

            // Check whether optional CropBox is present
            objtype = p.pcos_get_string(doc, "type:pages[" + page + "]/CropBox");
            System.out.print("    CropBox");

            if (objtype.equals("array")) {
                System.out.print("=[");
                for (int i = 0; i < 4; i++) {
                    System.out.print(p.pcos_get_number(doc, "pages[" + page + "]/CropBox[" + i + "]")
                        + (i<3 ? " " : "]\n"));
                }
            } else {
                System.out.println(" not available");
            }
        }
    }

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

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