Java 生成的word,然后保存为pdf,结果表格宽度就全都失效了

通过aspose-word java生成了word,宝行表格,然后指定table的偏移量,设置了table的宽度

for(Row row : table.getRows()) {
    for(Cell cell : row.getCells()) {
        //Calculate the ratio of each cell to the row width and then translate this ratio to the page width.
        double cellRatio = cell.getCellFormat().getWidth() / tableWidth;
        cell.getCellFormat().setWidth(cellRatio * pageWidth);
        //cell.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(cellRatio * pageWidth));
    }
}

File wordFile = new File(wordPath);
sourceDocument.save(wordFile.getPath(),SaveFormat.PDF);


测试偏移.docx (31.0 KB)

@yiranzhiyuan 不幸的是,我无法用以下代码重现我这边的问题:

Document doc = new Document("input.docx");
double pageWidth = doc.getFirstSection().getPageSetup().getPageWidth();

NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
for (Table table : (Iterable<Table>) tables) {
    double tableWidth =  table.getPreferredWidth().getValue();
    for (Row row : table.getRows()) {
        for (Cell cell : row.getCells()) {
            //Calculate the ratio of each cell to the row width and then translate this ratio to the page width.
            double cellRatio = cell.getCellFormat().getWidth() / tableWidth;
            cell.getCellFormat().setWidth(cellRatio * pageWidth);
            //cell.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(cellRatio * pageWidth));
        }
    }
}

doc.save("output.docx");

这是我的输出文件:
output.docx (22.5 KB)