阿拉伯字符无法正确显示 [英] Arabic characters do not display correctly

查看:114
本文介绍了阿拉伯字符无法正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的网站,我使用itextpdf 5.5.4生成PDF下载.该网站是为说英语的人设计的.最近,一个来自埃及的用户使用该网站,输入了一些阿拉伯语内容,并就他所遇到的问题与我联系.

For my website, I use itextpdf 5.5.4 to generate PDF downloads. The website is meant for people who speak English. Recently, a user from Egypt used the site, entered some Arabic content, and contacted me with the problem he has.

这是他在浏览器中正确显示的阿拉伯语内容:

This is his Arabic content shown correctly in the browser:

这在PDF中显示不正确:

This is incorrect display in PDF:

这是我拥有的Java代码.请注意,它实际上能够正确地生成带有中文字符的PDF:

Here is the Java code I have. Please note that it is actually able to generate PDF with Chinese characters CORRECTLY:

BASE_FONT base = BaseFont.createFont("/fonts/ARIALUNI.ttf", BaseFont.IDENTITY_H , BaseFont.EMBEDDED);                       
Font f = new Font(base, 10f);
String htmlString = string_with_Arabic_text;
Paragraph p = new Paragraph(htmlString, f); 
p.setSpacingBefore(20.0f);
p.setSpacingAfter(7.0f);
document.add(p);

如何解决该问题?

在Eclipse(我使用的IDE)中,我可以看到阿拉伯字符在htmlString中正确显示.目前,由于项目原因,我无法升级为使用最新版本的itextpdf.

In Eclipse (the IDE I use), I am able to see Arabic characters display correctly in htmlString. At this moment, I cannot upgrade to use the latest version of itextpdf due to various project reasons.

推荐答案

iText 5对非西方书写系统的支持有限.它支持从右到左书写,但仅在 ColumnText PdfPCell 对象的上下文中.

iText 5 has limited support for non-Western writing systems. It support right-to-left writing but only in the context of ColumnText and PdfPCell objects.

这是带有ColumnText的iText 5示例,其中p包含阿拉伯语文本:

This is an iText 5 example with ColumnText where p contains text in Arabic:

ColumnText canvas = new ColumnText(writer.getDirectContent());
canvas.setSimpleColumn(36, 750, 559, 780);
canvas.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
canvas.addElement(p);
canvas.go();

这是带有PdfPCell的iText 5示例,其中p包含阿拉伯语文本:

This is an iText 5 example with PdfPCell where p contains text in Arabic:

PdfPCell cell = new PdfPCell(p);
cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

这很烦人,因为这意味着您必须重写整个应用程序,以便将所有文本添加到ColumnTextPdfPCell对象中.您还必须检查内容,以检查是否需要更改运行方向.

This is very annoying, as it would mean that you have to rewrite your entire application so that all text is added either in a ColumnText or in a PdfPCell object. You'd also have to examine the content to check if you need to change the run direction.

因为无论如何都必须重写应用程序,所以最好升级到iText 7,因为iText 7具有一个附加程序,可以根据内容的UNICODE值检测书写系统(请参见如何显示使用itext 7 API从PDF中的RTL生成阿拉伯字符串?

As you have to rewrite the application anyway, it would be best to upgrade to iText 7, because iText 7 has an add-on that detects the writing system based on the UNICODE values of the content (see pdfCalligraph). When Arabic or Hebrew text is detected, the add-on changes the writing system for "left to right" to "right to left." See How to display Arabic strings from RTL in PDF generated using itext 7 API?

我看到您正在对文档进行编码.请注意,通过使用HTML创建内容,然后使用 pdfHTML插件. PDF到HTML教程包含一些涉及阿拉伯语的示例.请参见

I see that you are coding your document. Please note that you can save yourself a lot of work by creating the content in HTML, and then converting it to PDF using the pdfHTML add-on. The PDF to HTML tutorial has some examples involving Arabic. See the section on internationalization in chapter 6, and the following FAQ entries:

  • Which languages are supported in pdfHTML?
  • How to convert HTML containing Arabic/Hebrew characters to PDF?

iText 7也是第一个支持更多书写系统的版本,例如梵文,泰米尔语,泰卢固语....有关更多信息,请阅读

iText 7 is also the first version that supports more writing systems, such as Devanagari, Tamil, Telugu,... For more info, read the pdfCalligraph white paper.

重要:pdfCalligraph附加组件是封闭源.您将需要试用许可证来进行测试,并需要商业许可证才能在生产中使用它.请注意,您正在使用的iText的当前版本已作为AGPL软件获得许可,这意味着您不能在封闭的源上下文中使用您的项目.您提到外部用户,这意味着您正在分发服务.您是否开源了所有自己的源代码?如果没有,您应该购买商业许可证以使用iText.

Important: the pdfCalligraph add-on is closed source. You'll need a trial license to test it and a commercial license to use it in production. Note that the current version of iText that you are using is licensed as AGPL software, which implies that you can't use your project in a closed source context. You mention external users, which means that you are distributing your service. Did you open source all your own source code? If not, you should purchase a commercial license for your use of iText.

这篇关于阿拉伯字符无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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