pCOS Cookbook

cookbook

interchange/unpack_portfolio

Extract files from PDF portfolio to disc.

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

/*
 * Extract documents from a PDF Portfolio (called "collection" in ISO 32000)
 *
 * 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 com.pdflib.IpCOS;
import java.io.FileOutputStream;
import java.io.OutputStream;

import com.pdflib.cookbook.pcos.pcos_cookbook_example;

public class unpack_portfolio 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 {

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

        /* Check whether the document represents a PDF Portfolio */
        String colltype = p.pcos_get_string(doc, "type:/Root/Collection");
        if (colltype.equals("dict")) {
            String defaultdoc;
            System.out.print("PDF Portfolio, default document: ");

            /*
             * Check the default document (may be different from container PDF);
             * we currently don't check whether this name is actually present in
             * the list of embedded files.
             */
            if (p.pcos_get_string(doc, "type:/Root/Collection/D").equals(
                "string")) {
                defaultdoc = "'" + p.pcos_get_string(doc, "/Root/Collection/D")
                    + "'";
            }
            else
                defaultdoc = "container PDF";

            System.out.println(defaultdoc);
        }

        /* Check for document file attachments */
        int filecount = (int) p.pcos_get_number(doc, "length:names/EmbeddedFiles");

        for (int file = 0; file < filecount; file++) {
            /* Check for Unicode file name */
            String filename = "";

            String objtype = p.pcos_get_string(doc, "type:names/EmbeddedFiles["
                + file + "]/UF");

            if (objtype.equals("string")) {
                filename = p.pcos_get_string(doc, "names/EmbeddedFiles["
                    + file + "]/UF");
            }
            else {
                /* Check for ASCII/native encoded file name */
                objtype = p.pcos_get_string(doc, "type:names/EmbeddedFiles["
                    + file + "]/F");
                if (objtype.equals("string")) {
                    filename = p.pcos_get_string(doc, "names/EmbeddedFiles["
                        + file + "]/F");
                }
            }
            if (!filename.equals("")){
                byte[] attachment_contents = p.pcos_get_stream(doc, "",
                    "names/EmbeddedFiles[" + file + "]/EF/F");

                /* write attachment_contents to filename */
                OutputStream os = new FileOutputStream(filename);
                os.write(attachment_contents);
                os.close();
                System.out.println("Extracted embedded file '"
                    + filename
                    + "', length="
                    + attachment_contents.length + " bytes");
            }
        }

        System.out.println(filecount + " document(s) found in Portfolio");
        System.out.println();

    }

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

    public static void main(String argv[]) {
        unpack_portfolio example = new unpack_portfolio(argv, "Explode Portfolio",
            SEARCH_PATH);
        example.execute();
    }
}