- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用 PDFBox Apache 生成包含阿拉伯语文本的 PDF,但文本生成为单独的字符,因为 Apache 将给定的阿拉伯语字符串解析为一系列通用的“官方”Unicode 字符,等同于孤立形式阿拉伯字符。
举个例子:
以 PDF 格式写入的目标文本“应预期在 PDF 文件中输出”-> جمل٩ بالعربي
我在 PDF 文件中得到了什么 ->
我尝试了一些方法,但没有用,下面是其中一些:
1. 将字符串转换为比特流并尝试提取正确的值
2. 使用 UTF-8 && UTF-16 处理字符串字节序列并从中提取值
有一些方法似乎很有希望获得每个字符的值“Unicode”但是它生成一般的“官方 Unicode”这就是我的意思
System.out.println( Integer.toHexString( (int)(new String("كلمة").charAt(1))) );
输出是 644,但 fee0 是预期的输出,因为这个字符在中间,从那时起我应该得到中间的 Unicode fee0
所以我想要的是一些生成正确 Unicode 的方法,而不仅仅是官方的
下面链接第一个表最左边一列代表通用Unicode
Arabic Unicode Tables Wikipedia
最佳答案
此答案中的示例代码可能已过时,请参阅 h q's answer对于工作示例代码
我们将使用 ICU图书馆。
ICU 代表 Unicode 国际组件,它是一套成熟的、广泛使用的 C/C++ 和 Java 库,为软件应用程序提供 Unicode 和全局化支持。 ICU 具有广泛的可移植性,并在所有平台上以及在 C/C++ 和 Java 软件之间为应用程序提供相同的结果。
要下载库,请转到 here 的下载页面.
选择最新版本的ICU4J,如下图所示。
您将转到另一个页面,您会发现一个框,其中包含所需组件的直接链接。继续并下载三个文件,您将在下一张图片中找到突出显示的文件。
在Netbeans IDE中创建和添加库的说明如下
现在您可以使用该库了,只需导入您想要的内容即可
import com.ibm.icu.What_You_Want_To_Import;
使用 ArabicShaping 类并反转字符串,我们可以编写正确的附加阿拉伯语 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();
}
}
这是输出
我希望我已经了解了所有内容。
更新:反转后确保再次反转数字以获得相同的正确数字
这里有几个函数可以提供帮助
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<Separated.length;i++ )
{
if(isInt(Separated[i]+"") == true)
{
while(i < Separated.length && (isInt(Separated[i]+"") == true || Separated[i] == '.' || Separated[i] == '-'))
{
Hold += Separated[i];
i++;
}
Result+=reverse(Hold);
Hold="";
}
else{Result+=Separated[i];}
}
return Result;
}
关于java - 使用 PDFBOX 以正确的字符呈现形式书写阿拉伯语而不被分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48284888/
我正在使用 Solr 以 3 种语言(阿拉伯语、法语和英语)索引文档,我使用了这个 fieldType :
当我在设备(它的语言是阿拉伯语)中运行我的应用程序时,我感到震惊的是,应该在正确方向的对象会向左移动,反之亦然。 后来我意识到是阿拉伯语本地化的自动布局导致了这个问题。 我以编程方式设置自动布局如下:
我想一个字母一个字母地拆分阿拉伯语单词,将它们涂成不同的颜色并在其上添加事件处理程序。 Image, how it looks like 我实现了关于绘画的部分想法,并处理了辅音字母 (harf),但
我关注 this教程,一切都很好,我预处理和训练我的模型但是当我想找到与以下代码的相似之处时: model = gensim.models.Word2Vec.load("wiki.fa.word2ve
我在使用阿拉伯语文本进行变音符号不敏感搜索时遇到问题。 我已经为相关表格测试了多种设置:utf8 和 utf16 编码以及 utf8_general_ci、utf16_general_ci 和 utf
我正在评估 NLTK 在分析和提取情绪的研究中处理阿拉伯文本的能力。 问题如下: NTLK 是否能够处理并允许分析阿拉伯文本? python 是否能够操作\标记阿拉伯文本? 我能否使用 Python
我正在使用 PHPMailer API 发送电子邮件。我想知道如何用阿拉伯语(非英语)发送主题 $mail->CharSet = 'utf-8'; $array= FetchTable('cos
在过去的 15 天或更长时间里,我一直在为一个奇怪的问题而苦苦挣扎。实际上我有一些阿拉伯语文本,但也有一些英文文本。 MY NAME "some arabic text" "some arabic t
我想用阿拉伯语制作一个 flutter 日期选择器,但有一些变化,这是我的代码: locale: Locale('ar', 'MA'), localizationsDelegates: [
我一直在努力为移动应用程序寻找良好的国际日历支持(包括 Hirji)。我的主要限制是 JVM 是 1.3 并且我们只有 AWT。有一个选项可以转移到另一个为我们提供 SWT 的 JVM,但我似乎找不到
我的应用程序是用英语和阿拉伯语两种不同版本开发的。 我在 iText 中使用 rowspan 和 colspan 创建了 pdf 表格,这在英文版中运行良好,但在阿拉伯语版 rowspan 中不起作用
我想将阿拉伯语单词分割成单个字符。基于直方图/配置文件,我假设我可以通过根据其基线(它具有相似的像素值)剪切/分割字符来进行分割过程。 但是,不幸的是,我仍然坚持构建适当的代码,以使其工作。 % Or
我在 JavaScript 函数中遇到了问题 var tDate = new Intl.DateTimeFormat("ar-US", { day: 'numeric', month: 'lo
我已经编写了 java 代码,我希望在 PdfPTable 上显示阿拉伯文字,该 PdfPTable 被用于 itext 文档以创建 PDF 文档 如附图“???”是阿拉伯代码' PdfPTable
简单地说,有没有支持Lucene.Net的Arabic Analyzer。因为我想索引大量的阿拉伯语 txt 文件。我可以使用标准分析器或空白分析器来解析阿拉伯文文件吗? analyzer = new
我正在使用 Vuejs,我想防止输入标签显示用户的字符并将其替换为我自己的字符(这是一些数字)。 我已经在计算中使用了@onchange 和 Watch 以及 getter 和 setter。问题是字
我正在研究 RTL 功能。我从数据库中获取字符串值作为 (f1 (firstname。但我想要 f1 (firstname) 这是我的 javascript 代码。下面的代码运行良好 EN语言。它不仅
我已经下载了阿拉伯语格式的阿拉伯语语言包和编辑器显示,但是当我纠正一些它不是用正确的方向写它总是从左方向开始,阿拉伯语总是从右方向开始但是这是行不通的.. 我的代码——
我使用的是 3.8.2 版本,插入新记录时,utf8 字符串在数据库中显示为问号 (?),但它会正确显示旧记录(使用 phpmyadmin 插入)。 这是我如何初始化 MySQLPool fun in
我有一个阿拉伯语短信模板,其中包含客户支持号码的占位符,并将占位符替换为实际电话号码(例如 +987654400)。替换时加号 (+) 错位在数字末尾,如 987654400+ 示例代码: >>> >
我是一名优秀的程序员,十分优秀!