PDFlib

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

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

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

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

フォールバックフォント

PDFlib で、フォールバックフォントを利用するためのサンプルプログラムです。

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


/*
 * Starter sample for fallback fonts
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: suitable fonts
 */

package com.pdflib.cookbook.pdflib.fonts;

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

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

        String optlist;
        pdflib p = null;
        int i, table;
        final double llx = 30, lly = 50, urx = 800, ury = 570;
        String result;
        int exitcode = 0;

        final String headers[] = {
        	"Use case",
            "Option list for the 'fallbackfonts' option",
            "Base font",
            "With fallback font" };

        class testcase {
            testcase(String usecase, String fontname,
                    String fallbackoptions, String text) {
                this.usecase = usecase;
                this.fontname = fontname;
                this.fallbackoptions = fallbackoptions;
                this.text = text;
            }

            String usecase;
            String fontname;
            String fallbackoptions;
            String text;
        }

        final testcase testcases[] = {           
            new testcase(
                "Add Emoji to text font",
                "NotoSerif-Regular",
                // Due to "colormode=ignoremono" only color glyphs are processed.
                "{fontname=EmojiOneColor colormode=ignoremono}",
                // U+1F602 = Face with Tears of Joy
                // Use PDFlib syntax for numerical glyph references since
                // Java doesn't support U+XXXXX syntax for characters beyond U+FFFF
                "Emoji: 😂"),

            new testcase(
                "Add enlarged pictogram", "NotoSerif-Regular",
                // U+261E = WHITE RIGHT POINTING INDEX
                "{fontname=ZapfDingbats forcechars=U+261E fontsize=150% textrise=-15%}",
                "hand symbol: \u261E"),

            new testcase(
                "Add enlarged symbol glyph",
                "NotoSerif-Regular",
                // U+2663 = Black Club Suit
                "{fontname=Symbol forcechars=U+2663 fontsize=125%}",
                "club symbol: \u2663"),
                    
            new testcase(
                "Add Hebrew character to Latin font", "NotoSerif-Regular",
                "{fontname=NotoSerifHebrew-Regular}",
                "Hebrew: \u05D0")
        };

        try {
            p = new pdflib();

            p.set_option("searchpath={" + searchpath + "}");
            p.set_option("charref=true");
            p.set_option("glyphcheck=replace");

            /*
             * This means that formatting and other errors will raise an
             * exception. This simplifies our sample code, but is not
             * recommended for production code.
             */
            p.set_option("errorpolicy=exception");

            /* Set an output path according to the name of the topic */
            if (p.begin_document(outfile, "") == -1) {
                throw new Exception("Error: " + p.get_errmsg());
            }
            p.set_info("Creator", "PDFlib starter sample");
            p.set_info("Title", "starter_fallback");

            /* Start Page */
            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");

            table = -1;

            /* Table header */
            for (i = 0; i < headers.length; i++) {
                final int col = i + 1;

                optlist = "fittextline={fontname=NotoSerif-Bold fontsize=10} "
                        + "margin=4";
                table = p.add_table_cell(table, col, 1, headers[i], optlist);
            }

            /* Create fallback samples, one use case per row */
            for (i = 0; i < testcases.length; i++) {
                final int row = i + 2;
                final testcase testcase = testcases[i];
                int col = 1;

                /* Column 1: description of the use case */
                optlist = "fittextline={fontname=NotoSerif-Regular fontsize=10} "
                        + "margin=4";
                table = p.add_table_cell(table, col++, row, testcase.usecase,
                        optlist);

                /* Column 2: reproduce option list literally */
                optlist = "fittextline={fontname=NotoSerif-Regular fontsize=10} "
                        + "margin=4";
                table = p.add_table_cell(table, col++, row,
                        testcase.fallbackoptions, optlist);

                /* Column 3: text with base font */
                optlist = "fittextline={fontname=" + testcase.fontname
                        + " fontsize=10 replacementchar=? } margin=4";
                table = p.add_table_cell(table, col++, row, testcase.text,
                        optlist);

                /* Column 4: text with base font and fallback fonts */
                optlist = "fittextline={fontname=" + testcase.fontname
                        + " fontsize=10 fallbackfonts={"
                        + testcase.fallbackoptions + "}} margin=4";
                table = p.add_table_cell(table, col++, row, testcase.text,
                        optlist);
            }

            /* Place the table */
            optlist = "header=1 fill={{area=rowodd "
                    + "fillcolor={gray 0.9}}} stroke={{line=other}} ";
            result = p.fit_table(table, llx, lly, urx, ury, optlist);

            if (result.equals("_error")) {
                throw new Exception("Couldn't place table: " + p.get_errmsg());
            }

            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 - Jun 24, 2024)