我想将带有阿拉伯字母的word转换为pdf, 经过一番研发后,我决定使用
org.apache.poi.xwpf.converter.pdf.PdfConverter
但是当我写下面的代码时,最终结果是从左到右,并且单词很困惑,例如pdf中的“?????”结果是“?????”我的docx非常大而且没有一个段落:
public class ConvertWord {
public static void ConvertToPDF(String docPath, String pdfPath) {
try {
InputStream doc = new FileInputStream(new File(docPath));
XWPFDocument document = new
PdfOptions options = PdfOptions.create();
options.fontEncoding("UTF-8")
OutputStream out = new FileOutputStream(new File(pdfPath));
PdfConverter.getInstance().convert(document,out,options);
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
}
public static void main(String[] args) {
ConvertWord cwoWord=new ConvertWord();
cwoWord.ConvertToPDF("D://" + "usc.docx","D://test12.pdf");
}
}
最佳答案
有一个解决方法可以解决此问题。 您需要使用ICU4J图书馆。 然后:
String shaped = new StringBuilder(new ArabicShaping(ArabicShaping.LETTERS_SHAPE).shape(s))
.reverse().toString();
尽管您在使用波斯语 Unicode 时可能会遇到一些问题。存在一个问题,有人通过修补 ArabicShaping
解决了这个问题。我没有找到它的链接,但是
here是修补后的代码。
(我必须将文件上传到我的 Google 云端硬盘,这样它就不会随着时间的推移而被删除)
此外,here是代码的链接以及它与主代码的区别。
为了方便起见,我将类名称更改为 PersianShaping
。
关于java - 在 XWPFDocument 中使用阿拉伯语和波斯语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44998739/