打印 PDF 时反转阿拉伯语

发布于 2024-10-20 09:00:05 字数 503 浏览 1 评论 0原文

我正在尝试使用此处找到的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失退 2024-10-27 09:00:05

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 of PdfWriter.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.

终止放荡 2024-10-27 09:00:05

弄清楚了,这是完整的过程:

document.open();
java.awt.Font font = new java.awt.Font("times", 0, 30);
PdfContentByte cb = writer.getDirectContent();
java.awt.Graphics2D g2 = cb.createGraphicsShapes(PageSize.A4.width(), PageSize.A4.height());
g2.setFont(font);
String txt = "日本人 أبجدية عربية Dès Noël où";
System.out.println(txt);
java.awt.font.FontRenderContext frc = g2.getFontRenderContext();
java.awt.font.TextLayout layout = new java.awt.font.TextLayout(txt, font, frc);
layout.draw(g2, 15, 55);
g2.dispose();
document.close();

您会注意到它支持多种语言并具有双向支持。唯一的问题是无法复制/粘贴生成的 PDF 文本,因为它是图像。我可以忍受这一点。

Figured it out, here is the complete process :

document.open();
java.awt.Font font = new java.awt.Font("times", 0, 30);
PdfContentByte cb = writer.getDirectContent();
java.awt.Graphics2D g2 = cb.createGraphicsShapes(PageSize.A4.width(), PageSize.A4.height());
g2.setFont(font);
String txt = "日本人 أبجدية عربية Dès Noël où";
System.out.println(txt);
java.awt.font.FontRenderContext frc = g2.getFontRenderContext();
java.awt.font.TextLayout layout = new java.awt.font.TextLayout(txt, font, frc);
layout.draw(g2, 15, 55);
g2.dispose();
document.close();

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.

空‖城人不在 2024-10-27 09:00:05

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文