0

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?

j0k
  • 22,600
  • 28
  • 79
  • 90
Mohammad Dashti
  • 745
  • 1
  • 9
  • 22

1 Answers1

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"));
Mohammad Dashti
  • 745
  • 1
  • 9
  • 22
  • how can I set UTF-8 , by default when I convert word to pdf , Pdf text is spoiled, such as "ب ی ت ر ت" instead of "ترتیب" I used this code : options.fontEncoding("UTF-8") But instead of making it right. All letters disappeared – Saeed Aliakbari Jul 09 '17 at 16:10
  • 1
    Your issue is not related to this question, as it's about Docx format. I don't know about PDF using Apache POI. – Mohammad Dashti Jul 09 '17 at 19:37