PDFlib

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

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

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

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

レイヤーバリアントと透過を持つ PDF/X-4 標準の文書を作成します。

下位レベルレイヤーがイメージレイヤーと同様にそれぞれの言語のために作成されます。

それぞれの言語レイヤーは PDF/X-4 として「layer variant」イメージレイヤーフォームを伴います。(Acrobat では、コンフィギュレーションと呼ばれています)

これは、レイヤーバリアントが一致する場合にのみ、下位レベルレイヤーが不可視の有効無効の設定ができないことを意味します。

イメージレイヤーが提供されることなしに言語レイヤーの印刷が実行されることを防ぎます。

この文書は、以前の PDF/X 標準では許されなかった PDF/X-4 で備わった、透過テキストを含んでいます。

必要な製品:PDFlib または PDFlib+PDI または PPS


/*
 * PDF/X-4 starter:
 * Create PDF/X-4 conforming output with layers and transparency
 *
 * A low-level layer is created for each of several languages, as well
 * as an image layer. 
 *
 * The document contains transparent text which is allowed in
 * PDF/X-4, but not earlier PDF/X standards.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: font file, image file, ICC output intent profile
 *                (see www.pdflib.com for output intent ICC profiles)
 */

package com.pdflib.cookbook.pdflib.pdfx;

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

class starter_pdfx4 {
    public static void main(String argv[]) {
        /* This is where the data files are. Adjust as necessary. */
        final String searchpath = "../input";

        pdflib p = null;
        final String imagefile = "zebra.tif";
        String optlist;

        int font, image, icc, gstate;
        int layer_english, layer_german, layer_french, layer_image;
        double width, height;
        int exitcode = 0;

        try {
            p = new pdflib();

            /*
             * Set errorpoliy to return, this means we must check return
             * values of load_font() etc.
             * Set the search path for fonts and images etc.
             */
            p.set_option("errorpolicy=return SearchPath={" + searchpath + "}");

            if (p.begin_document("starter_pdfx4.pdf", "pdfx=PDF/X-4") == -1) {
                throw new Exception("Error: " + p.get_errmsg());
            }

            p.set_info("Creator", "PDFlib starter sample");
            p.set_info("Title", "starter_pdfx4");

            if (p.load_iccprofile("ISOcoated_v2_eci.icc", "usage=outputintent")
                    == -1) {
                System.err.println("Error: " + p.get_errmsg() );
                System.err.println("See www.pdflib.com for output intent ICC profiles.");
                p.delete();
                System.exit(2);
            }

            /*
             * Define the layers. "defaultstate" specifies whether or not
             * the layer is visible when the page is opened.
             */

            layer_english = p.define_layer("English text", "defaultstate=true");
            layer_german = p.define_layer("German text", "defaultstate=false");
            layer_french = p.define_layer("French text", "defaultstate=false");

        /* Define a radio button relationship for the language layers.
         */
        optlist = "group={" + layer_english + " " + layer_german + " "
            + layer_french + "}";
        p.set_layer_dependency("Radiobtn", optlist);

            layer_image = p.define_layer("Images", "defaultstate=true");
            
        p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            /* Font embedding is required for PDF/X */
            font = p.load_font("NotoSerif-Regular", "unicode", "embedding");

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

            p.setfont(font, 24);

            p.begin_layer(layer_english);

            p.fit_textline("PDF/X-4 starter sample with layers", 50, 700, "");

            p.begin_layer(layer_german);
            p.fit_textline("PDF/X-4 Starter-Beispiel mit Ebenen", 50, 700, "");

            p.begin_layer(layer_french);
            p.fit_textline("PDF/X-4 Starter exemple avec des calques", 50, 700,
                    "");

            p.begin_layer(layer_image);

            p.setfont(font, 48);

            /* The RGB image needs an ICC profile; we use sRGB. */
            icc = p.load_iccprofile("sRGB", "");
            optlist = "iccprofile=" + icc;
            image = p.load_image("auto", imagefile, optlist);

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

            /* Place a diagonal stamp across the image area */
            width = p.info_image(image, "width", "");
            height = p.info_image(image, "height", "");

            optlist = "boxsize={" + width + " " + height + "} stamp=ll2ur";
            p.fit_textline("Zebra", 0, 0, optlist);

            /* Set transparency in the graphics state */
            gstate = p.create_gstate("opacityfill=0.5");
            p.set_gstate(gstate);

            /* Place the image on the page and close it */
            p.fit_image(image, (double) 0.0, (double) 0.0, "");
            p.close_image(image);

            /* Close all layers */
            p.end_layer();

            p.end_page_ext("");

            p.end_document("");

        }
        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);
        }
    }
}
(Apr 3, 2007 - Oct 25, 2022)