Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know the correct encoding when I calling BaseFont.createFont() with itext?

Tags:

pdf

fonts

itext

I have some custom Chinese font files:

aaa.ttf
bbb.ttf
ccc.ttf

And I want to use them to create my pdf with iText.

This is the code I use to create a BaseFont:

String encoding = ???
BaseFont messageBaseFont = BaseFont.createFont("fonts/aaa.ttf", 
                               encoding, 
                               BaseFont.EMBEDDED);

But I don't know how to find the encoding. Take aaa.ttf for example:

enter image description here

Can I find the encoding from the information? I tried some encodings like BaseFont.CP1252 or BaseFont.WINANSI, but they didn't work, the output are blank page or a lot of ?????? characters if they are Chinese characters.

like image 352
Freewind Avatar asked Oct 17 '12 08:10

Freewind


1 Answers

There are different answers to this question. For a full overview, you could read chapter 11 of my book, "iText in Action", but if you just want to be safe, use Identity-H for horizontal text (and Identity-V). This isn't really an encoding, but it will create a composite font in the PDF that uses Unicode: http://api.itextpdf.com/itext/com/itextpdf/text/pdf/BaseFont.html#IDENTITY_H

like image 171
Bruno Lowagie Avatar answered Nov 03 '22 02:11

Bruno Lowagie