Document doc = new Document(“test.docx”);
SaveOptions saveOptions = SaveOptions.createSaveOptions(SaveFormat.PDF);
saveOptions.setMemoryOptimization(true);
doc.save(“test.pdf”, saveOptions);
Aspose.word for java 25.11. Word文档保存为pdf文件后,英文内容超出了文档边框,该怎么处理?
test.docx (34.5 KB)
test.pdf (64.0 KB)
@dhzhao2016
Summary
When converting a DOCX to PDF with the generic SaveOptions, the English text can appear outside the page margins.
Why it happens
- The conversion relies on the fonts that are available on the machine where the code runs. If a font used in the DOCX is missing, Aspose.Words substitutes it with a fallback font, which may have different character widths and cause layout overflow.
- Enabling memory‑optimization (
setMemoryOptimization(true)) can affect layout calculations in some scenarios, especially when font substitution occurs.
What to do next
- Check the fonts used in the source document
- Open test.docx in Microsoft Word and note the exact font names applied to the English text.
- Make sure those font files are installed on the server/PC where the Java code runs, or place them in a folder and add that folder to the Aspose.Words font search path, e.g.:
FontSettings.getDefaultInstance().setFontsFolder("/path/to/fonts", true);
- Try the conversion without memory optimization
Document doc = new Document("test.docx");
SaveOptions saveOptions = SaveOptions.createSaveOptions(SaveFormat.PDF);
saveOptions.setMemoryOptimization(false); // disable for testing
doc.save("test.pdf", saveOptions);
Verify whether the text stays within the margins. If it does, the issue is related to the memory‑optimization mode.
3. Use PDF‑specific save options if available
- If the library version you use provides
PdfSaveOptions, create an instance of it instead of the generic SaveOptions and enable font embedding (e.g., setEmbedFullFonts(true)). This helps keep the original layout.
- Enable logging to detect font substitution
java.util.logging.Logger logger = java.util.logging.Logger.getLogger("com.aspose.words");
logger.setLevel(java.util.logging.Level.FINE);
Look for messages about missing fonts; they indicate which fonts need to be installed.
5. If the problem persists
- Double‑check the document for any custom paragraph formatting (negative indents, oversized spacing) that could also cause overflow.
- Provide the font information and any log output to Aspose support for further analysis.
These steps should help you identify whether missing fonts or the memory‑optimization setting are causing the layout issue and allow you to correct it.
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 V25.11
Word中没有特殊字体,进行文档转换的机器中也有对应的字体。
将saveOptions.setMemoryOptimization(false); 设置为false。
保存为的pdf文件,英文内容仍然超出边框。还有什么办法?
@dhzhao2016 文档处于兼容模式,这意味着它可能有一些不受支持的样式。请使用以下代码以避免问题:
Document doc = new Document("input.docx");
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);
doc.save("output.pdf");
经测试,是Word文档兼容模式的问题,已解决,谢谢!
1 Like