/* * 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: * * _images[]_Metadata.txt * * where is the index of the image in the "images" array. * * Required software: pCOS interface 8 (PDFlib+PDI/PPS 9, TET 4.1, PLOP 5.0) * Required data: PDF document with images that have XMP metadata attached */ 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; 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) { super(argv, readable_name, search_path); } public static void main(String argv[]) { image_metadata example = new image_metadata(argv, "Image XMP metadata", SEARCH_PATH); example.execute(); } }