using var stream = new MemoryStream();
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
PrettyFormat = true,
ExportHeadersFootersMode = ExportHeadersFootersMode.PerSection,
ExportImagesAsBase64 = true,
ExportFontResources = true,
ExportFontsAsBase64 = true,
CssStyleSheetType = CssStyleSheetType.Inline
};
this method is not working
@vishika
To ensure that the proper fonts are included when converting a DOCX file to HTML using Aspose.Words for .NET, follow these steps:
Cause: The issue may arise if the font resources are not correctly configured or if the fonts used in the document are not available on the system during conversion.
Solution: Make sure that the HtmlSaveOptions
are correctly configured. Here are the key settings you need to verify:
-
ExportFontResources: Set this option to true
. This allows the fonts used in the document to be exported.
-
ExportFontsAsBase64: This option should also be set to true
, ensuring that the font resources are embedded directly in the HTML as Base64 strings.
-
Check Font Availability: Ensure that the fonts used in your DOCX document are installed on the system where the conversion is taking place. If the fonts are not available, Aspose may not be able to embed them properly.
-
Use the Correct Save Method: Ensure you are using the Document.Save
method with the configured HtmlSaveOptions
. Here’s a sample code snippet:
using Aspose.Words;
using Aspose.Words.Saving;
using System.IO;
var doc = new Document("input.docx");
var stream = new MemoryStream();
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
PrettyFormat = true,
ExportHeadersFootersMode = ExportHeadersFootersMode.PerSection,
ExportImagesAsBase64 = true,
ExportFontResources = true,
ExportFontsAsBase64 = true,
CssStyleSheetType = CssStyleSheetType.Inline
};
doc.Save(stream, options);
- Inspect the Output: After saving, inspect the generated HTML to confirm that the font resources are included. You should see
<style>
tags with Base64 encoded font data.
Next Steps: Implement the above configurations and run the conversion again. If the fonts are still not included, check the availability of the fonts on your system and ensure that the document is using the correct font names.
If you follow these steps, you will successfully include the proper fonts in your HTML output. Please let me know if you need further assistance!
@vishika Could you please elaborate what exactly does not work? 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.
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.