Document doc = new Document(“template.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.writeln(“test.”);
SaveOptions saveOptions = SaveOptions.createSaveOptions(SaveFormat.PDF);
saveOptions.setMemoryOptimization(true);
doc.save(“template.pdf”, saveOptions);
docx文件保存为pdf文件后,左侧的表格内容格式出现错误,该怎么处理?
template.docx (191.4 KB)
template.pdf (323.0 KB)
@dhzhao2016
Summary
The table on the left side of the PDF generated from template.docx is mis‑aligned. This is most often caused by font substitution (missing fonts on the conversion machine) or by the document layout not being refreshed before the PDF is saved.
Why it happens
- Font substitution – When the fonts referenced in the DOCX are not available to Aspose.Words, the library substitutes fallback fonts. Different character widths can shift table columns, producing the observed mis‑alignment.
- Stale layout cache – If the layout is not recomputed just before
save(), especially when PdfSaveOptions.setMemoryOptimization(true) is used, Aspose.Words may render the page using an outdated layout, which can affect tables.
What to do next
- Detect font‑substitution warnings
// Register a warning callback to capture font‑substitution messages
doc.setWarningCallback(new IWarningCallback() {
@Override
public void warning(WarningInfo info) {
if (info.getWarningType() == WarningType.FONT_SUBSTITUTION) {
System.out.println("Font substitution: " + info.getDescription());
}
}
});
Run the conversion again and watch the console for FONT_SUBSTITUTION warnings.
- Make the required fonts available
- Install the fonts on the server so the Java process can locate them, or
- Supply a custom font folder:
FontSettings fontSettings = new FontSettings();
fontSettings.setFontsFolder("C:/MyFonts", true); // true → search sub‑folders
doc.setFontSettings(fontSettings);
- Embed the fonts in the PDF (optional but helps viewers that lack the fonts):
PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.setEmbedFullFonts(true);
- Force a layout refresh before saving
doc.updatePageLayout(); // recompute layout
doc.save("template.pdf", pdfOptions);
- Test without memory optimisation (temporarily)
pdfOptions.setMemoryOptimization(false);
doc.save("template.pdf", pdfOptions);
If the PDF renders correctly, the optimisation flag may be interacting with the layout cache for this document.
-
Verify the PDF in different viewers
Open the resulting file in several PDF viewers (e.g., Adobe Acrobat Reader, a web browser) to ensure the viewer itself is not causing additional font substitution.
-
Gather additional information if the issue persists
- Aspose.Words for Java version you are using.
- Operating system and Java runtime details.
- List of custom fonts referenced in template.docx (you can inspect
word/fontTable.xml inside the DOCX).
Next‑step checklist
- Add the
IWarningCallback code and run the conversion → note any FONT_SUBstitution warnings.
- Provide the missing fonts via
FontSettings.setFontsFolder or install them on the server.
- Call
doc.updatePageLayout() before doc.save().
- Disable
PdfSaveOptions.setMemoryOptimization(true) to see if it influences the result.
- Re‑test the PDF in multiple viewers.
If the table is still mis‑aligned after completing these actions, please provide the Aspose.Words for Java version and the list of fonts used in the template so the issue can be examined further.
I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.
上述方式都无法解决此问题。Aspose.word for java 的运行版本是 25.9
之前一直使用的是 Aspose.word 20.2的版本,这个版本保存的pdf文件就没有问题。运行环境都一样。
template.pdf (319.1 KB)
@dhzhao2016 请检查25.10版本。使用此版本时,我这边没有遇到表格问题。