aspose-words14.9.0 word转pdf 表格某列宽度变窄了 关闭列宽自适应
时间: 2023-11-11 20:13:51 浏览: 195
如果你不希望表格列宽自适应大小,可以尝试使用以下代码:
```
Document doc = new Document("input.docx");
TableCollection tables = doc.getFirstSection().getBody().getTables();
for (Table table : tables) {
for (Row row : table.getRows()) {
for (Cell cell : row.getCells()) {
cell.getCellFormat().setShading(null);
cell.getCellFormat().setWidth(100); // 设置单元格的宽度为100
}
}
}
doc.save("output.pdf");
```
这将遍历表格的所有单元格,并将其宽度设置为100,从而避免了表格列宽自适应大小的问题。如果问题仍然存在,请提供更多的详细信息,以便我们更好地帮助你解决问题。
相关问题
aspose-words14.9.0 word转pdf 表格某列宽度变窄了
如果使用aspose-words14.9.0将Word转换为PDF时,表格某列的宽度变窄了,你可以尝试使用以下代码:
```
Document doc = new Document("input.docx");
TableCollection tables = doc.getFirstSection().getBody().getTables();
for (Table table : tables) {
// 计算表格宽度和页面宽度的比率
double tableWidth = table.getPreferredWidth().getValue();
double pageWidth = doc.getPageSetup().getPageWidth() - doc.getPageSetup().getLeftMargin() - doc.getPageSetup().getRightMargin();
double ratio = pageWidth / tableWidth;
// 缩放表格的宽度,以适应页面大小
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
table.setPreferredWidth(PreferredWidth.fromPoints(table.getPreferredWidth().getValue() * ratio));
}
doc.save("output.pdf");
```
这将比较表格的宽度和页面宽度,并计算它们的比率。然后,使用`table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW)`缩放表格的宽度,以适应页面大小。最后,使用`table.setPreferredWidth`方法将表格的宽度设置为缩放后的值,从而避免了表格宽度被修改的问题。如果问题仍然存在,请提供更多的详细信息,以便我们更好地帮助你解决问题。
aspose-words14.9.0 转换word至pdf 表格某列宽度被修改了
如果使用最新版本的Aspose.Words不能解决问题,你可以尝试使用以下代码:
```
Document doc = new Document("input.docx");
TableCollection tables = doc.getFirstSection().getBody().getTables();
for (Table table : tables) {
double tableWidth = table.getPreferredWidth().getValue();
double pageWidth = doc.getPageSetup().getPageWidth() - doc.getPageSetup().getLeftMargin() - doc.getPageSetup().getRightMargin();
if (tableWidth > pageWidth) {
// 如果表格宽度大于页面宽度,将表格宽度设置为页面宽度减去一点间距。
table.setPreferredWidth(PreferredWidth.fromPoints(pageWidth - 10)); // 10是间距
}
}
PdfSaveOptions options = new PdfSaveOptions();
options.setCompliance(PdfCompliance.PDF_A_1_B);
doc.save("output.pdf", options);
```
这将比较表格的宽度和页面宽度,并将表格宽度设置为页面宽度减去一点间距,以适应页面大小,并使用PDF/A-1b兼容性保存PDF文件,从而避免了表格宽度被修改的问题。如果问题仍然存在,请提供更多的详细信息,以便我们更好地帮助你解决问题。