有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在XWPF文档中使用阿拉伯语和波斯语

我想用阿拉伯字母将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");

    }



   }

共 (2) 个答案

  1. # 1 楼答案

    有一个解决方法可以解决这个问题。 你需要使用ICU4J库。 然后:

    String shaped = new StringBuilder(new ArabicShaping(ArabicShaping.LETTERS_SHAPE).shape(s))
    .reverse().toString();
    

    尽管你可能对波斯独角兽有一些问题。出现了一个问题,有人通过修补ArabicShaping解决了这个问题。我没有找到它的链接但是 here是修补代码。 (我必须把文件上传到我的谷歌硬盘上,这样它就不会被删除了)

    另外,here是指向代码及其与主代码的差异的链接

    为了方便起见,我把类名改为PersianShaping