我现在已经为这个问题苦苦挣扎了几个小时,但我找不到出路,问题是:
我编写了一个程序,使用 itext 版本 7(以及其中的很多)以及一些统计数据生成 pdf 文件,到这里为止一切都是正确的,但是当我的 pdf 应该包含一些阿拉伯字符串时,它们只是从左到是的,无论我尝试了什么(更改字体、使用通用编码、将字符串放入表格的单元格中、使用 Canvas ,...)我都无法使它们正常显示。 这是我用来显示阿拉伯字符串的一段代码:
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 步:将 pdfCalligraph 和 licensekey jar 加载到您的类路径中
第 2 步:从 xml 文件加载许可证 key :
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();
关于java - 如何在使用 itext 7 API 生成的 PDF 中显示来自 RTL 的阿拉伯语字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38621260/