pCOS Cookbook

cookbook

interchange/document_metadata

Retrieve the document's XMP meta data.

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

/*
 * Retrieve the document's XMP meta data.
 *
 * 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.io.IOException;

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

public class document_metadata 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/Metadata";

    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("stream")) {
            byte[] contents = p.pcos_get_stream(doc, "", KEY);
            if (contents != null && contents.length > 0) {
                try {
                    System.out.write(contents);
                }
                catch (IOException e) {
                    System.err.println("IOException occurred:");
                    System.err.println(e);
                }
            }
            else {
                System.out.println("Not found");
            }
        }
        else {
            System.out.println("Not found");
        }
    }

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

    public static void main(String argv[]) {
        document_metadata example = new document_metadata(argv,
            "Document XMP metadata", SEARCH_PATH);
        example.execute();
    }
}