Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new fonts to Itext using java

Tags:

java

fonts

itext

when I want to use a font is iText I do the following:

protected final static Font FONT_SIZE_11_BOLD = new Font(Font.HELVETICA, 11f, Font.BOLD);

and then I can use it whereever I want, as follows:

monthSize11 = new Chunk(month, FONT_SIZE_11_BOLD);

I want to use Arial instead of HELVETICA, but Arial is not directly available. I mean, I cannot do

new Font(Font.ARIAL, 11f, Font.BOLD);

because Arial is not defined at the Font class, but the Arial.ttf file is at my System under C:\WINDOWS\Fonts. The question is how I can bind the Arial.ttf file to iText and how can I use it.

Many thnaks in advance.

EDIT: I would like to use own fonts. I mean, I have a file called "myCompany.ttf" where own fonts have been defined and at some places I must use. The problem is not only with Arial.

like image 727
Luixv Avatar asked Aug 03 '11 07:08

Luixv


4 Answers

BaseFont base = BaseFont.createFont("c:/windows/fonts/arial.ttf", BaseFont.WINANSI);
Font font = new Font(base, 11f, Font.BOLD);
....

Read more here.

like image 156
dacwe Avatar answered Oct 13 '22 09:10

dacwe


Load it from inside the JAR using a leading slash; otherwise, use the absolute path of your font (C:\...\fonts\Sansation_Regular.ttf). For example:

Font font = FontFactory.getFont("/fonts/Sansation_Regular.ttf",
    BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 0.8f, Font.NORMAL, BaseColor.BLACK);
BaseFont baseFont = font.getBaseFont();
  • The relative path of the font is: 'src/main/resources/fonts'
  • Using Itext 5.4.5
  • Example code
like image 32
4F2E4A2E Avatar answered Oct 13 '22 08:10

4F2E4A2E


Use BaseFont.createFont to create a new Font object.

You can pass any Type1 or TTF font. You will just have to ensure your font file is distributed alongwith. Refer BaseFont API

like image 3
a-- Avatar answered Oct 13 '22 08:10

a--


Creating custom fonts using itext is simple

I had written code for the same below

Will definitely help someone

public class CustomFontStyle {
    public static void main(String[] args) {

        // creation of the document with a certain size and certain margins
        // may want to use PageSize.LETTER instead
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        try {
            // creation of the different writers
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("CustomFontsStyle.pdf"));
            final String NEWLINE = "\n";
            document.open();
            Phrase phrase = new Phrase();

            BaseFont baseFont3 = BaseFont.createFont("Xenotron.ttf", BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
            Font font2 = new Font(baseFont3, 12);

            document.add(new Paragraph("Custom Xenotron Font: ", font2));

            phrase.add(NEWLINE);

            document.add(phrase);

            document.close();

        }
        catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
    }

}
like image 1
Mohit Singh Avatar answered Oct 13 '22 08:10

Mohit Singh