PDFlib Cookbook

cookbook

fonts/character_references

Demonstrate character references.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Character references:
 * Demonstrate the use of name-based and numerical character references.
 *
 * 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 font;
        int x=20, y=220, width=300, height=300;
        int exitcode = 0;

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

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

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

            p.fit_textline("Input", x, y, "font=" + font + " fontsize=16");
            p.fit_textline("Output", x+160, y, "font=" + font +
                " 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. We set charref=false to show the character reference
             * literally.
             */

            /* Output the Euro glyph via a character reference */
            p.fit_textline("500 €", x, y-=24, "font=" + font +
                " fontsize=16 charref=false");
            p.fit_textline("500 €", x+160, y, "font=" + font +
                " fontsize=16 charref=true");

            /* Output the Euro glyph via a numerical character reference */
            p.fit_textline("500 €", x, y-=24, "font=" + font +
                " fontsize=16 charref=false");
            p.fit_textline("500 €", x+160, y, "font=" + font +
                " fontsize=16 charref=true");
            
            /* Output the zcaron glyph via a glyph name reference */
            p.fit_textline("&.zcaron;", x, y-=24, "font=" + font +
                " fontsize=16 charref=false");
            p.fit_textline("&.zcaron;", x+160, y, "font=" + font +
                " fontsize=16 charref=true");
            
            /* Output the zcaron glyph via a numerical character reference */
            p.fit_textline("ž", x, y-=24, "font=" + font +
                " fontsize=16 charref=false");
            p.fit_textline("ž", x+160, y, "font=" + font +
                " fontsize=16 charref=true");            

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