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

查看:693
本文介绍了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 dont really know if this is an issue with IText or I'm just missing something.

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

Here a small peace 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天全站免登陆