用PDFBOX写阿拉伯语并使用正确的字符表示forms而不分开

我正在尝试使用PDFBox Apache生成包含阿拉伯文本的PDF,但文本生成为单独的字符,因为Apache将给定的阿拉伯字符串解析为一系列通用的“官方”Unicode字符,这些字符相当于孤立forms的阿拉伯字符。

这是一个例子:
目标文本以PDF格式写入“应该是PDF文件中的预期输出” – >جملةبالعربي
我在PDF文件中得到了什么 – >

文字不正确

我尝试了一些方法,但这里有一些没用:
1.将字符串转换为比特流并尝试提取正确的值
2.使用UTF-8 && UTF-16处理String一个字节序列并从中提取值

有一些方法似乎非常有希望获得每个字符的值“Unicode”但它生成一般“官方Unicode”这是我的意思

System.out.println( Integer.toHexString( (int)(new String("كلمة").charAt(1))) ); 

输出是644但是fee0是预期的输出,因为这个字符在中间从那时起我应该得到中间的Unicode费用0

所以我想要的是一些生成正确Unicode的方法,而不仅仅是正式的Unicode

以下链接中第一个表中的“Left”列表示常规Unicode
阿拉伯语Unicode表维基百科

首先,我将感谢Tilman和M.Prokhorov向我展示了使用PDFBox Apache编写阿拉伯语的库。


本答复将分为两个部分:

  1. 下载库并安装它
  2. 如何使用该库

下载库并安装它

我们将使用ICU Library。
ICU代表Unicode的国际组件,它是一组成熟的,广泛使用的C / C ++和Java库,为软件应用程序提供Unicode和全球化支持。 ICU具有广泛的可移植性,可以在所有平台上以及C / C ++和Java软件之间为应用程序提供相同的结果。

要下载库,请从此处转到下载页面。
选择最新版本的ICU4J ,如下图所示。
下载页面
您将被转移到另一个页面,您将找到一个包含所需组件的直接链接的框。请继续下载三个文件,您将在下一个图像中找到突出显示的文件。

  1. ICU4J,docs.jar
  2. ICU4J-src.jar
  3. icu4j.jar

档

以下有关在Netbeans IDE中创建和添加库的说明

  1. 导航到工具栏和单击工具
  2. 选择图书馆
  3. 在左下角,您将找到新的库按钮创建您的
  4. 导航到您在库列表中创建的库
  5. 单击它并添加这样的jar文件夹
  6. 在类路径中添加icu4j.jar
  7. 在Sources中添加icu4j-src.jar
  8. 在Javadoc中添加icu4j-docs.jar
  9. 从右侧查看已打开的项目
  10. 展开要在其中使用库的项目
  11. 右键单击libraries文件夹,然后选择添加库
  12. 最后选择刚刚创建的库。

现在您已准备好使用该库,只需导入您想要的内容

 import com.ibm.icu.What_You_Want_To_Import; 

如何使用该库

使用ArabicShaping Class并反转String,我们可以编写正确的附加阿拉伯语LINE
以下是代码注释以下代码中的注释

 import com.ibm.icu.text.ArabicShaping; import com.ibm.icu.text.ArabicShapingException; import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.*; public class Main { public static void main(String[] args) throws IOException , ArabicShapingException { File f = new File("Arabic Font File of format.ttf"); PDDocument doc = new PDDocument(); PDPage Page = new PDPage(); doc.addPage(Page); PDPageContentStream Writer = new PDPageContentStream(doc, Page); Writer.beginText(); Writer.setFont(PDType0Font.load(doc, f), 20); Writer.newLineAtOffset(0, 700); //The Trick in the next Line of Code But Here is some few Notes first //We have to reverse the string because PDFBox is Writting from the left but Arabic is RTL Language //The output will be perfect except every line will be justified to the left "It's not hard to resolve this" // So we have to write arabic string to pdf line by line..It will be like this String s ="جملة بالعربي لتجربة الكلاس اللذي يساعد علي وصل الحروف بشكل صحيح"; Writer.showText(new StringBuilder(new ArabicShaping(reverseNumbersInString(ArabicShaping.LETTERS_SHAPE).shape(s))).reverse().toString()); // Note the previous line of code throws ArabicShapingExcpetion Writer.endText(); Writer.close(); doc.save(new File("File_Test.pdf")); doc.close(); } } 

这是输出

产量

我希望我已经完成了一切。

更新 :反转后请确保再次反转数字以获得相同的正确数字
这里有几个可以提供帮助的function

 public static boolean isInt(String Input) { try{Integer.parseInt(Input);return true;} catch(NumberFormatException e){return false;} } public static String reverseNumbersInString(String Input) { char[] Separated = Input.toCharArray();int i = 0; String Result = "",Hold = ""; for(;i