pCOS Cookbook

cookbook

interchange/pieceinfo

pCOS sample application for searching PieceInfo for document, pages, and Form XObjects (templates).

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

/*
 * pCOS sample application for searching PieceInfo for document, pages,
 * and Form XObjects (templates)
 * 
 * PieceInfo dictionaries can be used to attach private data structures
 * to the document, a page, or a Form XObject. This code searches for PieceInfo
 * data dictionaries and lists their names. Real applications could access the
 * individual items inside the private data dictionary.
 * 
 * Some common names of application-specific PieceInfo dictionaries:
 *
 * - "PDFlib"
 *   PDFlib Blocks for PDFlib Personalization Server (PPS)
 *
 * - "Illustrator", "AdobePhotoshop", "InDesign"
 *   Information for editing the document in these Adobe applications
 *
 * - "ComparePDF"
 *   Information stored by Acrobat's "Compare Document" feature
 *
 * - "ADBE_CompoundType"
 *   Editable watermarks (headers, footers, stamps) created by Acrobat
 *
 * - "IBM-ODIndexes"
 *   Indexing information for IBM Content Manager on Demand (CMOD)
 *   (called "Page-Piece Dictionary" or PPD)
 *
 * - "Esko_Barcode", "Esko_Crosshair"
 *   Information for packaging production
 * 
 * Required software: pCOS interface 8 (PDFlib+PDI/PPS 9.x, TET 4.x,
 * PLOP 5.x)
 * Required data: PDF document
 */

package com.pdflib.cookbook.pcos.interchange;


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

public class pieceinfo 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 {

        /* Search PieceInfo in document catalog */
        search_pieceinfo(p, doc, "type:/Root/PieceInfo", "document catalog");
        
        /* Search PieceInfo in all pages */
        int pagecount = (int) p.pcos_get_number(doc, "length:pages");

        for (int page = 0; page < pagecount; page++) {
            search_pieceinfo(p, doc, "pages[" + page + "]/PieceInfo", "page " + (page+1));
            
            /*
             * Search PieceInfo in all templates (Form XObjects) on this page.
             * Due to a bug in older versions this works correctly only in TET 5.2, PDFlib+PDI 9.2.1 or above.
             */
            int tmplcount = (int) p.pcos_get_number(doc, "length:pages[" + page + "]/templates");
            
            for (int tmpl = 0; tmpl < tmplcount; tmpl++) {
                search_pieceinfo(p, doc, "pages[" + page + "]/templates[" + tmpl + "]/PieceInfo", "page " + (page+1) +  ", template " + tmpl);
            }
        }
    }

    private void search_pieceinfo(IpCOS p, int doc, String path, String location) throws Exception {
        String objtype = p.pcos_get_string(doc, "type:" + path);
        
        /* PieceInfo must exist and have type "dict" */
        if (objtype.equals("dict")) {
            int picount = (int) p.pcos_get_number(doc, "length:" + path);
                
            for (int pi = 0; pi < picount; pi++) {
                objtype = p.pcos_get_string(doc, "type:" + path + "[" + pi + "]");

                /* The type of each entry in PieceInfo must also be "dict" */
                if (objtype.equals("dict")) {
                    /* The dictionary name is the owner (application) of the data */
                    String piname = p.pcos_get_string(doc, path + "[" + pi + "].key");
                    System.out.println("PieceInfo '" + piname + "' in " + location);
                } else {
                    System.out.println("PieceInfo has wrong type '" + objtype + "' in " + location);
                    return;
                }
                
                /* Additional pCOS calls for retrieving application-specific PieceInfo
                 * entries in the "Private" dictionary would go here, e.g.
                   System.out.println("reading private entry: " + p.pcos_get_string(doc, path + "[" + pi + "]/Private/Version"));
                 */
            }
        }
    }

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

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