如何使用 eclipse 在 pdf 中创建波斯内容 [英] how to create persian content in pdf using eclipse

查看:18
本文介绍了如何使用 eclipse 在 pdf 中创建波斯内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 eclipse 的 PDF 文件中插入 UNICODE 字符时遇到问题.对此有一些解决方案,但对我来说效率不高.

解决方法是这样的.

document.add(new Paragraph("Unicode: u0418", new Font(bfComic, 12)));

我想从数据库中检索数据并将它们显示给用户,我的字符使用阿拉伯文字,有时使用波斯文字.

您有什么建议?

谢谢

解决方案

您遇到了不同的问题:

数据编码:

I have a problem with inserting UNICODE characters in a PDF file in eclipse. There is some solution for this that it is not very efficient for me.

The solution is like this.

document.add(new Paragraph("Unicode: u0418", new Font(bfComic, 12)));

I want to retrieve data from a database and show them to the user and my characters are in Arabic script and sometimes in Farsi script.

What solution do you suggest?

thanks

解决方案

You are experiencing different problems:

Encoding of the data:

Please download chapter 2 of my book and go to section 2.2.2 entitled "The Phrase object: a List of Chunks with leading". In this section, look for the title "Database encoding versus the default CharSet used by the JVM".

You will see that database values are retrieved like this:

String name1 = new String(rs.getBytes("given_name"), "UTF-8");

That’s because the database contains different names with special characters. You risk that these special characters are displayed as gibberish if you would retrieve the field like this:

String name2 = rs.getString("given_name")

Encoding of the font:

You create your font like this:

Font font = new Font(bfComic, 12);

You don't show how you create bfComic, but I assume that this object is a BaseFont object using IDENTITY_H as encoding.

Writing from right to left / making ligatures

Although your code will work to show a single character, it won't work to show a sentence correctly.

Suppose that name1 is the Arabic version of the name "Lawrence of Arabia" and that we want to write this name to a PDF. This is done three times in the following screen shot:

The first line is wrong, because the characters are in the wrong order. They are written from left to right whereas they should be written from right to left. This is what will happen when you do:

document.add(name1);

Even if the encoding is correct, you're rendering the text incorrectly.

The second line is also wrong. The characters are now in the correct order, but no ligatures are made: ل followed by و should be combined into a single glyph: لو

You can only achieve this by adding the content to a ColumnText or PdfPCell object, and by setting the run direction to PdfWriter.RUN_DIRECTION_RTL. For instance:

pdfCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

Now the text will be rendered correctly.

This is explained in chapter 11 of my book. You can find a full example here: Ligatures2

这篇关于如何使用 eclipse 在 pdf 中创建波斯内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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