PDFlib Cookbook

cookbook

fonts/character_references

Download Java Code     Switch to PHP Code     Show Output PDF

/* $Id: character_references.java,v 1.10 2013/01/15 10:11:58 stm Exp $
 * Character references:
 * Demonstrate the usefulness of character references, using a suitable font.
 *
 * Output text containing a character with the glyph name "g.alt". Output the
 * Euro glyph via a character reference by name or by numerical value.
 *
 * 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 character_references
{
public static void main (String argv[])
    {
    /* This is where the data files are. Adjust as necessary. */
    String searchpath = "../input";
    String outfile = "character_references.pdf";
    String title = "Character References";

    pdflib p = null;
    int gentiumfont, courierfont;
    int x=20, y=220, width=300, height=300;

    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.10 $");

        /* Load the font "Gentium-Italic" with unicode encoding
         * (see http://scripts.sil.org/gentium)
         */
        gentiumfont = p.load_font("GenI102", "unicode", "");
        if (gentiumfont == -1)
            throw new Exception("Error: " + p.get_errmsg());

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


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

        p.fit_textline("Input", x, y, "font=" + courierfont + " fontsize=16");
        p.fit_textline("Output", x+160, y, "font=" + gentiumfont +
            " fontsize=20");


        /* Select a glyph via a character reference which refers to the glyph
         * name. Use the "charref" option to enable the character referencing
         * feature.
         */

        /* First for comparison purposes output the text with the usual glyph "g" */
        p.fit_textline("Wing", x, y-=30, "font=" + courierfont +
            " fontsize=16");
        p.fit_textline("Wing", x+160, y, "font=" + gentiumfont +
            " fontsize=20 charref");

        /* With the "Gentium-Italic" font, output the text "Wing" containing
         * a character with the glyph name "g.alt". (Such a glyph name is used
         * to choose among multiple glyphs in the font which have the same
         * Unicode value and therefore cannot uniquely be addressed via Unicode
         * values.) "g.alt" is addressed via a character reference by name
         * "&.g.alt;"
         */
        p.fit_textline("Win&.g.alt;", x, y-=23, "font=" + courierfont +
            " fontsize=16");
        p.fit_textline("Win&.g.alt;", x+160, y, "font=" + gentiumfont +
            " fontsize=20 charref");

        /* Output the Euro glyph via a character reference by name */
        p.fit_textline("500 €", x, y-=23, "font=" + courierfont +
            " fontsize=16");
        p.fit_textline("500 €", x+160, y, "font=" + gentiumfont +
            " fontsize=20 charref");

        /* Output the Euro glyph via a character reference by numerical value */
        p.fit_textline("500 €", x, y-=23, "font=" + courierfont +
            " fontsize=16");
        p.fit_textline("500 €", x+160, y, "font=" + gentiumfont +
            " fontsize=20 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();
            }
        }
    }
}