iText is deforming/misspelling Unicode characters

2020-02-11 07:55发布

问题:

I am trying to generate a pdf using iText v2.1.5 which basically contains Unicode characters, Ariel is the font being used for the same. Now the problem is that some of the characters like the "Hindi" and "Gujarati" characters are having issues being generated properly. Please take a look at the below screenshot to understand the issue better.

Both the highlighted fonts in the Graph and Table use the same fonts, but the font in the table are not proper for hindi and gujarti characters.

The highlighted characters in the Graph(fusion charts) are generated using PhantomJS and embedded in the pdf as a png image. The characters in the Table below have the same "Arial_Unicode_MS.ttf" font (available at /usr/share/fonts/truetype/Arial_Unicode_MS.ttf in the linux machine) as that of the ones used in the Graph, but the table is generated using iText and I can't figure out why this is resulting in such unexpected behaviour.

I tried debugging phantomJS and I can assure that it uses the same font file mentioned above. Please refer the code below that I have in iText :

String formattedData="જિજ્ઞેશ";
BaseFont nationalBase = BaseFont.createFont("/usr/share/fonts/truetype/Arial_Unicode_MS.ttf", BaseFont.COURIER, BaseFont.NOT_EMBEDDED);
PdfPCell dataCell= new PdfPCell(new Phrase(8, new Chunk(formattedData, dataFont)));

To check if the issue is because of iText I tried using the Unicode characters in the formattedData="જિજ્ઞેશ" directly, still the font's reflected in the table generated differ a lot that what I originally used.

回答1:

As explained many times before, iText 5 doesn't support Hindi. We fixed this in iText 7 and we released that version in India at the Great Indian Developer Summit.

iText 7 has a special add-on, named pdfCalligraph and as soon as you plug in this add-on, Devaganari and Tamil will be rendered correctly. Please read Chapter 2 of "iText 7: Building Blocks" (please scroll towards the end of the chapter) to find out how the pdfCalligraph add-on works. You can get a free trial version of pdfCalligraph here.

Currently Devanagari, Tamil and Arabic are supported. Support for other writing systems will be added at the request of paying customers.