|
iTextSharp
Tutorial
|
|
iTextSharp, a Free C#-PDF library
|
| [Home] |
[Previous] |
[TOC] |
[Next] |
[PDF] |
Part III: Advanced iText
Chapter 9: Fonts
This chapter deals with different types of
fonts. If you only need simple Type 1 fonts (Courier, Helvetica, Times
Roman, Symbol, ZapfDingbats), please take a look at Chapter
2.
|
Specifying an encoding
In the following
example we will use a known font (Helvetica) specifying an encoding
that is widely used in Europe: 'Cp1252' (this is latin 1 extended with 27
characters; the encoding is also known as 'winansi'). You can use
BaseFont.CP1252 or BaseFont.WINANSI as parameter. Another encoding that is
supported by iText is MacRoman (BaseFont.MACROMAN).
BaseFont helvetica = BaseFont.createFont("Helvetica",
BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Font font = new Font(helvetica, 12, Font.NORMAL);
Chunk chunk = new Chunk("Sponsor this example and send me 1\u20ac.", font);
document.Add(chunk);
As you can see, the EURO symbol is added by using its UNICODE value. Check out
the result.
Remark:
If you want to retrieve the exact postscript name of a font, you can use the
method getPostscriptFontName().
|
|
True Type Fonts
Using a ttf font
In example 2, we are going to use a very
popular True Type font:
BaseFont bfComic =
BaseFont.createFont("c:\\winnt\\fonts\\comicbd.ttf", BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);
Font font = new Font(bfComic, 12);
String text1 = "This is the quite popular True Type font 'Comic'.";
document.Add(new Paragraph(text1, font));
You can find out the different possible encodings of a True Type Font by using
the property CodePagesSupported (remark: this
only works with True Type Fonts). In example 2, the possible encodings are:
-
1252 Latin 1
-
1250 Latin 2: Eastern Europe
-
1251 Cyrillic
-
1253 Greek
-
1254 Turkish
-
1257 Windows Baltic
-
Macintosh Character Set (US Roman)
You can find out the fontname of a True Type Font in different languages by
using the property FullFontName (remark: this only
works with True Type Fonts). In example 2, these are some of the results:
-
Comic Sans MS Negreta
-
Comic Sans MS Bold
-
Comic Sans MS Gras
-
Comic Sans MS Vet
-
Comic Sans MS Krepko
-
...
Embedding a ttf font
If the used TrueType font is not found on your machine, Acrobat Reader will
throw an error such as: 'a font is missing or corrupted'. This can be the case
when you try to open Chap0902.pdf. To
embed the font, you just have to set the last parameter of method
createFont to BaseFont.EMBEDDED:
BaseFont bfComic =
BaseFont.createFont("c:\\winnt\\fonts\\comic.ttf", BaseFont.WINANSI,
BaseFont.EMBEDDED);
Remark:
The Bold, Italic an BoldItalic styles don't work with embedded fonts.
BaseFont arial = BaseFont.createFont("arial.ttf",
BaseFont.WINANSI, BaseFont.EMBEDDED);
Font font = new Font(arial, 12, Font.NORMAL);
Font fontItalic = new Font(arial, 12, Font.ITALIC);
When you are using a built-in font, applying another style, changes the font.
For instance: when you are using 'Arial' and you change the style into italic,
the font is replaced internally by 'Arial Italic' (which is a totally different
font).
In this case you will have to embed both fonts: "arial.ttf" and "ariali.ttf".
BaseFont arial = BaseFont.createFont("arial.ttf",
BaseFont.WINANSI, BaseFont.EMBEDDED);
Font font = new Font(arial, 12);
BaseFont arialItalic = BaseFont.createFont("ariali.ttf", BaseFont.WINANSI,
BaseFont.EMBEDDED);
Font fontItalic = new Font(arialItalic, 12);
If you use the FontFactory, you can use fontstyles on condition that you added
all the necessary ttf's. See example
fontfactory_styles and its resulting
PDF.
|
Unicode
Greek, cyrillic, special characters can be added using unicode. As a matter of
fact all characters can be added in unicode (but this will make the resulting
PDF file larger). Please take a look at example 3:
BaseFont bfComic =
BaseFont.createFont("c:\\winnt\\fonts\\comic.ttf", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
Font font = new Font(bfComic, 12);
String text1 = "This is the quite popular True Type font 'Comic'.";
String text2 = "Some greek characters: \u0393\u0394\u03b6";
String text3 = "Some cyrillic characters: \u0418\u044f";
document.Add(new Paragraph(text1, font));
document.Add(new Paragraph(text2, font));
document.Add(new Paragraph(text3, font));
As you can see the resulting PDF file is
a lot larger: it contains all Acrobat Reader needs to know to show the TrueType
font
Remark:
BaseFont.IDENTITY_H and BaseFont.IDENTITY_V or not 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.
|
True Type Collections
Example 4 shows you how to use a True
Type Collection. This is very similar to using a True Type Font:
BaseFont bf = BaseFont.createFont("c:\\winnt\\fonts\\msgothic.ttc,1",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
A True Type collection contains more than one font. As you can see, we add a
number after the TTC-file to indicate which of the fonts we want to use. If you
want to know the names of the fonts in a certain TTC-file, you can use the
method enumerateTTCNames as shown in the example.
The file msgothic.ttc contains 3 fonts: (0) MS-Gothic, (1) MS-PGothic and (2)
MS-UIGothic. In the example, we want to use MS-PGothic.
Font widths
The width of each character in a certain font is extracted from a ttf-file, an
afm-file,... but it is possible to change the widths of each character
yourself. This is demonstrated in example 5
(Chap0905.pdf).
ttf-files and Operating Systems
As you can see, you need to create a font using the path to the ttf-file. The
above example works on a Win2K machine, but if you use another OS, you will
have to change this path. Remark: the ttf file can be placed on any OS. It
isn't Windows-specific. It has been succesfully tested on Linux and Solaris
too.
Font Factory
Working with paths to a certain file on a disk can be very annoying. If you
change from OS or deployment machine, you risk getting errors. That's why you
may want to consider using class FontFactory to create your fonts.
You still need the actual paths to the ttf- or ttc-file, but you can register
them to the FontFactory -class at startup. If a path is
illegal or not found, a Exception is thrown. Once a font is registered, it
can be used by specifying its postscript fontname. This is demonstrated in
example 6 (Chap0906.pdf).
|
Barcodes
There are two ways to generate Barcodes with
iText. One where you need one of the ttf-files (provided in barcodefonts.zip)
and one that doesn't need external ttf's (Barcodes are generated as images).
Barcodes generated as images (without a ttf)
Example 7 (Chap0907.pdf)
shows you how to add a barcode without using a ttf-file. The following types
are supported (use the corresponding classname):
-
Barcode39:
code 39 and code 39 extended
-
Barcode128:
code 128 and the special encoding UCC/EAN-128
-
BarcodeEAN:
EAN13, EAN8, UPCA, UPCE, and EAN with supplemental 5, EAN with supplemental 5,
EAN with supplemental 2
-
BarcodeInter25:
interleaved 2 of 5
-
BarcodePostnet: postnet and planet
This is an code example to generate an EAN-barcode with value 9780201615883:
PdfContentByte cb = writer.DirectContent;
BarcodeEAN codeEAN = new BarcodeEAN();
codeEAN.CodeType = BarcodeEAN.EAN13;
codeEAN.Code = "9780201615883";
Image imageEAN = codeEAN.createImageWithBarcode(cb, null, null);
document.Add(new Phrase(new Chunk(imageEAN, 0, 0)));
To change to 'look' of these barcodes, please read the API documentation.
Barcodes using a ttf-file
iText has a class called PdfBarcode that can be
used to generate 4 types of barcodes:
-
CODE 39
-
UPC-A
-
EAN-13
-
EAN-13.TTF
- EAN-13B.TTF
- EAN-13BH.TTF
- EAN-13HH.TTF
-
Interleaved 2/5
This class has to be used in combination with some specific TTF-files that can
be downloaded from
http://itextsharp.sourceforge.net/downloads/barcodefonts.zip (not all
these fonts are free; please read the txt-files first before you use any of
these fonts).
The PdfBarcode-object can be constructed like this:
Chunk chunk = new PdfBarcode("c:\\winnt\\fonts\\CODE39.TTF",
PdfBarcode.CODE39, 36, "0123456789");
Remark: you will always need to tell the system where you have put the ttf-file
and the type of Barcode you are using. It is also important to know that if you
want to use another barcode font than the ones provided, you will have to make
some changes to the class PdfBarcode!
The third and fourth parameter are the fontsize and finally the number you want
to turn into a barcode.
For UPC, EAN en I2/5, you can choose between different styles of fonts: see
example 8 (Chap0908.pdf).
|
Measuring text
Sometimes it's necessary to know the length
of a certain piece of text. If you have created a BaseFont
object, you can use the method:
public float getWidthPoint(String text, float fontSize);
So if you are using some barcode font with size 36 as in the previous example
and you want to know how much space you need on the line to print this barcode,
you just do: getWidthPoint("0123456789", 36f).
The result is the width in points. There are 72 points in 1 inch. So if you
have a result of 252 points (as in example 8),
you can convert this to inches and centimeters like this: 252 / 72 = 3.5 inch *
2.54 = 8.89 cm (see also Chapter 1).
|
Adobe fonts
If you have some .AFM and .PFB files,
describing an Adobe font, you can put them in the same directory and then use:
BaseBont bf = BaseFont.createFont("myfont.afm",
BaseFont.WINANSI, BaseFont.EMBEDDED);
But if you have the alternative, use a true type font: the font will be subset
and the resulting document will be a lot smaller.
|
| [Top] |
[Previous] |
[TOC] |
[Next] |
[PDF] |
|