PDFlib

高度なPDFアプリケーションの開発を支援する定番プログラムライブラリー Supported by インフォテック株式会社

PDFlib pCOS サンプル集(クックブック)

本サンプルプログラムは、PDF の情報を取得する pCOS インターフェースの基本的な機能を実際のプログラムで紹介したものです。

本サイトでダウンロードした PDFlib 製品は、一部機能の制限を除き、評価版として無償でお使いいただけます。

pCOS インターフェースで、XFA(XML Form Architecture)データを抽出するサンプルプログラムです。


import java.io.IOException;

import com.pdflib.pCOS;
import com.pdflib.pCOSException;

/**
 * Extract XFA (XML Forms Architecture) data.
 * <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
 * 
 * @version $Id: xfa.java,v 1.5 2010/09/27 13:24:25 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.pcos_get_string(doc, "filename"));

        String objtype = p.pcos_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.pcos_get_stream(doc, "", KEY);
            write_xfa_data(contents);
        }
        else if (objtype.equals("array")) {
            int length = (int) p.pcos_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.pcos_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.5 $");
        example.execute();
    }
}
(Nov 10, 2010 - Feb 21, 2014)