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

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

问题描述

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

public static void main(String[] args) 抛出 IOException {尝试 {BaseFont ArialBase = BaseFont.createFont("C:\\Users\\dell\\Desktop\\arialbd.ttf", BaseFont.IDENTITY_H, true);Font ArialFont = new Font(ArialBase, 20);文档文档 = 新文档(PageSize.LETTER);PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\dell\\Desktop\\HelloWorld.pdf"));document.setMargins(72f, 72f, 72f, 0f);文档.open();document.add(new Paragraph("الموقع الإلكتروني,",ArialFont));文档.close();System.out.println("PDF 完成");} catch (DocumentException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();}}

使用上面的代码,阿拉伯文将显示如下:

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

这是未识别的,文本从左到右.那么我该如何解决这个问题?

解决方案

错误的编码:

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

如何解决这个问题?使用 UNICODE 符号:"\u0627\u0644\u0645\u0648\u0642\u0639\u0627\u0644\u0625\u0644\u0643\u062a\u0631\u0646\u062a\u0631\u0646\u0648\u0648

请查阅官方文档,免费电子书StackOverflow 上的最佳 iText 问题,你会发现这里已经描述了这个问题:可以'生成 PDF 时不获取捷克语字符

字体错误:

如果您仔细阅读本书,您会发现您的示例可能不起作用,因为您可能使用了错误的字体.这在我对这个问题的回答中得到了解释:来自 html 的阿拉伯字符使用 iText 将内容转换为 pdf

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

错误的做法:

此外,您忽略了一个事实,即您只能在 ColumnTextPdfPCell 对象的上下文中使用阿拉伯语.这在这里解释: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 列 = 新的 ColumnText(writer.getDirectContent());column.setSimpleColumn(36, 730, 569, 36);column.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);column.addElement(新段落("\u0627\u0644\u0645\u0648\u0642\u0639\u0627\u0644\u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a\u0646\u064a;"column.go();

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

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?

解决方案

Wrong encoding:

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 "الموقع الإلكتروني"

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

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

Wrong font:

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

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

Wrong approach:

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

For instance:

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

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

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

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