PDFlib
KAUFEN
PDFlib

interactive/xfa

Extract XFA (XML Forms Architecture) data.

Download Java Code     Show Output     Show Input PDF

package com.pdflib.cookbook.pcos.interactive;


import java.io.IOException;


import com.pdflib.pCOS;

import com.pdflib.pCOSException;

import com.pdflib.cookbook.pcos.pcos_cookbook_example;


/**

 * Extract XFA (XML Forms Architecture) data.

 * <p>

 * Required software: pCOS interface 3 (pCOS 2.x, PDFlib 7.x, TET 2.2,

 * PLOP 3.x)<br>

 * Required data: PDF document

 *

 * @version $Id: xfa.java,v 1.2 2007/09/19 08:45:30 stm Exp $

 */

public class xfa 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/AcroForm/XFA";


    public void example_code(pCOS p, String filename) throws pCOSException,

        Exception {

       

        /* Open the PDF document */

        int doc = p.open_document(filename, "");

        if (doc == -1)

            throw new Exception("Error: " + p.get_errmsg());


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

       

        String objtype = p.get_string(doc, "type:" + KEY);

       

        if (objtype.equals("null")) {

            System.out.println("No XFA data found");

        }

        else if (objtype.equals("stream")) {

            System.out.println("XFA data is contained in a single stream");

           

            byte[] contents = p.get_stream(doc, "", KEY);

            write_xfa_data(contents);

        }

        else if (objtype.equals("array")) {

            int length = (int) p.get_number(doc, "length:" + KEY);

           

            System.out.println("XFA data is contained in "

                + (length / 2) + " separate streams");

           

            /*

             * every second entry in the array contains a portion of the

             * XFA data

             */

            for (int i = 1; i < length; i += 2) {

                byte[] contents = p.get_stream(doc, "", KEY + "[" + i + "]");

                write_xfa_data(contents);

            }

        }

       

        p.close_document(doc);

    }


    private void write_xfa_data(byte[] contents) {

        if (contents != null && contents.length > 0) {

            try {

                System.out.write(contents);

            }

            catch (IOException e) {

                System.err.println("IOException occurred:");

                System.err.println(e.getMessage());

            }

        }

    }


    public xfa(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) {

        xfa example = new xfa(argv, "XFA", SEARCH_PATH,

            "$RCSfile: xfa.java,v $", "$Revision: 1.2 $");

        example.execute();

    }

}