Right to Left (RTL) text in XWPFDocument (Apache POI)

爱⌒轻易说出口 提交于 2020-01-09 11:57:09

问题


I could not find a way to make a RTL paragraph in Docx using XWPFDocument (Apache POI) in my Java program. Here's my code that generates the XWPFDocument.

    XWPFParagraph title = document.createParagraph();
    title.setAlignment(ParagraphAlignment.CENTER);
    title.setVerticalAlignment(TextAlignment.CENTER);
    title.setWordWrap(true);
    XWPFRun titleRun = title.createRun();
    titleRun.setText(reportDesign.getName());

    XWPFTable s = document.createTable(resultList.size()+1, columnList.size());
    // declare a row object reference
    XWPFTableRow r = s.getRow(0);
    // declare a cell object reference
    XWPFTableCell c = null;
    // create columnList.size() cells (0-(columnList.size()-1))
    for (int cellnum = 0; cellnum < columnList.size(); cellnum++) {
        c = r.getCell(cellnum);
        c.setColor("c9c9c9");
        c.setVerticalAlignment(XWPFVertAlign.CENTER);
        c.setText(columnList.get(cellnum).getColumnHeader());
    }
    // create a sheet with resultList.size() rows (1-resultList.size())
    for (int rownum = 0; rownum < resultList.size(); rownum++) {
        // create a row
        r = s.getRow(rownum+1);

        // create columnList.size() cells (0-(columnList.size()-1))
        for (int cellnum = 0; cellnum < columnList.size(); cellnum++) {
            c = r.getCell(cellnum);
            Object value = resultList.get(rownum).get(columnList.get(cellnum).getColumnKey());
            if (value != null) {
                c.setText(value.toString());
            } else {
                c.setText("");
            }
        }
    }

Would you please help me? Is there a logical way to extend POI (or similar solution) for gaining this feature?


回答1:


A workaround that I found till now is using a template document.

Using this method, you make an empty document that "Normal" style in it, is configured to be RTL. This way, everything in your document will be RTL.

XWPFDocument document = new XWPFDocument(AbstractWordView.class.getClassLoader().getResourceAsStream("empty.docx"));


来源:https://stackoverflow.com/questions/11171777/right-to-left-rtl-text-in-xwpfdocument-apache-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!