package com.pdflib.cookbook.pcos.interactive;

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

/** 
 * Retrieve digital signature information.
 * <p>
 * Required software: pCOS interface 3 (pCOS 2.x, PDFlib 7.x, TET 2.2,
 * PLOP 3.x)<br>
 * Required data: PDF document
 * 
 * @version $Id: signatures.java,v 1.3 2007/09/19 08:45:30 stm Exp $
 */
public class signatures 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(pCOS p, String filename) throws pCOSException, Exception {
        /* Open the PDF document */
        int doc = p.open_document(filename, "");
        if (doc == -1)
            throw new Exception("Error: " + p.get_errmsg());
        
        System.out.println("File name: " + p.get_string(doc, "filename"));
        
        String objtype = p.get_string(doc, "type:/Root/Perms");
        String objtype2 = p.get_string(doc,
            "type:/Root/ViewerPreferences/Rights");

        if (objtype.equals("dict") || objtype2.equals("dict")) {
            System.out.println("Reader-enabled: yes\n");
        }

        int fieldcount = (int) p.get_number(doc, "length:fields");

        int sigcount = 0;

        for (int f = 0; f < fieldcount; f++) {
            objtype = p.get_string(doc, "type:fields[" + f + "]/FT");
            if (!objtype.equals("name"))
                continue;

            String res = p.get_string(doc, "fields[" + f + "]/FT");
            if (!res.equals("Sig"))
                continue;

            sigcount++;

            objtype = p.get_string(doc, "type:fields[" + f + "]/T");

            if (!objtype.equals("string"))
                continue;

            /* invisible signatures have /Rect [0 0 0 0] */
            boolean invisible =
                p.get_number(doc, "fields[" + f + "]/Rect[0]") == 0
                && p.get_number(doc, "fields[" + f + "]/Rect[1]") == 0
                && p.get_number(doc, "fields[" + f + "]/Rect[2]") == 0
                && p.get_number(doc, "fields[" + f + "]/Rect[3]") == 0;
            System.out.print(invisible ? "Invisible" : "Visible");

            res = p.get_string(doc, "fields[" + f + "]/fullname");
            System.out.print(" signature field '" + res + "': ");

            objtype = p.get_string(doc, "type:fields[" + f + "]/V");

            if (objtype.equals("dict")) {
                System.out.println("signed");

                /* dump various pieces of relevant signature information */

                res = p.get_string(doc, "fields[" + f + "]/V/Filter");
                System.out.println("Filter: '" + res + "'");

                objtype = p.get_string(doc, "type:fields[" + f + "]/V/Name");
                if (objtype.equals("string")) {
                    res = p.get_string(doc, "fields[" + f + "]/V/Name");
                    System.out.println("Name: '" + res + "'");
                }
                objtype = p.get_string(doc, "type:fields[" + f + "]/V/Reason");
                if (objtype.equals("string")) {
                    res = p.get_string(doc, "fields[" + f + "]/V/Reason");
                    System.out.println("Reason: '" + res + "'");
                }
                objtype = p.get_string(doc, "type:fields[" + f + "]/V/M");
                if (objtype.equals("string")) {
                    res = p.get_string(doc, "fields[" + f + "]/V/M");
                    System.out.println("Time of signing: '" + res + "'");
                }

                /* a document may contain more than one signature */
                System.out.println();
            }
            else {
                System.out.println("unsigned");
            }
        }
        if (sigcount == 0)
            System.out.println("no signature fields");
        
        p.close_document(doc);
    }
    
    public signatures(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[]) {
        signatures example = new signatures(argv, "Digital signature",
            SEARCH_PATH, "$RCSfile: signatures.java,v $", "$Revision: 1.3 $");
        example.execute();
    }
}
