The Font-class:
There are 14 standard fonts that should be available by default in each PDF reader.
These fonts are Courier, Courier Bold, Courier Italic (Oblique), Courier Bold and Italic,
Helvetica, Helvetica Bold, Helvetica Italic (Oblique), Helvetica Bold and Italic,
Times Roman, Times Roman Bold, Times Roman Italic, Times Roman Bold and Italic,
Symbol and ZapfDingBats® (see also the section on built-in fonts).
As you can see the different faces (normal, bold, italic and bolditalic) of the same type face (Courier, Helvetica, Times Roman)
are considered to be different fonts. This is an issue that shall recur in the sections on
getting fonts from files and font styles .
iText tries to make these differences more transparant, by asking you
for the font 'family' (Courier, Helvetica, Times Roman) and the font style (normal, bold, italic).
You can construct a Base 14 Font-object with one of the simple constructors as is done in the next example:
Go to top of the pageYou can construct a Base 14 Font-object with one of the simple constructors as is done in the next example:
fonts[0] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.NORMAL); fonts[2] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD); fonts[3] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD | Font.ITALIC); fonts[7] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLDITALIC); fonts[9] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.ITALIC);
Example: java
com.lowagie.examples.fonts.StandardType1Fonts
Sums up the 14 Standard Type 1 Fonts: see StandardType1Fonts.pdf
Sums up the 14 Standard Type 1 Fonts: see StandardType1Fonts.pdf
The FontFactory-class:
In the next example, we use the class FontFactory to create a Font,
as this is the easiest way to get a new Font-object
(unless you want to use the mechanism of Font Propagation).
If you read the chapter on getting fonts,
you will see that there are different kinds of font 'sources'. Fontfactory has an API
with some methods that can construct a Font object of any kind in a uniform way (except for some specialized CID fonts).
Go to top of the pagefonts[0] = FontFactory.getFont( FontFactory.COURIER, Font.DEFAULTSIZE, Font.NORMAL); fonts[2] = FontFactory.getFont( FontFactory.COURIER, Font.DEFAULTSIZE, Font.BOLD); fonts[3] = FontFactory.getFont( FontFactory.COURIER, Font.DEFAULTSIZE, Font.BOLD | Font.ITALIC); fonts[7] = FontFactory.getFont( FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.BOLDITALIC); fonts[9] = FontFactory.getFont( FontFactory.TIMES_ROMAN, Font.DEFAULTSIZE, Font.ITALIC);
Example: java
com.lowagie.examples.fonts.FontFactoryType1Fonts
Sums up the 14 Standard Type 1 Fonts (using the FontFactory): see FontFactoryType1Fonts.pdf
Sums up the 14 Standard Type 1 Fonts (using the FontFactory): see FontFactoryType1Fonts.pdf
The BaseFont-class:
The Font-object returned by
the FontFactory
is in many cases just a wrapper class for the more complex class
BaseFont.
You can create such a basefont directly by using one of the
BaseFont.createFont methods:
Go to top of the pageBaseFont helvetica = BaseFont.createFont( BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
- The first parameter should the name of the font or the location where it can be found.
On windows systems, most fonts are in the directory C:\WINDOWS\FONTS, so this could be a correct way to construct a basefont object for Arial:
BaseFont arial = BaseFont.createFont( "C:\\WINDOWS\\FONTS\\ARIAL.TTF", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
You can ask a font for its full font names. Take a look at the example.Example: java com.lowagie.examples.fonts.FullFontNamesWe create a font using the path to its ttf-file. Then we ask the ttf file for the fontnames. (Skip to the section on fonts from files to learn how you can register complete font-directories with one method, so that you don't have to use the path to each separate font anytime you want to create a Font object.)
Retrieving the full font name: see fullfontname_arialbi.txt - The second parameter is the encoding. A character is an abstract symbol, whereas a glyph is a specific graphical rendering of a character.
For example, the glyphs A, A and A are renderings of the abstract "A" character.
The encoding is the association between the character codes (obtained from text strings) and glyph descriptions.
The same Java character in a Java String object can be rendered in many different ways, depending on the encoding.
Basefont has some default values, but you could use any of the encodings that are available in the font you are using.
BaseFont.WINANSI = BaseFont.CP1252 Latin 1 BaseFont.CP1250 Latin 2: Eastern Europe BaseFont.CP12571257 Windows Baltic BaseFont.MACROMAN MacRomanEncoding You could easily extend the list with "1251" (Cyrillic), "1253" (Greek), "1254" (Turkish) Example: java com.lowagie.examples.fonts.ListEncodingsNote that not all fonts contain all the encodings you want. Just go looking for the font that is best suited for your needs.
Asking the font for its available encodings: see encodings.txtExample: java com.lowagie.examples.fonts.FontEncodingIn the next example, we are passing the value IDENTITY_H as encoding. BaseFont.IDENTITY_H and BaseFont.IDENTITY_V are not really encodings. They indicate that the unicode character wil be looked up in the font and stored as-is, taking two bytes of space. It's the only way to have Asian fonts and some encodings left out by Adobe such as Thai. For Europe or the Middle-East, it is better to use an available encoding that will store a single byte per character. Fonts with BaseFont.IDENTITY_H or BaseFont.IDENTITY_V will always be embedded no matter what you enter as third parameter.
Using a True Type font that will be embedded in the PDF: see fontencoding.pdfExample: java com.lowagie.examples.fonts.UnicodeExample
Using BaseFont.IDENTITY_H as 'encoding': see unicode.pdfExample: java com.lowagie.examples.fonts.EncodingFont
Using an encoding to display characters from other alphabets: see encodingfont.pdf - With the third parameter you specify if you want to embed (a subset of) a font into the PDF document.
This is necessary if you are using special fonts that aren't on the system used by the consumer of your document.
Example: java com.lowagie.examples.fonts.FontEncoding
Using a True Type font that will be embedded in the PDF: see fontencoding.pdf
to be continued...:
There more on fonts in the chapters Getting fonts (built-in, fonts from files, fonts from jars, CID fonts)
and Font characteristics (changing the style, changing the write direction,...).
Go to top of the page