concept font in category itext

appears as: fonts, font, fonts, font, The fonts
iText in Action, Second Edition

This is an excerpt from Manning's book iText in Action, Second Edition.

If you look at listing 1.1, you see that you can also expect an IOException. Once you start using resources such as images, fonts, or existing PDFs, this exception can occur if something goes wrong while reading from an InputStream.

A TrueType collection is, as the name indicates, a collection of TrueType fonts bundled in one .ttc file. Figure 11.3 shows the fonts available in the file msgothic.ttc.

Figure 11.3. TrueType collection example

In the next example, you’ll use the enumerateTTCNames() method to find the names of the fonts inside the collection.

Listing 11.2. TTCExample.java

The FONT parameter contains the path to msgothic.ttc. Observe that you have to add the index of the font to the font file when you want to create a BaseFont object. For instance, if you want to use the font MS-PGothic, you need to add the index 1:

BaseFont bf = BaseFont.createFont("c:/windows/fonts/msgotic.ttc,1",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

When you ask iText to embed a TrueType or an OpenType font with TrueType outlines, iText will not embed the complete font, as is the case with Type 1 fonts or OpenType fonts with Type 1 outlines. Instead, it will only embed a subset of the font, containing only those glyphs that were used in the document. iText will ignore the embedded parameter and always embed a subset of the font if you use the encoding IDENTITY_H or IDENTITY_V.

If you open a file using these CJK fonts in Adobe Reader, and if the fonts aren’t available, a dialog box will open. You’ll be asked if you want to update the Reader. If you agree, the necessary font packs will be downloaded and installed. You’ll find the font files in the folder where Adobe Reader was installed; for example, C:/Program Files/Adobe/Reader 9.0/ Resource/CIDFont/. These fonts are only licensed for use in combination with Adobe Reader; you’re not allowed to use them in any other application (unless you’ve bought a license from Adobe).

This code registers the font Garamond Bold and gives it the alias my_bold_font. From now on, you can use this custom name to get the font from the factory.

The alias parameter is optional. You can also use one of the names that is stored in the font to retrieve a Font object. This bit of code shows how to read not only the PostScript name, but also the full font names in different languages.

One of the fonts in the list that is produced has a very cryptic name: cmr10. You used this font in section 11.1.2, and you know that the real font name is Computer Modern Regular. If you want to use the full name as an alias, you can change the PostScript name of the BaseFont like this:

Font cmr10 = FontFactory.getFont("cmr10");
cmr10.getBaseFont().setPostscriptFontName("Computer Modern Regular");
Font computerModern = FontFactory.getFont(
"Computer Modern Regular", "", BaseFont.EMBEDDED);
document.add(new Paragraph("Computer Modern", computerModern));

This is an interesting way to get fonts that are shipped with an application, but what you really want is to register all the system fonts, be it on Windows, Mac, or Linux.

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest