itext PDF - Greek letters are not appearing in the

2020-03-26 06:49发布

问题:

I am having hard time trying to generate PDF files containing Greek letters using itextpdf. I am reading the strings from an external source as UTF-8 strings. English letters appear in results but not the Greek ones. Searching for the problem, I think it might be related to the font used. I do not know what ttf file to use if this is the problem. Here is how am creating the font

 BaseFont bfTimes = BaseFont.createFont(FontFactory.HELVETICA,"UTF-8", BaseFont.EMBEDDED);
 Font times = new Font(bfTimes, 12, Font.BOLD);

any help is heartily appreciated!

回答1:

You need to use a font that has Greek characters in it. You may find Greek unicode fonts here.

You can check an example here on using unicode fonts from the author of itext.



回答2:

Actually the problem could lead to the way you create the font. Instead of passing "UTF-8" as codepage try to use "CP1253".

BaseFont bfTimes = BaseFont.createFont(FontFactory.HELVETICA,"CP1253", BaseFont.EMBEDDED);
Font times = new Font(bfTimes, 12, Font.BOLD);

Btw, BaseFont has static string constnts for CP1250, CP1252 and CP1257...



回答3:

I used this code:

BaseFont fonty = BaseFont.createFont("assets/ARIALBD.TTF", BaseFont.IDENTITY_H,       BaseFont.NOT_EMBEDDED);
     fonty.setSubset(true);
     Phrase myName = new Phrase("Your Not Latin Words", new Font(fonty,12));
     ColumnText.showTextAligned(canvas,
             Element.ALIGN_LEFT, myName, 66, 400, 0);

i had to paste the ARIALBD.TTF to assets folder