问题
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