package com.pdflib.cookbook.pcos.special;

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

/**
 * Create a list of all layers contained in a PDF document and
 * show their names.
 * <p>
 * This topic will catch all layers (even those which are not presented
 * in Acrobat's user interface), but it will not reflect the default
 * layer hierarchy, and will not show text labels which can separate
 * layer names.
 * <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: layers.java,v 1.4 2007/10/04 06:59:05 stm Exp $
 */
public class layers extends pcos_cookbook_example {

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

    private final static String KEY = "/Root/OCProperties";

    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:" + KEY);
        if (!objtype.equals("dict")) {
            System.out.println("No layers found\n");
            return;
        }

        objtype = p.get_string(doc, "type:" + KEY + "/OCGs");
        if (!objtype.equals("array")) {
            System.out.println("PDF error in OCGProperties dict");
            return;
        }

        int numentries = (int) p.get_number(doc, "length:" + KEY + "/OCGs");

        for (int i = 0; i < numentries; i++) {
            String key = KEY + "/OCGs[" + i + "]/Name";
            
            objtype = p.get_string(doc, "type:" + key);
            
            if (!objtype.equals("null")) {
                System.out.println(p.get_string(doc, key));
            }
        }

        p.close_document(doc);
    }

    public layers(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[]) {
        layers example = new layers(argv, "Layer names",
            SEARCH_PATH, "$RCSfile: layers.java,v $", "$Revision: 1.4 $");
        example.execute();
    }
}
