如何在使用itext 7 API生成的PDF中显示RTL的阿拉伯字符串?

时间:2016-07-27 19:07:23

标签: java pdf itext itext7

我现在几个小时都在努力解决这个问题,但我无法找到出路,问题是:

我编写了一个程序,使用itext版本7(以及很多版本)生成一个pdf文件以及一些统计信息,每个事情都是正确的,但是当我的pdf应该包含一些阿拉伯字符串时,它们只是从左边出现是的,无论我尝试过什么(改变字体,使用通用编码,将字符串放在桌子的单元格中,使用画布,......)我无法使它们正常显示。 这是我用来显示阿拉伯字符串的一段代码:

PdfFont fArabic=PdfFontFactory.createFont(ARABICFONT,PdfEncodings.IDENTITY_H, true);
final String ARABIC = "\u0627\u0644\u0633\u0644\u0627\u0645 \u0639\u0644\u064A\u0643\u0645";
document.add(new Paragraph(ARABIC).setFont(fArabic).setBaseDirection(BaseDirection.RIGHT_TO_LEFT));

注意:我认为itext 5版本也许可以解决它,但正如我所说,我无法撤消我编写的代码,特别是我有它的第三个统计库,而且项目已经很晚了。我只是想要使用itext 7版本的解决方案。

1 个答案:

答案 0 :(得分:6)

步骤1:将pdfCalligraph和licensekey jar加载到类路径中

步骤2:从xml文件加载许可证密钥:

LicenseKey.loadLicenseFile("itextkey-typography.xml");

第3步: 像往常一样创建Document

Document document = new Document(new PdfDocument(new PdfWriter(outFileName)));

PdfFont bf = PdfFontFactory.createFont(ARABIC_FONT, PdfEncodings.IDENTITY_H);
document.setFont(bf);

document.add(new Paragraph(ARABIC_TEXT).setTextAlignment(TextAlignment.RIGHT));

document.close();
相关问题