Search code examples
javafontsitexttruetype

Embedding font in a Java program using iText


I want to embed font into my Java program and I know how to do it using standard Java libraries. However, how do I do it using iText?

I don't want anything outside of my project (or later jar file) to be tampered with if it's not absolutely necessary (I want my program to work on all Java supporting platforms).

I'm importing font using *.ttf file.


Solution

  • Define a font

    public static String[] FONT = 
        {"path/to/fonts/xxx.ttf", BaseFont.WINANSI};
    

    then use

    BaseFont bf = BaseFont.createFont(FONT[0], FONT[1], BaseFont.EMBEDDED);
    Font font = new Font(bf, 24);
    

    with the document.