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?
4 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
-
1I have studied some examples in iText book, seems none gave a hint for my problem. In your example, there is a flag "BaseFont.EMBEDDED" that can be used to indicate embedding of font file. But what I have to do is converting XML file to PDF directory with defined tagmap, the whole process is only one call. There is no place where I can put some code to embed font. In your example, we can set the font to some paragraph so that font is embedded into that paragraph. But for automated PDF generation with tagmap, how can we set the font, which class/interface can we use to embed the font? Thanks. – moonli Dec 04 '09 at 16:47
-
4Problem solved. We can embed font with configuration in TagMap. Setting "embedded" attribute to "true" indicates font embedding. Thanks anyway. – moonli Dec 07 '09 at 03:46
-
I have to add the PATH to the FONT - which I want to embedded. `BaseFont.CreateFont(@"D:\fonts\HELR45W.ttf", BaseFont.CP1250, true)` – Jenan Dec 13 '17 at 13:05
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/

- 1
- 38
- 145
- 223
Here's a really easy way of instructing iText to embed all fonts. Insert this before your code to load fonts:
FontFactory.defaultEmbedding = true;

- 1,782
- 17
- 17
-
3Note that in iTextSharp, FontFactory.DefaultEmbedding is a read-only property. – howcheng Jul 16 '14 at 21:45
You can try transforming it into PDF/A which has all the fonts embedded.
PdfReader reader = new PdfReader(GetTemplateBytes());
pst = new PdfStamper(reader, Response.OutputStream);
pst.Writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_4);
pst.Writer.PDFXConformance = PdfWriter.PDFA1A;

- 8,018
- 10
- 47
- 62
-
This suggestion is not working, The itext writer sets some flags here and there, but the actual font references and font data are not added to the file properly. Running this on a file that has unembedded Fonts still creates a file with the same fonts missing. I tested this with iText4.1.6 and iText 5.5.10 I have been looking for an easy way to embed fonts with iText 4 or 5 for a long time, no success – Hakan Usakli Jul 12 '20 at 11:33
-
This is a 9 year old solution. As I remember it was pretty difficult to do it then. I would not bet on it working today. Maybe try something newer. – Dragos Durlut Jul 13 '20 at 10:01
-
Unfortunately iText has not made it any easier; Embedding existing Fonts in an existing file was not working 9 years ago and it is still not working today as of this writing to my knowledge. There is no easy way with iText as far as I know..I am still looking. – Hakan Usakli Jul 14 '20 at 14:48