PDFlib

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

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

本サンプルプログラムは、PDF 文書生成ライブラリーの実装である PDFlib の基本的な機能を実際のプログラムで紹介したものです。

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

PDF/X のインポート

PDF/X-3 文書をインポートして、PDF/X-3 として出力します。

文書からインポートされたページのみがマージされます(インタラクティブな要素は除かれます)。


/* $Id: import_pdfx.java,v 1.6 2013/02/05 10:13:16 stm Exp $
 * Import PDF/X:
 * Import an existing PDF/X-4 document and output it as PDF/X-4
 * 
 * Only the pages from the imported documents are merged; interactive elements
 * will be ignored.
 *
 * Required software: PDFlib+PDI/PPS 9
 * Required data: PDF document
 */
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class import_pdfx
{
    public static void main (String argv[])
    {
    /* This is where the data files are. Adjust as necessary. */
    String searchpath = "../input";
    String outfile = "import_pdfx.pdf";
    String title = "Import PDF/X";

    pdflib p = null;
    String pdffile = "PLOP-datasheet-PDFX-4.pdf";
    int indoc, pageno, endpage, page;
    double ret;
    String res;

    try {
        p = new pdflib();

        p.set_option("searchpath={" + searchpath + "}");

        /* This means we must check return values of load_font() etc. */
        p.set_option("errorpolicy=return");
        
        /* Set the desired PDF/X conformance level */
        if (p.begin_document(outfile, "pdfx=PDF/X-4") == -1)
            throw new Exception("Error: " + p.get_errmsg());

        p.set_info("Creator", "PDFlib Cookbook");
        p.set_info("Title", title + " $Revision: 1.6 $");
        
        /* Open the input PDF */
        indoc = p.open_pdi_document(pdffile, "");
        if (indoc == -1)
            throw new Exception("Error: " + p.get_errmsg());

        endpage = (int) p.pcos_get_number(indoc, "length:pages");
        
        /* Since the input document contains its own output intent retrieve
         * the output intent from the input document and copy it to the output
         * document
         */
        res = p.pcos_get_string(indoc, "type:/Root/OutputIntents");
        if (res.equals("array")) {
            ret = p.process_pdi(indoc, -1, "action=copyoutputintent");
            if (ret == -1)
                throw new Exception("Error: " + p.get_errmsg());
        }
        else {
            throw new Exception("Error: " +
                "Invalid PDF/X (output intent is not an array)"); 
        }

        /* Loop over all pages of the input document */
        for (pageno = 1; pageno <= endpage; pageno++)
        {
            page = p.open_pdi_page(indoc, pageno, "");

            if (page == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Dummy page size; will be adjusted later */
            p.begin_page_ext(10, 10, "");

            /* Place the imported page without performing any changes on the
             * output page
             */
            p.fit_pdi_page(page, 0, 0, "adjustpage");
            
            p.close_pdi_page(page);
            p.end_page_ext("");
        }

        p.end_document("");

        } catch (PDFlibException e) {
            System.err.print("PDFlib exception occurred:\n");
            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +
                ": " + e.get_errmsg() + "\n");
        } catch (Exception e) {
            System.err.println(e.getMessage());
        } finally {
            if (p != null) {
                p.delete();
            }
        }
    }
}
(Apr 3, 2007 - Feb 20, 2014)