Images not being rendered properly when converting Docx to html

using var stream = new MemoryStream();
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
    PrettyFormat = true,
    ExportHeadersFootersMode = ExportHeadersFootersMode.PerSection,
    ExportImagesAsBase64 = true,
};

doc.Save(stream, options);
stream.Position = 0;

even if i am exporting the images in base64 , somehow those images are not reflected in the html response , they are somehow coming with weird span styles like lesser z-index and “nbsp” in place of images , please let me know if there are any changes i can do in my code to get all the images properly

@vishika

It sounds like you’re encountering issues with images not rendering correctly when converting a DOCX file to HTML using Aspose.Words. Here’s a structured approach to address the problem:

Cause: The issue may stem from unsupported image formats, outdated library versions, or incorrect HTML export settings.

Solution: Follow these steps to troubleshoot and resolve the issue:

  1. Check Image Format: Ensure that the images in your DOCX file are in a supported format. Aspose.Words supports various image formats, and unsupported formats may not render correctly.

  2. Update Aspose.Words: Verify that you are using the latest version of Aspose.Words. Newer releases often include bug fixes that could resolve your issue.

  3. Adjust HtmlSaveOptions: Experiment with different settings in HtmlSaveOptions. For example, set ExportImagesAsBase64 to false to export images as separate files instead of embedding them in the HTML.

  4. Inspect the Output HTML: After saving the document, closely inspect the generated HTML. Look for <img> tags and check their src attributes. If they do not point to valid image data, that could be the source of the problem.

  5. Debugging: If the issue persists, log the output of the MemoryStream to ensure the images are being processed correctly before saving.

Here’s a minimal code snippet to illustrate how you might adjust the HtmlSaveOptions:

var stream = new MemoryStream();
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
    PrettyFormat = true,
    ExportHeadersFootersMode = ExportHeadersFootersMode.PerSection,
    ExportImagesAsBase64 = false, // Try setting this to false
};

doc.Save(stream, options);
stream.Position = 0;

Next Steps: Implement the suggested changes and test the output. If the problem persists, please provide additional details about the images and the specific behavior you are observing.

Let me know if these steps help or if you need further assistance!

@vishika Could you please attach your problematic input and output documents here for our reference? We will check the issue and provide you more information.

You should note, that HTML documents and MS Word documents object models are quite different and it is not always possible to provide 100% fidelity after conversion one format to another.

PS: If the output HTML is for viewing purposes, i.e. it is not supposed to be edited or processed, you can consider using HtmlFixed format. In this case the output should look exactly the same as it looks in MS Word:

Document doc = new Document(@"C:\Temp\in.docx");

HtmlFixedSaveOptions opt = new HtmlFixedSaveOptions();
opt.PrettyFormat = true;
opt.ExportEmbeddedCss = true;
opt.ExportEmbeddedFonts = true;
opt.ExportEmbeddedImages = true;
opt.ExportEmbeddedSvg = true;

doc.Save(@"C:\Temp\out_html_fixed.html", opt);

HtmlFixed format is designed to preserve original document layout for viewing purposes. So if your goal is to display the HTML on page, then this format can be considered as an alternative. But unfortunately, it does not support roundtrip to DOCX at all.