docx转换PDF后,标题行换行了,但是在word中直接导出不会出现这个情况。
new.docx (11.1 KB)
您遇到将 docx 转换为 PDF 时标题出现换行的问题。
目前我们尚未在内部复现此现象,需进一步确认。为帮助我们定位并记录该问题,请提供以下信息:
有了这些细节后,我们可以在此线程中继续跟进并尝试复现问题。谢谢!
现象看图片,转换后的文件是
new.pdf (8.3 KB)
aspose.word for java 22.1会出现这个情况,最新版26.1也有这个情况;
最小代码:
private static void convertPdf() {
String inputFile = "C:\\Users\\Admin\\Desktop\\new.docx";
String outputFile = "C:\\Users\\Admin\\Desktop\\new.pdf";
Document doc;
try {
doc = new Document(inputFile);
doc.save(outputFile, SaveFormat.PDF);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
您在使用 Aspose.Words for Java(22.1 与 26.1)将 DOCX 转 PDF 时,标题在 PDF 中出现了意外换行。我们目前尚未在内部复现此现象,需要更多细节才能定位根本原因【1】。
请您提供以下信息(并在可能的情况下上传原始 .docx 文件):
ParagraphFormat.setKeepTogether(true))。可尝试的快速排查步骤(在确认上述信息后可执行):
Document doc = new Document("new.docx");
// 示例:确保标题段落保持在同一页
Style heading = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_1);
heading.getParagraphFormat().setKeepTogether(true);
// 如有手动换行符,可在保存前删除
for (Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
String txt = para.getText().replace("\r", "").replace("\n", " ");
para.getRange().replace(para.getText(), txt);
}
// 更新布局后保存
doc.updatePageLayout();
doc.save("new.pdf", SaveFormat.PDF);
如果上述检查后仍出现换行,请将文档和 PDF 附件回复到本线程,我们将在此处继续跟进。谢谢!