pCOS Cookbook

cookbook

interchange/image_metadata

Retrieve the XMP metadata for all images in the document. The metadata is written to a separate file for each image. The naming convention for the generated files is:
<basename of input document>_images[<n>]_Metadata.txt
with <n> being the index of the image in the "images" array.

Download Java Code     Show Output     Show image specific output     Show Input PDF

package com.pdflib.cookbook.pcos.interchange;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

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

/**
 * Retrieve the XMP metadata for all images in the document.
 * <p>
 * The metadata is written to a separate file for each image. The naming
 * convention for the generated files is:
 * <p>
 * &lt;basename of input document&gt;_images[&lt;n&gt;]_Metadata.txt
 * <p>
 * with &lt;n&gt; being the index of the image in the "images" array.
 * <p>
 * Required software: pCOS interface 3 (pCOS 3.x, PDFlib+PDI/PPS 7.x, TET 2.2,
 * PLOP 3.x) <br>
 * Required data: PDF document with images that have XMP metadata attached
 * 
 * @version $Id: image_metadata.java,v 1.9 2015/11/16 11:53:16 stm Exp $
 */
public class image_metadata 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(IpCOS p, int doc) throws Exception {

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

        /*
         * Loop over all images in the document and check whether there is a
         * Metadata stream attached. If a Metadata stream is found, the contents
         * are dumped to a file.
         */
        int imagecount = (int) p.pcos_get_number(doc, "length:images");
        for (int i = 0; i < imagecount; i += 1) {
            String image_path = "images[" + i + "]/Metadata";

            String objtype = p.pcos_get_string(doc, "type:" + image_path);

            if (objtype.equals("stream")) {
                xmp_dump(p, doc, filename, i, image_path);
            }
        }
    }

    private void xmp_dump(IpCOS p, int doc, String filename, int i,
        String image_path) throws Exception {
        /*
         * Strip relative pathname and ".pdf" suffix from input filename.
         */
        File path = new File(filename);
        String basename = strip_suffix(path.getName());

        /*
         * Create an output filename that has blanks and forward slashes
         * replaced with underscores
         */
        String output_filename = (basename + "_" + image_path + ".txt")
            .replaceAll("[ \\/]", "_");

        System.out.println("Writing metadata of image " + i + " to file \""
            + output_filename + "\" ...");

        byte[] contents = p.pcos_get_stream(doc, "", image_path);

        PrintStream output_file = new PrintStream(new FileOutputStream(
            output_filename));
        output_file.write(contents);
        output_file.close();
    }

    private String strip_suffix(String filename) {
        int dot_index = filename.lastIndexOf('.');

        return dot_index > 0 ? filename.substring(0, dot_index) : filename;
    }

    public image_metadata(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[]) {
        image_metadata example = new image_metadata(argv, "Image XMP metadata",
            SEARCH_PATH, "$RCSfile: image_metadata.java,v $",
            "$Revision: 1.9 $");
        example.execute();
    }
}