Hi Aspose Team,
We’ve encountered an issue while converting DOCX files to PDF using the Aspose.Words.Document.Save
method for .NET. In some cases, the converted PDF has noticeable changes in font, formatting, and page layout. The font size appears to differ, which seems to affect the overall layout of the document. This issue doesn’t happen with all DOCX files, but only with certain ones.
Here’s the code we’re using for the conversion:
public async Task ConvertDocxToPdf(string docxFilePath, string pdfFilePath)
{
Aspose.Words.Document doc = new Aspose.Words.Document(docxFilePath);
doc.Save(pdfFilePath);
await Task.CompletedTask;
}
We have examples of the problematic DOCX files, and we can share them with you via email if needed to help with troubleshooting.
Library Version: 23.7.0 (Aspose.Words for .NET)
Could you please advise on what might be causing this issue and how we can ensure consistent formatting during the conversion process?
Looking forward to your guidance.
@douglasvf
It seems you are experiencing issues with font and formatting changes when converting DOCX files to PDF using Aspose.Words. This can occur due to various reasons, including differences in how fonts are handled during the conversion process.
To ensure consistent formatting during the conversion, you can utilize the PdfSaveOptions
class, which allows you to specify various settings that can affect the output. Here are some suggestions to help you troubleshoot and potentially resolve the issue:
-
Font Embedding: Ensure that fonts are properly embedded in the PDF. By default, Aspose.Words embeds a subset of the fonts used in the document. You can control this behavior using the EmbedFullFonts
property to embed the full fonts instead of just a subset. This can help maintain the original appearance of the document.
var pdfSaveOptions = new Aspose.Words.Saving.PdfSaveOptions
{
EmbedFullFonts = true
};
doc.Save(pdfFilePath, pdfSaveOptions);
-
Use Core Fonts: If your documents use standard fonts (like Arial or Times New Roman), consider setting the UseCoreFonts
property to true. This will prevent these fonts from being embedded, which can reduce file size and maintain consistency across different systems.
pdfSaveOptions.UseCoreFonts = true;
-
Check Document Structure: Sometimes, the layout issues can arise from the document structure itself. Ensure that the problematic DOCX files are well-structured and do not contain any unusual formatting or styles that could affect the conversion.
-
Test with Different PDF Compliance Levels: You can also experiment with different PDF compliance levels using the PdfCompliance
enumeration. This might help in achieving better results depending on the specific requirements of your documents.
pdfSaveOptions.Compliance = Aspose.Words.Saving.PdfCompliance.Pdf17;
-
Update Library Version: Ensure that you are using the latest version of Aspose.Words, as updates often include bug fixes and improvements related to document conversion.
If the issue persists after trying these options, it may be helpful to share the problematic DOCX files with the Aspose support team for further investigation.
By implementing these suggestions, you should be able to achieve more consistent formatting in your PDF outputs.
Sources:
[1]: Convert Word to PDF in C#|Aspose.Words for .NET
[2]: Options When Converting to PDF|Aspose.Words for .NET
@douglasvf Usually, such problems occur because the fonts used in your input document are not available on the machine where document is converted to PDF. The fonts are required to build document layout. If Aspose.Words cannot find the font used in the document, the font is substituted . This might lead into fonts mismatch and document layout differences due to the different fonts metrics. You can implement IWarningCallback to get notifications when font substitution is performed.
Please see our documentation to learn where Aspose.Words looks for fonts:
https://docs.aspose.com/words/net/specifying-truetype-fonts-location/
If after providing the required font the problem still persists, please attach your input and output documents here for testing.