打印 PDF 时反转阿拉伯语
我正在尝试使用此处找到的 Java 代码在一些 PDF 文档中打印阿拉伯语: http://www.java2s.com/Code/Java/PDF-RTF /ArabicTextinPDF.htm
该示例效果很好,只是文本向后显示。例如,稍微改变一下示例:
String txt = "\u0623\u0628\u062c\u062f\u064a\u0629 \u0639\u0631\u0628\u064a\u0629";
System.out.println(txt);
g2.drawString(txt, 100, 30);
与 PDF 相比,屏幕上打印的是相同的字符,但方向相反。控制台输出正确,但 PDF 不正确。
我不想简单地反转字符,因为否则我将失去双向支持......
非常感谢
I'm trying to print Arabic in some PDF documents using the Java code found here :
http://www.java2s.com/Code/Java/PDF-RTF/ArabicTextinPDF.htm
The example works great, except that the text comes out backwards. For example, changing the example slightly :
String txt = "\u0623\u0628\u062c\u062f\u064a\u0629 \u0639\u0631\u0628\u064a\u0629";
System.out.println(txt);
g2.drawString(txt, 100, 30);
What is printed on the screen are the same characters but in the opposite direction, compared to the PDF. The console output is correct, the PDF is not.
I don't want to simply reverse the characters because otherwise I would lose bi-directional support ...
Thanks much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IIRC,iText 支持比
drawString
更高级别的阿拉伯语整形。让我们看看这里...啊!
ColumnText.showTextAligned(PdfContentByte canvas, int adjustment, Phrasephrase, float x, float y, floatrotation, int runDirection, int arabicOptions)
对齐方式是
Element.ALIGN_*
之一。运行方向是PdfWriter.RUN_DIRECTION_*
之一。阿拉伯语选项是位标志,ColumnText.AR_*
这应该可以解决问题,但有一个警告:我不确定它是否会处理同一短语中的多个方向。您的测试字符串包含 CJKV、阿拉伯语和拉丁字符,因此应该有两个方向变化。
祝你好运。
IIRC, iText supports Arabic shaping at a highler level than
drawString
. Lets see here...Ah!
ColumnText.showTextAligned(PdfContentByte canvas, int alignment, Phrase phrase, float x, float y, float rotation, int runDirection, int arabicOptions)
Alignment is one of
Element.ALIGN_*
. Run direction is one ofPdfWriter.RUN_DIRECTION_*
. Arabic options are bit flags,ColumnText.AR_*
That should do the trick, with one caveat: I'm not sure that it'll handle multiple directions in the same phrase. Your test string has CJKV, Arabic, and Latin characters, so there should be two direction changes.
Good luck.
弄清楚了,这是完整的过程:
您会注意到它支持多种语言并具有双向支持。唯一的问题是无法复制/粘贴生成的 PDF 文本,因为它是图像。我可以忍受这一点。
Figured it out, here is the complete process :
You'll notice it does multiple languages with bi-directional support. Only thing is it's impossible to copy/paste the resulting PDF text, as it is an image. I can live with that.
Unicode 阿拉伯语(或其他任何内容)在 Java 程序中始终按逻辑顺序排列。有些 PDF 是按照视觉顺序制作的,尽管这在现代世界中相当罕见。您引用的程序可能是一个 hack,最终生成的 PDF 可以在某种程度上用于某些目的。
如果我是你,我会首先检查一些用现代工具用阿拉伯语生成的 PDF。
这种构建 PDF 的“图形”方法对我来说似乎是有风险的。
Unicode Arabic (or anything else) is always in logical order in a Java program. Some PDFs are made in visual order, though this is quite rare in the modern world. The program you cite might be a hack that ends up with PDF's that work, sort of, for some purposes.
If I were you, I'd start by examining some PDF's produced in Arabic by some modern tool.
This sort of 'graphics' approach to PDF construction seems risky to me at best.