如何在使用itext 7 API生成的PDF中显示RTL的阿拉伯字符串? [英] How to display Arabic strings from RTL in PDF generated using itext 7 API?

查看:126
本文介绍了如何在使用itext 7 API生成的PDF中显示RTL的阿拉伯字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决这个问题好几个小时但是我找不到出路,问题是:

I'm struggling with this problem for hours now but I can't find a way out, the problem is that:

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

I wrote a program that generate a pdf file using itext version 7 (and a lot of it) along with some statistics, every things is right till here, but when my pdf should contain some arabic strings they just appear from left to right, no matter what I've tried (changing fonts, using universal encodings, making the string inside a cell of table, using canvas, ...) I can't make them appear normally. Here is a piece of code I use for displaying arabic strings:

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版本的解决方案。

Note: I think that itext 5 version perhaps can solve it but as I said I can't undone the code I wrote especially I have with it a third library for statistics and also the project is already late.I just want a solution using itext 7 version.

推荐答案

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

Step 1: load pdfCalligraph and licensekey jars into your classpath

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

Step 2: load license key from xml file:

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

第3步:
创建凭证像往常一样:

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();

这篇关于如何在使用itext 7 API生成的PDF中显示RTL的阿拉伯字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆