在将 Word 转为 PDF 时,Aspose.Words 会尝试根据 PDF 的页面布局自动调整文本的换行。如果您希望保持单元格内容的原始格式,可以尝试使用以下方法来禁用自动换行:

  1. 在转换 Word 文档为 PDF 之前,使用 Aspose.Words API 设置单元格的自动换行属性为 false。例如:
Document doc = new Document('input.docx');
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
Cell cell = table.getFirstRow().getFirstCell();
cell.getCellFormat().setWrapText(false);

doc.save('output.pdf', SaveFormat.PDF);
  1. 如果上述方法无效,您可以尝试在 Word 文档中使用硬换行符'\n' 来代替自动换行。在转换为 PDF 之前,将'\n' 替换为空格或其他字符。例如:
Document doc = new Document('input.docx');
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
Cell cell = table.getFirstRow().getFirstCell();
String cellText = cell.toString(SaveFormat.TEXT).replace('\n', ' ');
cell.removeAllChildren();
cell.appendChild(new Run(doc, cellText));

doc.save('output.pdf', SaveFormat.PDF);

这样,转换为 PDF 后,单元格的内容就不会换行了。

请注意,以上代码仅为示例,您需要根据自己的实际情况进行调整。

Aspose.Words 21.1.jar 转换 Word 为 PDF 时单元格换行问题解决方案

原文地址: http://www.cveoy.top/t/topic/pdnm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录