How to display Arabic strings from RTL in PDF generated using itext 7 API?

我与影子孤独终老i 提交于 2019-11-27 08:29:23

问题


I'm struggling with this problem for hours now but I can't find a way out, the problem is that:

I wrote a program that generate a pdf file using itext version 7 (and a lot of it) along with some statistics, every things is right till here, but when my pdf should contain some arabic strings they just appear from left to right, no matter what I've tried (changing fonts, using universal encodings, making the string inside a cell of table, using canvas, ...) I can't make them appear normally. Here is a piece of code I use for displaying arabic strings:

PdfFont fArabic=PdfFontFactory.createFont(ARABICFONT,PdfEncodings.IDENTITY_H, true);
final String ARABIC = "\u0627\u0644\u0633\u0644\u0627\u0645 \u0639\u0644\u064A\u0643\u0645";
document.add(new Paragraph(ARABIC).setFont(fArabic).setBaseDirection(BaseDirection.RIGHT_TO_LEFT));

Note: I think that itext 5 version perhaps can solve it but as I said I can't undone the code I wrote especially I have with it a third library for statistics and also the project is already late.I just want a solution using itext 7 version.


回答1:


Step 1: load pdfCalligraph and licensekey jars into your classpath

Step 2: load license key from xml file:

LicenseKey.loadLicenseFile("itextkey-typography.xml");

Step 3: Create your Document as usual:

Document document = new Document(new PdfDocument(new PdfWriter(outFileName)));

PdfFont bf = PdfFontFactory.createFont(ARABIC_FONT, PdfEncodings.IDENTITY_H);
document.setFont(bf);

document.add(new Paragraph(ARABIC_TEXT).setTextAlignment(TextAlignment.RIGHT));

document.close();


来源:https://stackoverflow.com/questions/38621260/how-to-display-arabic-strings-from-rtl-in-pdf-generated-using-itext-7-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!