PDFlib Cookbook

cookbook

fonts/font_configuration

Configure font resources and search for fonts.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Font resources:
 * Configure font resources and search for fonts
 *
 * In the easiest case, load a host font on Windows or Mac which is already
 * installed in the system. Then, use the "SearchPath" resource category to
 * define the directory with your fonts. In the next case, use a font
 * where the font file name equals the font name. If the font name and font
 * file name are not equal, use the "FontOutline" resource category to define
 * a font name to be used by PDFlib. With info_font() get the font name and 
 * the font file name.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: font file
 */
package com.pdflib.cookbook.pdflib.fonts;

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

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

        pdflib p = null;
        int font;
        String fontname;
        int x = 20, y = 260;
        int exitcode = 0;

        try {
            p = new pdflib();

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

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

            /*
             * The convenient way: load a host font on Windows or Mac which is
             * already installed in the system. In this case no prerequisites
             * are required provided that you know the exact (case-sensitive)
             * name of the font (see the PDFlib Tutorial for information about
             * how to retrieve the exact name of a host font). For example, if
             * you installed the font "Verdana" in the system you can load it
             * just as follows.
             */

            /*
             * The following will only work on Mac or Windows, provided the font
             * Verdana is installed in the system.
             * 
             * font = p.load_font("Verdana", "unicode", "");
             * if (font == -1)
             *     throw new Exception("Error: " + p.get_errmsg());
             * 
             * p.setfont(font, 10);
             * p.fit_textline("Font file is installed in the system", x, y, "");
             * fontname = p.get_string((int) p.info_font(font, "fontname", "api"));
             * p.fit_textline("Font name used in PDFlib: "
             *                                + fontname, x, y-=20, "");
             */

            /*
             * If you do not want to use a host font the location of the font
             * files must be provided. Use the "SearchPath" resource category to
             * define the folder for your fonts to be searched in. Add similar
             * "Searchpath" commands to define further folders if necessary.
             * PDFlib will first search the font in all folders defined with
             * "Searchpath".
             */
            p.set_option("searchpath={" + searchpath + "}");

            /*
             * If you know the font name and the font file name (excluding the
             * file name extension such as ".ttf", ".otf", ".pfb" etc.) is equal
             * to the font name, you can simply load the font.
             */

            /*
             * Alternatively, you can supply an absolute path name such as
             * 
             * p.set_option("FontOutline=NotoSerif-Regular=/usr/fonts/NotoS-R.ttf");
             * 
             * In this case the font is loaded from the location defined above
             * without the searchpath being applied.
             */

            /*
             * Now you can load the font using the name "NotoSerif-Regular".
             */
            font = p.load_font("NotoSerif-Regular", "unicode", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.setfont(font, 10);
            p.fit_textline("Font file name: NotoSerif-Regular.ttf", x, y -= 40, "");
            fontname = p.get_string((int) p.info_font(font, "fontname", "api"),
                "");
            p.fit_textline("Font name used in PDFlib: " + fontname, x, y -= 20,
                "");

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