我需要在特定书签的位置创建一个表。即我需要找到书签并插入表格。我如何使用 docx4j 来做到这一点在此先感谢
抱歉 Jason,我是 Stackoverflow 的新手,所以我无法清楚地写出我的问题,这是我的情况和问题。
我按照您的建议和我的需要对该代码进行了更改,代码在这里
//loop through the bookmarks
for (CTBookmark bm : rt.getStarts()) {
// do we have data for this one?
String bmname =bm.getName();
// find the right bookmark (in this case i have only one bookmark so check if it is not null)
if (bmname!=null) {
String value = "some text for testing run";
//if (value==null) continue;
List<Object> theList = null;
//create bm list
theList = ((ContentAccessor)(bm.getParent())).getContent();
// I set the range as 1 (I assume this start range is to say where the start the table creating)
int rangeStart = 1;
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
// create the table
Tbl table = factory.createTbl();
//add boards to the table
addBorders(table);
for(int rows = 0; rows<1;rows++)
{// create a row
Tr row = factory.createTr();
for(int colm = 0; colm<1;colm++)
{
// create a cell
Tc cell = factory.createTc();
// add the content to cell
cell.getContent().add(wordPackage.getMainDocumentPart()
.createParagraphOfText("cell"+colm));
// add the cell to row
row.getContent().add(cell);
}
// add the row to table
table.getContent().add(row);
// now add a run (to test whether run is working or not)
org.docx4j.wml.R run = factory.createR();
org.docx4j.wml.Text t = factory.createText();
run.getContent().add(t);
t.setValue(value);
//add table to list
theList.add(rangeStart, table);
//add run to list
//theList.add(rangeStart, run);
}
我不需要删除书签中的文本,所以我删除了它。我不知道是什么问题,程序正在编译,但我无法打开 doc 一词,它显示“未知错误”。我测试写一些字符串“值”,它完美地写在那个书签和文档正在打开但不是在表格的情况下。请帮助我提前谢谢