ITextPdf:从右到左打印阿拉伯语字符串 (RTL) [英] ITextPdf: Printing Arabic strings from Right To Left (RTL)

查看:33
本文介绍了ITextPdf:从右到左打印阿拉伯语字符串 (RTL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 java 上使用 iTextpdf 来生成带戳记的 PDF,有时生成的 PDF 是阿拉伯语,我面临一个有趣的问题.为了让阿拉伯语页面从右到左 (RTL) 创建,我使用具有属性 PdfPCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL) 的表格和单元格.当我使用这个属性时,阿拉伯语根本不显示,如果我避免调用这个属性,阿拉伯语字符串被正确显示,这意味着我不应该有字体问题,我真的不知道这是否是 iText 的问题或者我只是错过了一些东西.

I use iTextpdf on java in order to generate stamped PDFs, sometimes the generated PDF is in Arabic and I am facing a funny problem. To let the Arabic page being created from Right To Left (RTL) I use tables and cells which have the property PdfPCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL). When I use this property Arabic does not show at all, if I avoid the call to this property Arabic strings are correctly showed, this means I shouldn't have problems with fonts and I don't really know if this is an issue with iText or I'm just missing something.

这是正确显示阿拉伯字符串的一小段代码:

Here a small piece of code which shows an Arabic string correctly:

BaseFont bf = BaseFont.createFont(Application.getBASEPATH() + "fonts/arabic.ttf",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font trebuchetSmaller = new Font(bf, 10, 0);

PdfPTable tbl = new PdfPTable(1); 
PdfPCell cell = new PdfPCell();
Paragraph paragraph = new Paragraph();
paragraph.add(new Phrase("ربط صفحة على شبكة الإنترنت"), trebuchetSmaller));
cell.addElement(paragraph);
tbl.addCell(cell);

这是使阿拉伯字符串消失所需的更改:

Here the needed change which makes the Arabic string disappear:

BaseFont bf = BaseFont.createFont(Application.getBASEPATH() + "fonts/arabic.ttf",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font trebuchetSmaller = new Font(bf, 10, 0);

PdfPTable tbl = new PdfPTable(1); 
PdfPCell cell = new PdfPCell();
cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
Paragraph paragraph = new Paragraph();
paragraph.add(new Phrase("ربط صفحة على شبكة الإنترنت"), trebuchetSmaller));
cell.addElement(paragraph);
tbl.addCell(cell);

如果我将 PdfWriter.RUN_DIRECTION_RTL 与英文字符串一起使用,它会以应有的格式正确显示.如果我使用混合了英文和阿拉伯字符的字符串,则只显示英文字符.

If I use PdfWriter.RUN_DIRECTION_RTL with an English string it shows correctly in the format it has supposed to be. If I use a string with mixed English and Arabic characters just the English ones are showed.

推荐答案

更改您的代码以包含注册的字体:

Change your code to include the registered font:

new Phrase("آزمايش", font)

您也可以直接添加短语:

Also you can add the phrase directly:

PdfPCell pdfCell = new PdfPCell(new Phrase("آزمايش", font));  
pdfCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL); 

这篇关于ITextPdf:从右到左打印阿拉伯语字符串 (RTL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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