pCOS Cookbook

cookbook

special/layers updated

Create a list of all layers contained in a PDF document and show their names.

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

/*
 * Create a list of all layers contained in a PDF document and show their names.
 *
 * This topic catches all layers (even those which are not presented in
 * Acrobat's user interface), but it doesn't reflect the default layer
 * hierarchy and doesn't show text labels which can separate layer names.
 * The layer hierarchy is shown in the corresponding topic "defaultlayers"
 * in the pCOS Cookbook.
 *
 * If a GTS_Metadata entry according to ISO 19593-1 "Processing Steps for
 * Packaging and Labels" is found, the corresponding group and type entries
 * are also emitted.
 *
 * Required software: pCOS interface 8 (PDFlib+PDI/PPS 9, TET 4.1, PLOP 5.0)
 * Required data: PDF document
 */
package com.pdflib.cookbook.pcos.special;

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

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(IpCOS p, int doc) throws Exception {

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

        String objtype = p.pcos_get_string(doc, "type:" + KEY);
        if (!objtype.equals("dict")) {
            System.out.println("No layers found\n");
            return;
        }

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

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

        for (int i = 0; i < numentries; i++) {
            String base = KEY + "/OCGs[" + i + "]";

            objtype = p.pcos_get_string(doc, "type:" + base + "/Name");

            if (!objtype.equals("null")) {
                System.out.print(p.pcos_get_string(doc, base + "/Name"));
            }

            /* Check Processing Steps metadata according to ISO 19593-1 */
            objtype = p.pcos_get_string(doc, "type:" + base + "GTS_Metadata");
            if (objtype.equals("dict")) {
            	System.out.print("  [Processing Steps: ");
            	
            	/* The GTS_ProcStepsGroup entry is required */
            	System.out.print("group=");
            	objtype = p.pcos_get_string(doc, "type:" + base + "GTS_Metadata/GTS_ProcStepsGroup");
                if (objtype.equals("name"))
                	System.out.print(p.pcos_get_string(doc, base + "GTS_Metadata/GTS_ProcStepsGroup"));
                else
                	System.out.print("(missing)");
                
                /* The GTS_ProcStepsType entry is optional */
            	objtype = p.pcos_get_string(doc, "type:" + base + "GTS_Metadata/GTS_ProcStepsType");
                if (objtype.equals("name")) {
                	System.out.print(", type=");
                	System.out.print(p.pcos_get_string(doc, base + "GTS_Metadata/GTS_ProcStepsType"));
                }
                System.out.print("]");
            }
            System.out.println();
        }
    }

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

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