Registering fonts
In the style
attribute of the body
tag of thoreau.html, we've told the HTML renderer that we prefer the font Nimbus Roman No9 L (a font you can usually find on Linux distributions).
If that font can't be found, we want the font to be Times New Roman (a font that is usually distributed with Windows).
If that font isn't found, the default font can be used (which is what happened in thoreau0.pdf).
XML Worker uses the FontFactory
class to retrieve fonts. Initially, this class is only aware of the standard Type 1 fonts.
If we want iText to find Nimbus Roman No9 L or Times New Roman, we need to register these fonts (see section 11.4.1 of iText in Action Second Edition).
In this example, we just register the "usual suspects":
FontFactory.registerDirectories();
On Windows, this method will find the font files times.ttf, timesbd.ttf, timesbi.ttf, and timesi.ttf. iText will use fonts these to render all the text in the HTML. On Linux, iText will use the Type1 font stored in n021004l.afm/n021004l.pfb and use it whenever a regular font is needed. Unfortunately, it will be more difficult to find the corresponding bold, italic and bold italic font. Choose your font wisely if you want to avoid this problem.