As Alexis wrote in his comment, you really shouldn't use iText 2.1.2 anymore. The solution that works for more recent versions of iText may work, but this doesn't solve all problems related to old iText versions.
This being said, you could solve this problem in recent iText versions, but using a FontMapper.
Suppose that dir
is a directory where you have stored the font programs of the fonts you want to use (.ttf
-files, .otf
-files,...). In that case you could use the DefaultFontMapper
like this:
DefaultFontMapper mapper = new DefaultFontMapper();
mapper.insertDirectory(dir);
Graphics2D g2 = new PdfGraphics2D(canvas, 600, 60, mapper);
If you read chapter 14 of my book, you'll notice that you can hit a couple of problems.
- The name of the font needs to match,
- Not every font program will be embedded automatically (only Type1 fonts for which you have an
.afm
as well as a .pfb
file).
You can solve these problems by looking at some of the examples.
For instance: this maps the name MS Gothic
(used when creating a Java font) to the corresponding font program (in this case, a specific font in a TrueType collection):
DefaultFontMapper mapper = new DefaultFontMapper();
BaseFontParameters parameters = new BaseFontParameters("c:/windows/fonts/msgothic.ttc,1");
parameters.encoding = BaseFont.IDENTITY_H;
mapper.putName("MS PGothic", parameters );
As we've used IDENTITY_H
as encoding, the characters will be stored in Unicode and a subset of the font will be embedded.
You can also create your own FontMapper
implementation, for instance:
FontMapper arialuni = new FontMapper() {
public BaseFont awtToPdf(Font font) {
try {
return BaseFont.createFont(
"c:/windows/fonts/arialuni.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public Font pdfToAwt(BaseFont font, int size) {
return null;
}
};
Graphics2D g2 = new PdfGraphics2D(canvas, 300, 150, arialuni);
Now it doesn't matter which java.awt.Font
you're using: all fonts will be mapped to MS Arial Unicode and the font will be embedded (BaseFont.EMBEDDED
).
These are only some examples. There are more on the official web site and in the book.
As I said before, this may work in iText 2.1.2, but if you take pride in what you do and if you value our customer, you'll upgrade to a more recent version of iText.