pCOS Cookbook

cookbook

interchange/dumper

pCOS sample application for dumping PDF information.

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

/*
 * pCOS sample application for dumping PDF information
 * 
 * Required software: pCOS interface 8 (PDFlib+PDI/PPS 9, TET 4.1, PLOP 5.0)
 * Required data: PDF document
 */
package com.pdflib.cookbook.pcos.interchange;

import java.text.DecimalFormat;


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

public class dumper 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 {
        int count;
        int i;
        boolean plainmetadata;
        String objtype;
        
        int pcosinterface = (int) p.pcos_get_number(doc, "pcosinterface");

        /* --------- general information (always available) */

        String pcosmode = p.pcos_get_string(doc, "pcosmodename");

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

        System.out.println(" PDF version: "
            + p.pcos_get_string(doc, "pdfversionstring"));

        System.out.println("  Encryption: "
            + p.pcos_get_string(doc, "encrypt/description"));
        
        /*
         * Starting with pCOS interface 11 it is possible to open files
         * that are encrypted with certificate security, and to retrieve
         * information about the recipients and a digital id supplied to
         * open the file.
         * 
         * Note that this pCOS Cookbook example is not prepared out-of-the-box
         * for passing a digital id to open a file encrypted with certificate
         * security. The code would have to be modified to pass a digital id
         * to the pcos_open_document() function.
         * 
         * On Windows this example program can decrypt an input file
         * encrypted with certificate security with a suitable digital id
         * installed in the Windows Certificate Store. In this case it is
         * sufficient to pass the option "engine=mscapi" to the
         * the pcos_open_document() function.
         */
        if (pcosinterface >= 11) {
            int recipientcount =
                (int) p.pcos_get_number(doc, "length:encrypt/recipients");
    
            if (recipientcount > 0)
            {
                System.out.println("Recipient groups: " + recipientcount);
                System.out.print("  Digital ID: ");
        
                if (pcosmode.equals("full"))
                    System.out.println("supplied digital ID has master permissions");
                else if (pcosmode.equals("restricted"))
                    System.out.println("supplied digital ID doesn't have master permissions");
                else
                    System.out.println("no suitable digital ID supplied");
            }
        }

        System.out.println("   Master pw: "
            + ((p.pcos_get_number(doc, "encrypt/master") != 0) ? "yes" : "no"));

        System.out.println("     User pw: "
            + ((p.pcos_get_number(doc, "encrypt/user") != 0) ? "yes" : "no"));

        System.out.println("Text copying: "
            + ((p.pcos_get_number(doc, "encrypt/nocopy") != 0) ? "no" : "yes"));

        System.out.println("  Linearized: "
            + ((p.pcos_get_number(doc, "linearized") != 0) ? "yes" : "no"));

        System.out.println();

        if (pcosmode.equals("minimum")) {
            System.out.println("Minimum mode: no more information available");
            p.delete();
            System.exit(0);
        }

        /* --------- more details (requires at least user password) */

        System.out.println("PDF/X status: " + p.pcos_get_string(doc, "pdfx"));

        System.out.println("PDF/A status: " + p.pcos_get_string(doc, "pdfa"));

        boolean xfa_present = !p
            .pcos_get_string(doc, "type:/Root/AcroForm/XFA").equals("null");
        System.out.println("    XFA data: " + (xfa_present ? "yes" : "no"));

        System.out.println("  Tagged PDF: "
            + ((p.pcos_get_number(doc, "tagged") != 0) ? "yes" : "no"));

        System.out.println("No. of pages: "
            + (int) p.pcos_get_number(doc, "length:pages"));

        DecimalFormat page_dim_format = new DecimalFormat();
        page_dim_format.setMinimumFractionDigits(0);
        page_dim_format.setMaximumFractionDigits(3);

        System.out
            .println(" Page 1 size: width="
                + page_dim_format.format(p.pcos_get_number(doc,
                    "pages[0]/width"))
                + ", height="
                + page_dim_format.format(p.pcos_get_number(doc,
                    "pages[0]/height")));

        count = (int) p.pcos_get_number(doc, "length:fonts");
        System.out.println("No. of fonts: " + count);

        for (i = 0; i < count; i++) {
            String fonts;

            fonts = "fonts[" + i + "]/embedded";
            if (p.pcos_get_number(doc, fonts) != 0)
                System.out.print("embedded ");
            else
                System.out.print("unembedded ");

            fonts = "fonts[" + i + "]/type";
            System.out.print(p.pcos_get_string(doc, fonts) + " font ");
            fonts = "fonts[" + i + "]/name";
            System.out.println(p.pcos_get_string(doc, fonts));
        }

        System.out.println();

        plainmetadata = p.pcos_get_number(doc, "encrypt/plainmetadata") != 0;

        if (pcosmode.equals("restricted") && !plainmetadata
            && p.pcos_get_number(doc, "encrypt/nocopy") != 0) {
            System.out.println("Restricted mode: no more information available");
            p.delete();
            System.exit(0);
        }

        /* ----- document info keys and XMP metadata (requires master pw) */

        count = (int) p.pcos_get_number(doc, "length:/Info");

        for (i = 0; i < count; i++) {
            String info;
            String key;
            int len;

            info = "type:/Info[" + i + "]";
            objtype = p.pcos_get_string(doc, info);

            info = "/Info[" + i + "].key";
            key = p.pcos_get_string(doc, info);
            len = 12 - key.length();
            while (len-- > 0)
                System.out.print(" ");

            System.out.print(key + ": ");

            /* Info entries can be stored as string or name objects */
            if (objtype.equals("name") || objtype.equals("string")) {
                info = "/Info[" + i + "]";
                System.out.println("'" + p.pcos_get_string(doc, info) + "'");
            }
            else {
                info = "type:/Info[" + i + "]";
                System.out.println("(" + p.pcos_get_string(doc, info)
                    + " object)");
            }
        }

        System.out.println();
        System.out.print("XMP metadata: ");

        objtype = p.pcos_get_string(doc, "type:/Root/Metadata");
        if (objtype.equals("stream")) {
            byte[] contents;

            contents = p.pcos_get_stream(doc, "", "/Root/Metadata");
            System.out.println(contents.length + " bytes");
        }
        else {
            System.out.println("not present");
        }
    }

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

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