tblPr要素とその属性tblpXとtblpYを使用してテーブルを配置しようとしています。私の問題は、.docxファイルを開くと、テーブルはページの左上にあり、tblpXとtblPYの値は無視されるということです。 .docxテーブルの配置方法の詳細はhereまたはhereです。それは次のようになります。Apache POI、テーブルの配置が無視されています(.docx、xwpf)
<w:tbl>
<w:tblPr>
<w:tblpPr w:vertAnchor="text" w:tblpY="200" />
</w:tblPr>
…
</w:tbl>
のApache POIは、「tblpY」と「tblpX」属性を提供していませんが、私は、この属性を追加するために考えることができる唯一の方法は手動です。ここではそうする私のコードは:
public static XWPFTable createTable(XWPFDocument doc) {
//CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = doc.createTable();//new XWPFTable(ctTable, doc, 0, 0);
XmlObject x = (XmlObject) table.getCTTbl().getTblPr();
XmlCursor c = x.newCursor(); // Create a cursor at the element
c.toNextToken(); // Move cursor after the tblPr tag
c.insertElement("tblPr", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
c.toPrevSibling(); //Now go to the tblPr
XmlObject x2 = c.getObject(); //Get the tblPr object
c.dispose();
c = x2.newCursor(); //Now our cursor is inside the second tblPr
c.toNextToken();
c.insertAttributeWithValue("tblpX", "http://schemas.openxmlformats.org/wordprocessingml/2006/main", "500");
c.insertAttributeWithValue("tblpY", "http://schemas.openxmlformats.org/wordprocessingml/2006/main", "500");
c.dispose();
XWPFTableRow tr = table.getRow(0);
XWPFTableCell cell = tr.getCell(0);
cell.setText("some text");
return table;
}
私は.docxのを開いて、次はdocument.xml内
<w:tbl>
<w:tblPr>
<w:tblPr w:tblpX="500" w:tblpY="500"/>
<w:tblW w:w="0" w:type="auto"/>
<w:tblBorders>
<w:top w:val="single"/>
<w:left w:val="single"/>
<w:bottom w:val="single"/>
<w:right w:val="single"/>
<w:insideH w:val="single"/>
<w:insideV w:val="single"/>
</w:tblBorders>
</w:tblPr>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>some text</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>
は、だから私は間違って何をやっていることを確認しましたか?テーブルがまだ左上にあるのはなぜですか?なぜ私のtblpX値とtblpY値は無視されますか?
ああ!!!私は解決策を見つけた! –