Ms word 如何在Apache word poi(XWPF)中更改表方向?

Ms word 如何在Apache word poi(XWPF)中更改表方向?,ms-word,apache-poi,direction,Ms Word,Apache Poi,Direction,我正在尝试使用ApachePOIWord3.8以波斯语/阿拉伯语创建word文档 我的问题是:如何更改文档中的表方向?它表示表中列的顺序(而不是表单元格中的文本方向) 例如: 在MS word中,我们可以使用以下方法更改表属性: 如果整个表是双向的,则必须包含双向的设置为ON import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.w

我正在尝试使用ApachePOIWord3.8以波斯语/阿拉伯语创建word文档

我的问题是:如何更改文档中的表方向?它表示表中列的顺序(而不是表单元格中的文本方向)

例如:

在MS word中,我们可以使用以下方法更改表属性:


如果整个表是双向的,则必须包含
双向的
设置为
ON

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.math.BigInteger;

public class CreateWordRTLTable {

 public static void main(String[] args) throws Exception {

  XWPFDocument doc= new XWPFDocument();

  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run = paragraph.createRun();
  run.setText("Paragraph 1 LTR");

  paragraph = doc.createParagraph();

  //create table:
  XWPFTable table = doc.createTable();
  //set the table itself to bidi visual
  if (table.getCTTbl().getTblPr() == null) {
   table.getCTTbl().addNewTblPr().addNewBidiVisual().setVal(STOnOff.ON);
  } else {
   table.getCTTbl().getTblPr().addNewBidiVisual().setVal(STOnOff.ON);
  }
  //create first row
  XWPFTableRow tableRow = table.getRow(0);
  tableRow.getCell(0).setText("first cell");
  tableRow.addNewTableCell().setText("السلام عليكم");
  tableRow.addNewTableCell().setText("third cell");
  //Each cell contains at least a paragraph. Those can be set to support bidi.
  for (int col = 0 ; col < 3; col++) {
   paragraph = tableRow.getCell(col).getParagraphs().get(0);
   CTP ctp = paragraph.getCTP();
   CTPPr ctppr = ctp.getPPr();
   if (ctppr == null) ctppr = ctp.addNewPPr();
   ctppr.addNewBidi().setVal(STOnOff.ON);
  }

  //create CTTblGrid for this table with widths of the 3 columns. 
  //necessary for Libreoffice/Openoffice to accept the column widths.
  //values are in unit twentieths of a point (1/1440 of an inch)
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440));
  for (int col = 1 ; col < 3; col++) {
   table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440));
  }

  //create and set column widths for all columns in all rows
  //most examples don't set the type of the CTTblWidth but this
  //is necessary for working in all office versions
  //values are in unit twentieths of a point (1/1440 of an inch)
  for (int col = 0; col < 3; col++) {
   CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
   tblWidth.setW(BigInteger.valueOf(2*1440));
   tblWidth.setType(STTblWidth.DXA);
   for (int row = 0; row < 1; row++) {
    CTTcPr cttcpr = table.getRow(row).getCell(col).getCTTc().getTcPr();
    if (cttcpr != null) {
     cttcpr.setTcW(tblWidth);
    } else {
     cttcpr = CTTcPr.Factory.newInstance();
     cttcpr.setTcW(tblWidth);
     table.getRow(row).getCell(col).getCTTc().setTcPr(cttcpr);
    }
   }
  }

  paragraph = doc.createParagraph();

  doc.write(new FileOutputStream("WordDocument.docx"));

 }
}
import java.io.FileOutputStream;
导入org.apache.poi.xwpf.usermodel.*;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
导入java.math.biginger;
公共类CreateWordRTLTable{
公共静态void main(字符串[]args)引发异常{
XWPFDocument doc=新XWPFDocument();
XWPFParagraph paragraph paragraph=doc.createParagraph();
XWPFRun=段落.createRun();
run.setText(“第1款LTR”);
段落=doc.create段落();
//创建表:
XWPFTable table=doc.createTable();
//将表格本身设置为bidi visual
if(table.getCTTbl().getTblPr()==null){
table.getCTTbl().addNewTblPr().addNewBidiVisual().setVal(STOnOff.ON);
}否则{
table.getCTTbl().getTblPr().addNewBidiVisual().setVal(STOnOff.ON);
}
//创建第一行
XWPFTableRow tableRow=table.getRow(0);
tableRow.getCell(0.setText(“第一个单元格”);
tableRow.addNewTableCell().setText(tableRow.addNewTableCell().setText);
tableRow.addNewTableCell().setText(“第三个单元格”);
//每个单元格至少包含一个段落。这些段落可以设置为支持bidi。
for(int col=0;col<3;col++){
段落=tableRow.getCell(col).getParations().get(0);
CTP CTP=段落.getCTP();
CTPPr CTPPr=ctp.getPPr();
如果(ctppr==null)ctppr=ctp.addNewPPr();
ctppr.addNewBidi().setVal(STOnOff.ON);
}
//为此表创建宽度为3列的CTTblGrid。
//Libreoffice/Openoffice需要接受列宽。
//数值单位为点的二十分之一(1/1440英寸)
table.getCTTbl().addnewtbgrid().addNewGridCol().setW(biginger.valueOf(2*1440));
for(int col=1;col<3;col++){
table.getCTTbl().getTblGrid().addNewGridCol().setW(biginger.valueOf(2*1440));
}
//为所有行中的所有列创建和设置列宽
//大多数示例不设置CTTblWidth的类型,但
//对于在所有office版本中工作是必需的
//数值单位为点的二十分之一(1/1440英寸)
for(int col=0;col<3;col++){
CTTblWidth tblWidth=CTTblWidth.Factory.newInstance();
tblWidth.setW(BigInteger.valueOf(2*1440));
设置类型(STTblWidth.DXA);
对于(int行=0;行<1;行++){
CTTcPr CTTcPr=table.getRow(row).getCell(col.getCTTc().getcpr();
如果(cttcpr!=null){
cttcpr.setTcW(tblWidth);
}否则{
cttcpr=cttcpr.Factory.newInstance();
cttcpr.setTcW(tblWidth);
table.getRow(row).getCell(col).getCTTc().setTcPr(cttcpr);
}
}
}
段落=doc.create段落();
doc.write(新文件输出流(“WordDocument.docx”);
}
}