PDFlib

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

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

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

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

Type 3 ベクターロゴ

PDFlib で、PDF ページをベースにしたベクターから簡単なロゴを含む Type 3 フォントを作成するサンプルプログラムです。

一つのグリフを含む Type 3 フォントを作成するために PDF ファイルからベクターデータをインポートします。カスタムエンコーディングでグリフを追加し、このグリフでテキストを出力します。


/*
 * $Id: type3_vectorlogo.java,v 1.9 2012/12/10 12:27:37 stm Exp $
 * 
 * Type 3 vector logo font:
 * Create a Type 3 font which contains a single logo derived from a vector
 * based PDF page
 *
 * Import vector data from a PDF file to create a Type 3 logo font containing
 * one glyph. Output text with that glyph.
 *
 * Required software: PDFlib+PDI/PPS 9
 * Required data: PDF file
 */
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class type3_vectorlogo {
    public static void main(String argv[]) {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "type3_vectorlogo.pdf";
        String title = "Type 3 Vector Logo Font";

        pdflib p = null;
        String logofile = "kraxi_logo.pdf";
        int normalfont, logofont, indoc, page;

        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_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title + " $Revision: 1.9 $");

            /*
             * Import vector data from "kraxi_logo.pdf" to create the "LogoFont"
             * Type 3 font with one glyph "kraxi". Output text containing
             * the glyph by addressing via character reference value by glyph
             * name and via PUA value (Unicode Private Use Area).
             */

            /* Load vector data from PDF file */
            indoc = p.open_pdi_document(logofile, "");
            if (indoc == -1)
                throw new Exception("Error: " + p.get_errmsg());

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

            /*
             * Create the font LogoFont. The matrix entries are chosen to create
             * the common 1000x1000 coordinate system. These numbers are also
             * used when placing the logo within the glyph box below (option
             * "boxsize").
             */
            p.begin_font("LogoFont", 0.001, 0.0, 0.0, 0.001, 0.0, 0.0,
                "colorized");

            /*
             * The .notdef (fallback) glyph should be contained in all Type 3
             * fonts to avoid problems with some PDF viewers. It is usually
             * empty.
             */
            p.begin_glyph_ext(0x0000, "width=1000");
            p.end_glyph();

            /*
             * Add a glyph with the name "kraxi" and width 1000. Colorized fonts
             * do not need any bounding box, so we supply the values 0, 0, 0, 0.
             * With colorized=false (the default) we could use 0, 0, 1000, 1000
             * for the common 1000x1000 coordinate system.
             * 
             * As there is no meaningful Unicode value for the logo, we let
             * PDFlib assign a PUA value. The glyph can be addressed via its
             * glyph name, or alternatively the Unicode PUA value can be
             * retrieved via the info_font() API call.
             */
            p.begin_glyph_ext(-1, "width=1000 glyphname=kraxi");

            /*
             * Fit the contents of the PDF in a box similar to the dimensions of
             * the glyph box. We place the glyph at (0, 100) in order to
             * slightly move up the logo so that it better matches standard
             * text.
             */
            p.fit_pdi_page(page, 0, 100, "boxsize={1000 1000} fitmethod=meet");
            p.end_glyph();

            p.end_font();

            p.close_pdi_page(page);
            p.close_pdi_document(indoc);

            /* Load the new "LogoFont" font with encoding "unicode" */
            logofont = p.load_font("LogoFont", "unicode", "");
            if (logofont == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Load the "Helvetica" font */
            normalfont = p.load_font("Helvetica", "unicode", "");
            if (normalfont == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Start page */
            p.begin_page_ext(0, 0, "width=200 height=100");

            /* Print the glyph "kraxi" of the "LogoFont" font */
            p.fit_textline("&.kraxi;", 10, 50, "font=" + logofont
                            + " fontsize=14 charref");

            /* Print standard text */
            p.fit_textline("This is the kraxi logo.", 30, 50, "font="
                            + normalfont + " fontsize=14");

            /*
             * Alternatively, fetch the PUA value, and address the glyph with
             * the PUA value.
             */
            int kraxi_pua_value = (int) p.info_font(logofont, "unicode",
                                                        "glyphname=kraxi");
            String kraxi_dec_charref = "&#" + kraxi_pua_value + ";";
            p.fit_textline(kraxi_dec_charref, 170, 50, "font=" + logofont
                + " fontsize=14 charref");

            /* Finish 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)