Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
702 views
in Technique[技术] by (71.8m points)

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

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 = "u0627u0644u0633u0644u0627u0645 u0639u0644u064Au0643u0645";
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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...