PDFlib

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

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

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

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

ネストされた PDFlib ブロック

ネストされた PDFlib ブロックの処理では組み付けと個別化の両方を実現します:

  • 上位の PDFlib ブロックコンテナページは印刷された紙面上で主要な領域を占める大規模な PDF Block を複数含んでいます。PDFlib ブロックの配置には折り返しのような印刷後のことを考慮しています。
  • 上位の PDFlib ブロックは個別化のために、下位の PDF ページにテキスト、画像、様々なテキストを埋め込んだ PDFlib ブロック等を含んでいます。

/*
 * Nested_blocks:
 * Nested Block processing is used to implement both imposition and 
 * personalization with Blocks:
 *
 * - The first-level Block container page contains several large PDF Blocks
 *   which indicate the major areas on the paper to be printed on. The
 *   arrangement of PDF Blocks reflects the intended postprocessing of the
 *   paper (e.g. folding).
 * - Each of the first-level PDF Blocks is then filled with a second-level
 *   container PDF page which contains Text, Image, or PDF Blocks to be filled
 *   with variable text for personalization.
 *
 * Required software: PPS 9
 * Required data:
 * - first-level Block container with PDF Blocks named "Block_"
 * - second-level Block container with arbitrary Blocks
 * - variable data for filling second-level Blocks (provided in an inline array)
 */
package com.pdflib.cookbook.pdflib.blocks;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

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

        pdflib p = null;
        String infilename1 = "A3_imposition_2x2.pdf";
        String infilename2 = "businesscard_blocks.pdf";
        int record, block, inpage1, inpage2, indoc1, indoc2;
        int exitcode = 0;

        int pagespersheet; // number of imposed pages per sheet

        /* Names of the blocks contained in the imported page */
        String blocknames[] = { "name", "business_title",
            "business_address_line1", "business_address_city",
            "business_telephone_voice", "business_telephone_fax",
            "business_email", "business_homepage" };

        int nblocks = blocknames.length; // number of blocks to be filled

        /* Data related to various persons used for personalization */
        String persons[][] = {
            { "Victor Kraxi", "Chief Paper Officer", "17, Aviation Road",
                "Paperfield", "7079-4301", "7079-4391", "victor@kraxi.com",
                "www.kraxi.com" },
            { "Paula Kraxi", "Chief Paper Pilot", "17, Aviation Road",
                "Paperfield", "7079-4302", "7079-4392", "paula@kraxi.com",
                "www.kraxi.com" },
            { "Serge Kraxi", "Chief Paper Folder", "17, Aviation Road",
                "Paperfield", "7079-4303", "7079-4393", "serge@kraxi.com",
                "www.kraxi.com" },
            { "Lena Kraxi", "Chief Financial Officer", "17, Aviation Road",
                "Paperfield", "7079-4304", "7079-4394", "lena@kraxi.com",
                "www.kraxi.com" },
            { "Dana Kraxi", "Auxiliary Paper Folder", "17, Aviation Road",
                "Paperfield", "7079-4305", "7079-4395", "dana@kraxi.com",
                "www.kraxi.com" },
            { "Anna Kraxi", "Accounting Assistant", "17, Aviation Road",
                "Paperfield", "7079-4306", "7079-4396", "anna@kraxi.com",
                "www.kraxi.com" } };

        int recordcount = persons.length; // number of personalization records

        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");

            if (p.begin_document(outfile, "") == -1)
                throw new Exception("Error: " + p.get_apiname() + ": "
                        + p.get_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title);

            /* Open the first-level Block container with imposition Blocks */
            indoc1 = p.open_pdi_document(infilename1, "");
            if (indoc1 == -1)
                throw new Exception("Error: " + p.get_apiname() + ": "
                        + p.get_errmsg());

            /* Open the first page and prepare the page boxes for cloning */
            inpage1 = p.open_pdi_page(indoc1, 1, "cloneboxes");
            if (inpage1 == -1)
                throw new Exception("Error: " + p.get_apiname() + ": "
                        + p.get_errmsg());

            /* Query the number of Blocks to keep the code flexible */
            pagespersheet = (int) p.pcos_get_number(indoc1,
                    "length:pages[0]/blocks");

            /*
             * Open the second-level Block container with personalization Blocks
             */
            indoc2 = p.open_pdi_document(infilename2, "");
            if (indoc1 == -1)
                throw new Exception("Error: " + p.get_apiname() + ": "
                        + p.get_errmsg());

            /* Open the first page */
            inpage2 = p.open_pdi_page(indoc2, 1, "");
            if (inpage2 == -1)
                throw new Exception("Error: " + p.get_apiname() + ": "
                        + p.get_errmsg());

            for (record = 0; record < recordcount; record++) {
                /* Start a new output page if required */
                if (record % pagespersheet == 0) {
                    /* The page size will be adjusted with "cloneboxes" */
                    p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

                    /*
                     * Place the first-level Block container page on the output
                     * page
                     */
                    p.fit_pdi_page(inpage1, 0, 0, "cloneboxes");
                }

                /*
                 * Imposition: fill first-level PDF Blocks with second-level
                 * container page
                 */
                if (p.fill_pdfblock(inpage1, "Block_" + record % pagespersheet,
                        inpage2, "fitmethod=meet") == -1) {
                    System.err.println("Warning: " + p.get_errmsg());
                    continue;
                }

                /* 
                 * Personalization: fill second-level Blocks with variable data
                 */
                for (block = 0; block < nblocks; block++) {

                    if (p.fill_textblock(inpage2, blocknames[block],
                            persons[record][block],
                            "encoding=unicode embedding") == -1) {
                        System.err.println("Warning: " + p.get_errmsg());
                    }
                }

                /*
                 * Finish the page if no more space, or no more records
                 * available
                 */
                if (record % pagespersheet == pagespersheet - 1
                        || record == recordcount - 1) {
                    p.end_page_ext("");
                }
            }

            p.close_pdi_page(inpage1);
            p.close_pdi_page(inpage2);

            p.end_document("");
            p.close_pdi_document(indoc1);
            p.close_pdi_document(indoc2);
        }
        catch (PDFlibException e) {
            System.err.println("PDFlib exception occurred:");
            System.err.println("[" + e.get_errnum() + "] " + e.get_apiname() +
                ": " + e.get_errmsg());
        exitcode = 1;
        }
        catch (Exception e) {
            System.err.println(e);
        exitcode = 1;
        }
        finally {
            if (p != null) {
                p.delete();
            }
        System.exit(exitcode);
        }
    }
}
(Dec 11, 2012 - May 23, 2019)