fr3d0
fr3d0

Reputation: 225

write special characters using a custom font with itext java

I am adding a custom font to itext like so:

PdfContentByte cb = writer.getDirectContent();
BaseFont customfont = BaseFont.createFont("/assets/fonts/GillSans-SemiBold.ttf", BaseFont.CP1257, BaseFont.EMBEDDED);

now this it does fine but when i try to print accented characters like Matemática it prints ?
Any thoughts on how to print accented characters with gilSans SemiBold font?

Upvotes: 2

Views: 1446

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

Please change the second parameter in this line:

BaseFont customfont = BaseFont.createFont(
    "/assets/fonts/GillSans-SemiBold.ttf",
    BaseFont.CP1257, BaseFont.EMBEDDED);

Make it:

BaseFont customfont = BaseFont.createFont(
    "/assets/fonts/GillSans-SemiBold.ttf",
    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

When using CP1257, you create a simple font that contains only 256 characters. You are using characters that aren't a part of those 256 characters. You need to create a composite font that accepts UNICODE characters. As you are using a horizontal writing system, use IDENTITY_H as "encoding".

Upvotes: 4

Related Questions