如何在使用iText创建的PDF中显示阿拉伯语 [英] How to display Arabic in PDF created using iText

查看:407
本文介绍了如何在使用iText创建的PDF中显示阿拉伯语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要您帮助显示阿拉伯语内容,并在我尝试创建的PDF示例中从右到左开始编写。下面是示例代码:

I need your help in display the Arabic content and to start the writing from right to left in the PDF sample that I am trying to create. Here is the sample code:

public static void main(String[] args) throws IOException {
    try {

        BaseFont ArialBase = BaseFont.createFont("C:\\Users\\dell\\Desktop\\arialbd.ttf", BaseFont.IDENTITY_H, true);
        Font ArialFont = new Font(ArialBase, 20);


        Document document = new Document(PageSize.LETTER);


        PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\dell\\Desktop\\HelloWorld.pdf"));
        document.setMargins(72f, 72f, 72f, 0f);

        document.open();
        document.add(new Paragraph("الموقع الإلكتروني,",ArialFont));
        document.close();
        System.out.println("PDF Completed");

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

使用上面的代码,阿拉伯语文字如下所示:

With the above code, the Arabic text will be shown as below:

اÙÙ...ÙÙ,عاÙØ¥ÙكترÙÙ†,,

الموقع الإلكتروني,

未识别,文字从左到右。那我怎么解决这个问题呢?

which is unidentified and the text is from left to right. So how can I solve this?

推荐答案

编码错误:

在源代码中使用非ASCII字符是一种糟糕的编程习惯。例如,您有الموقعالإلكتروني。此字符串应解释为双字节UNICODE字符。但是,当您使用与UNICODE不同的编码保存源代码文件时,或者使用不同的编码编译该代码时,或您的JVM时 em>使用不同的编码,每个双字节字符都有被破坏的风险,导致乱码,例如اÙÙ...ÙÙ,عاÙØ¥ÙكترÙني

It is a bad programming practice to use non-ASCII characters in your source code. For instance, you have "الموقع الإلكتروني". This String should be interpreted as double byte UNICODE characters. However, when you save the source code file using an encoding different from UNICODE, or when you compile that code using a different encoding, or when your JVM uses a different encoding each double-byte character risks to be corrupted, resulting in gibberish such as "الموقع الإلكتروني"

如何解决这个问题?使用UNICODE表示法:\ u0627 \ u0644 \ u0645 \ u0648 \ u0642 \ u0639 \ u0627 \ u0644 \ u0625 \ u0644 \ u0643 \ u062a \ u0631\\\و\\\ن\\\ي

How to solve this? Use the UNICODE notation: "\u0627\u0644\u0645\u0648\u0642\u0639 \u0627\u0644\u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a"

请查阅官方文档,免费电子书 StackOverflow上的最佳iText问题,您将在此处发现此问题已在此处描述:在生成PDF时无法获取捷克字符

Please consult the official documentation, the free ebook The Best iText Questions on StackOverflow, where you will discover that this problem has already been described here: Can't get Czech characters while generating a PDF

错误的字体:

如果您仔细阅读本书,您会发现您的示例可能不会工作,因为你可能使用了错误的字体。我在回答这个问题时解释了这一点: html中的阿拉伯字符内容为使用iText的pdf

If you read this book carefully, you'll discover that your example might not work because you may be using the wrong font. This is explained in my answer to this question: Arabic characters from html content to pdf using iText

您假设 arialbd.ttf 可以生成阿拉伯字形。据我所知,只有 arialuni.ttf 支持阿拉伯语。

You are assuming that arialbd.ttf can produce Arabic glyphs. As far as I know only arialuni.ttf supports Arabic.

错误的方法:

此外,您忽略了这样一个事实,即您只能在 ColumnText 和<上下文中使用阿拉伯语。 code> PdfPCell 对象。这里解释如下:如何使用eclipse在pdf中创建波斯语内容

Furthermore, you are overlooking the fact that you can only use Arabic in the context of the ColumnText and the PdfPCell object. This is explained here: how to create persian content in pdf using eclipse

例如:

BaseFont bf = BaseFont.createFont(
    "c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 20);
ColumnText column = new ColumnText(writer.getDirectContent());
column.setSimpleColumn(36, 730, 569, 36);
column.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
column.addElement(new Paragraph(
    "\u0627\u0644\u0645\u0648\u0642\u0639 \u0627\u0644\u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a", font));
column.go();

请注意,我使用的是 Identity-H 编码,因为涉及UNICODE。

Note that I am using the Identity-H encoding because UNICODE is involved.

这篇关于如何在使用iText创建的PDF中显示阿拉伯语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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