我正在尝试在使用 PDFBox 生成的 pdf 文件中显示阿拉伯字符串,实际上我可以使用 ICU4J 和特定字体显示来自 RTL 的阿拉伯字符串,但问题是这样的:即使字符串从 RTL 中正确显示,字符仍然分开即使我尝试了一些字体,问题还没有解决。这是用于测试的代码片段:
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
String dir = "resources/fonts/";
//I've used several fonts
PDType0Font farialUni = PDType0Font.load(document, new File(dir + "ARIALUNI.ttf"));
PDFont fArabType = PDType0Font.load(document, new File(dir + "arabtype.ttf"));
PDFont fMuka = PDType0Font.load(document, new File(dir + "Mukadimah.ttf"));
PDFont fFreeSans = PDType0Font.load(document, new File(dir + "FreeSans.ttf"));
PDFont fNoto = PDType0Font.load(document, new File(dir + "NotoNaskhArabic-Regular.ttf"));
PDPageContentStream stream = new PDPageContentStream(document, page);
stream.beginText();
stream.setFont(fNoto, 12);
stream.setLeading(12 * 1.2);
stream.newLineAtOffset(100, 800);
//Switch text order from to RTL
BiDiClass bidiClass = new BiDiClass();
String arabicText = "\u0627\u0644\u0633\u0644\u0627\u0645 \u0639\u0644\u064A\u0643\u0645 ";
//Use icu to inverse the order
String out = bidiClass.makeLineLogicalOrder(arabicText, true);
System.out.println(out);
stream.showText(out);
stream.newLine();
// ligature
stream.showText(out);
stream.endText();
stream.close();
document.save("example.pdf");
document.close();
这段代码给我的结果字符串是这样的: ال س ل ام ع ل ي ك م </p>
注意:我在结果字符串中添加空格字符只是为了清楚起见。