How to configure iText to support Asian characters

2020-03-24 03:35发布

问题:

My Java application creates PDFs using iText. Sometimes the content is Asian, which does not work. The Asian characters are not visible.

I found iTextAsian.jar on the iText download page, but don't know how to use it.

The goal is to use my favorite font as default. But when a character is not available an other font is used for this character. I have read, iText can select fonts that way automaticaly, but how to configure this?

I also want all required fonts to be included in the PDF, so users don't have to install Asian fonts on their computers.

回答1:

  • You can define a "FontSelector" and then add your fonts (fontSelector.AddFont). Now iText will choose the not available char from the next defined font by using the fontSelector.Process method.
  • Asian characters are usually Unicode so you have to set "BaseFont.IDENTITY_H" instead of Ansi ones (such as FontFactory.GetFont("tahoma", BaseFont.IDENTITY_H)).
  • If it's RTL, you need to wrap your text in elements which have run direction property and set it to rtl.