请问怎么修改表格属性的 单元格边距

请问怎么修改表格属性的 单元格边距
怎么将表1格式改为 表2 的格式
变化主要在单元格边距
表1.docx (21.7 KB)

@ouchli 您可以尝试使用以下代码更改单元格边距:

Document doc = new Document("input.docx");

Table table1 = (Table) doc.getChild(NodeType.TABLE, 0, true);
Table table2 = (Table) doc.getChild(NodeType.TABLE, 1, true);

for (Row row1 : table1.getRows()) {
    for (Cell cell1 : row1.getCells()) {
        // 在表2中获取相应的单元格。
        Row row2 = table2.getRows().get(row1.getParentTable().indexOf(row1));
        Cell cell2 = row2.getCells().get(row1.indexOf(cell1));

        // 将单元格边距从表1复制到表2。
        cell2.getCellFormat().setTopPadding(cell1.getCellFormat().getTopPadding());
        cell2.getCellFormat().setBottomPadding(cell1.getCellFormat().getBottomPadding());
        cell2.getCellFormat().setLeftPadding(cell1.getCellFormat().getLeftPadding());
        cell2.getCellFormat().setRightPadding(cell1.getCellFormat().getRightPadding());
    }
}

doc.save("output.docx");