1

I am generating PDF documents with iText. When I try to include the "Agenda Tabular Light" font, iText ignores my choice of font. I confirmed that the font isn't set using Adobe Reader's File > Properties > Fonts. The PDF shows that Helvetica was used, but I didn't choose Helvetica. The colors are visible, but not the font.

My code looks like this:

public static final Font FONT_HEADER = FontFactory.getFont(AGENDA_TABULAR_LIGHT, 18, Font.NORMAL, TITLE_COLOR);

I even I tried a sample program.

// step 1
    Document document = new Document();
    // step 2
    PdfWriter.getInstance(document, new FileOutputStream(filename));
    // step 3
    document.open();
    // step 4:
    Font font = FontFactory.getFont("Agenda Tabular Light");

    System.out.println(font.toString());

    document.add(new Phrase("Agenda Tabular Light J j", font));
    Font fontbold = FontFactory.getFont("Times-Roman", 12, Font.BOLD);
    document.add(new Phrase("Times-Roman, Bold", fontbold));
    document.add(Chunk.NEWLINE);

    document.close();

It displays like Times-Roman and uses a different font for agenda. But whenever I see fonts tab on properties of Adobe Reader, it displays Helvetica.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
mabimal
  • 199
  • 1
  • 1
  • 7
  • It's hard to believe that your business would need you to use an obsolete, unsupported version of iText (dating from July 2009). Please upgrade to a more recent version to avoid the legal and technical issues that exist in iText 2.1.7 and we'll take another look at your question. – Bruno Lowagie Sep 17 '15 at 07:20
  • It's in the production and upgrading it to latest version is not what I decide. But I am in need of setting fonts, everything gets setup, like the size, color but not particularly the font. Is there a solution you can provide? – mabimal Sep 17 '15 at 13:42
  • Yes, as the documentation clearly indicates, you can't use font aliases such as `"Agenda Tabular Light"` if you don't register a font with that alias first. Who decides on the upgrading process? Please let us know so that we can avoid that customers get to use a problematic version of iText. – Bruno Lowagie Sep 17 '15 at 13:46
  • Thanks Bruno, I will register it first then. And about upgrading, I should talk to my manager first. After that I can let you know. – mabimal Sep 17 '15 at 13:49

1 Answers1

1

As explained in the comments, this usually doesn't work:

Font font = FontFactory.getFont("garamond bold");

By default only the standard Type 1 fonts are known to the FontFactory and "garamond bold" isn't one of those fonts, hence Helvetica will be used instead (Helvetica is the default font in iText).

You can "teach" the FontFactory where to find other fonts by registering them.

You could try:

FontFactory.registerDirectories();

But that is a very expensive operation as you ask iText to search your operating system for font files in different font directories (e.g. C:/Windows/Fonts). This can take a few seconds and you'll end up with much more fonts than you need (and maybe the font you need won't be registered anyway).

The best way is to register the fonts you need like this:

FontFactory.register("c:/windows/fonts/garabd.ttf", "garamond bold");

We tell iText where to find the ttf file ("c:/windows/fonts/garabd.ttf") and we define an alias for that font ("garamond bold"). Now that the font name is registerd, we can start using it:

Font myBoldFont = FontFactory.getFont("garamond bold");
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165