Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed font into PDF file by using iText

I defined a tag map, and got a XML data file. I want to convert the XML data file to PDF by using iText. The question is how to embed fonts (e.g. Polish font, Chinese font) into the target PDF when converting XML to PDF?

like image 660
moonli Avatar asked Nov 21 '09 09:11

moonli


3 Answers

If you are doing more work with iText, you may want to invest into the iText book - it has examples for all the features of iText.

There is a parameter that you specify when you create your font that defines font embedding:

BaseFont helvetica = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
Font font = new Font(helvetica, 12, Font.NORMAL);

You can find more iText font related examples here: http://1t3xt.info/examples/browse/?page=toc&id=14

like image 135
khkremer Avatar answered Nov 05 '22 16:11

khkremer


However, you will have problem if you will not have single font matching any characters used by you and you need multiple fonts.

In this case the FontSelector class is for you.

I've written a short article about that:
http://lechlukasz.wordpress.com/2010/01/06/using-dynamic-fonts-for-international-texts-in-itext/

like image 36
Danubian Sailor Avatar answered Nov 05 '22 17:11

Danubian Sailor


Here's a really easy way of instructing iText to embed all fonts. Insert this before your code to load fonts:

FontFactory.defaultEmbedding = true;
like image 6
pents90 Avatar answered Nov 05 '22 17:11

pents90