TIMES NEW ROMAN font in iText (Java version)

13,297

You have a problem.

Font.getFamily(string) only works for the "Base 14" fonts, Helvetica, Times Roman, Courier (plus their bold & italic variants), plus Symbol and ZapfDingbats. Asking it for anything but one of those fonts will return FontFamily.UNDEFINED.

Your code isn't working the way you think it does. If you happen to be ending up with Arial, that's only because it's the default font.

What you want is FontFactory.getFont(...). Before FontFactory can return a given font by name, you need to register that font's file. The easiest way to do that is to call FontFactory.registerDirectories() which will enumerate all the directories known operating systems use to store fonts and register all the fonts found there. This will take longer depending on how many fonts you have there, and how big those fonts are. A lot of CJKV fonts will take a significant amount of time to register... under 30 seconds in all likelyhood, but my beast of a system can take 10-20 seconds to do so (but I have a LOT of Big Fonts).

You can also register individual files with FontFactory.register(fontPath), but that requires that you know where to find them in the first place.

At any rate, your code could read something like:

FontFactory.registerDirectories();

Font fuente = FontFactory.getFont("Times New Roman");

And FontFactory does indeed ignore case when looking up fonts. Font.getFamily() does not.

Share:
13,297
Lucas
Author by

Lucas

I have an imaginary friend named Reinaldo.

Updated on June 04, 2022

Comments

  • Lucas
    Lucas almost 2 years

    I need to do something like this in iText:

    Font fuente=new Font(Font.getFamily("ARIAL"),30,Font.BOLD);
    

    But instead using ARIAL font, I need to use the TIMES NEW ROMAN font over a Paragraph. How could I accomplish this?

    Thanks.