pCOS Cookbook

cookbook

pages/blocks

Create a list of all blocks contained on the pages of a PDF document and show their name, type and dimensions.

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

/*
 * Create a list of all blocks contained on the pages of a PDF document and show
 * their name, type and dimensions.
 *
 * 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 blocks 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
                + "]/blocks");
            if (objtype.equals("null"))
                continue;

            int blockcount = (int) p.pcos_get_number(doc, "length:pages["
                + page + "]/blocks");
            if (blockcount == 0)
                continue;
            else
                System.out.println("Blocks on page " + (page + 1) + ":");

            for (int b = 0; b < blockcount; b++) {
                String val = p.pcos_get_string(doc, "pages[" + page + "]"
                    + "/blocks[" + b + "]/Subtype");

                System.out.print(val + " Block ");

                val = p.pcos_get_string(doc, "pages[" + page + "]"
                    + "/blocks[" + b + "]/Name");
                System.out.println("'" + val + "':");

                for (int j = 0; j < 4; j++) {
                    System.out.print(p.pcos_get_number(doc, "pages[" + page + "]"
                        + "/blocks[" + b + "]/Rect[" + j + "]")
                        + " ");
                }
                System.out.println();
            }
        }
    }

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

    public static void main(String argv[]) {
        blocks example = new blocks(argv, "PDFlib Blocks", SEARCH_PATH);
        example.execute();
    }
}