package com.pdflib.cookbook.pcos.interchange;

import com.pdflib.pCOSException;
import com.pdflib.pCOS;
import com.pdflib.cookbook.pcos.pcos_cookbook_example;

/**
 * pCOS sample application for dumping PDF information
 * 
 * Required software: pCOS interface 3 (pCOS 2.x, PDFlib 7.x, TET 2.2,
 * PLOP 3.x)<br>
 * Required data: PDF document
 * 
 * @version $Id: dumper.java,v 1.5 2007/09/20 06:40:19 stm Exp $
 */
public class dumper extends pcos_cookbook_example {
    // private static final String DOCOPTLIST = "requiredmode=minimum";
    private static final String DOCOPTLIST = "requiredmode=restricted password=demo";

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

    public void example_code(pCOS p, String filename) throws pCOSException,
        Exception {
        int count, pcosmode;
        int i;
        boolean plainmetadata;
        String objtype;

        /* Open the PDF document */
        int doc = p.open_document(filename, DOCOPTLIST);
        if (doc == -1)
            throw new Exception("Error: " + p.get_errmsg());
        
        /* --------- general information (always available) */

        pcosmode = (int) p.get_number(doc, "pcosmode");

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

        System.out.println(" PDF version: " + p.get_number(doc, "pdfversion")
            / 10);

        System.out.println("  Encryption: "
            + p.get_string(doc, "encrypt/description"));

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

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

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

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

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

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

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

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

        boolean xfa_present =
            !p.get_string(doc, "type:/Root/AcroForm/XFA").equals("null");
        System.out.println("    XFA data: " + (xfa_present ? "yes" : "no"));
        
        System.out.println("  Tagged PDF: "
            + ((p.get_number(doc, "tagged") != 0) ? "yes" : "no") + "\n");

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

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

        count = (int) p.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.get_number(doc, fonts) != 0)
                System.out.print("embedded ");
            else
                System.out.print("unembedded ");

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

        System.out.println();

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

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

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

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

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

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

            info = "/Info[" + i + "].key";
            key = p.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.get_string(doc, info) + "'");
            }
            else {
                info = "type:/Info[" + i + "]";
                System.out.println("(" + p.get_string(doc, info) + " object)");
            }
        }

        System.out.println();
        System.out.print("XMP meta data: ");

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

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

        
        p.close_document(doc);
    }
    
    public dumper(String[] argv, String readable_name,
        String search_path, String full_rcs_file_name, String revision) {
        super(argv, readable_name, search_path, full_rcs_file_name, revision);
    }

    public static void main(String argv[]) {
        dumper example = new dumper(argv, "General info",
            SEARCH_PATH, "$RCSfile: dumper.java,v $", "$Revision: 1.5 $");
        example.execute();
    }
}
