Font not preserverd when converting docx to pdf

Hello,
when we try to convert docx file to pdf file, we have some problems.

  1. The font isn’t the same;
  2. We have an image in the docx file but it’s not present in the pdf file

In addition, we already installed the latest version of Aspose.Words (25.1, January 2025)
Can you help us with this problem?

Thank you in advance.

@clusterreply

To address the issues you’re experiencing with font preservation and image conversion when converting a DOCX file to PDF using Aspose.Words, you can utilize specific options in the PdfSaveOptions class.

1. Font Preservation

To ensure that the fonts are preserved during the conversion, you can set the EmbedFullFonts property to true. This will embed all fonts found in the document into the PDF, which helps maintain the original appearance of the text.

Here’s a code snippet demonstrating how to do this:

using Aspose.Words;
using Aspose.Words.Saving;

Document doc = new Document("input.docx");
PdfSaveOptions saveOptions = new PdfSaveOptions
{
    EmbedFullFonts = true // Embed all fonts
};

doc.Save("output.pdf", saveOptions);

2. Image Preservation

If images from the DOCX file are not appearing in the PDF, ensure that the images are correctly embedded in the document. You can also check the MetafileRenderingOptions to handle metafiles properly, which can sometimes affect image rendering.

Here’s how you can set up the MetafileRenderingOptions:

MetafileRenderingOptions metafileRenderingOptions = new MetafileRenderingOptions
{
    EmulateRasterOperations = false, // Set to true if you want to emulate raster operations
    RenderingMode = MetafileRenderingMode.VectorWithFallback // Use vector graphics with fallback to bitmap if necessary
};

PdfSaveOptions saveOptions = new PdfSaveOptions
{
    MetafileRenderingOptions = metafileRenderingOptions
};

doc.Save("output.pdf", saveOptions);

Summary

By embedding fonts and configuring the metafile rendering options, you should be able to resolve the issues with font and image preservation during the DOCX to PDF conversion. Make sure to test these settings with your specific documents to ensure they meet your requirements.

If you continue to experience issues, consider checking the Aspose documentation or community forums for additional troubleshooting tips.

Sources

We don’t have to save the PDF but we have to pass the byte array of the PDF to the server.

@clusterreply

  1. The problem might occur because fonts required to render the document are not availabe. 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/

  2. Most likely your application is deployed to Linux server. Aspose.Words for .NET Standard and .NET Core uses SkiaSharp to deal with graphics, to make it work on Linux you have to add reference either to SkiaSharp.NativeAssets.Linux or to SkiaSharp.NativeAssets.Linux.NoDependencies

    If you add reference to SkiaSharp.NativeAssets.Linux, you should also install libfontconfig1 in your system. SkiaSharp.NativeAssets.Linux depends on this library. You can use the following command to install it:

    apt-get update && apt-get install -y libfontconfig1
    

    If you do not have rights to install packages, or other reasons not to install libfontconfig1, you can simply use SkiaSharp.NativeAssets.Linux.NoDependencies , which does not require installation of libfontconfig1.

    Currently it is recommended to use 2.88.x version of SkiaSharp. 3.x.x version of SkiaSharp has breaking API chnages that are not compatible with current version of Aspose.Words.

    <PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.8" />