Export OfficeMath as SVG in HTML

Is it possible to convert docx to HTML with OfficeMath as SVG? Could you please mention the way of doing that?

@maaaruf.osl Unfortunately, currently there is not direct way to export OfficeMath equations as SVG while saving document to HTML. I have created a feature request for this WORDSNET-23802. We will let you know once this feature is available. Currently OfficeMath can be exported too HTML as Image, MathML or Text, see HtmlSaveOptions.OfficeMathOutputMode for more information.
However, it is also possible to render OfficeMath as Metafile and then instruct Aspose.Words to save metafile as SVG to HTML. So you can implement a workaround to achieve what you need. For example see the following code:

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

ImageSaveOptions emfSaveOptions = new ImageSaveOptions(SaveFormat.Emf);

NodeCollection maths = doc.GetChildNodes(NodeType.OfficeMath, true);
List<OfficeMath> mathsToRemove = new List<OfficeMath>();
foreach (OfficeMath m in maths)
{
    // Render only root OfficeMath nodes
    if (m.ParentNode.NodeType != NodeType.OfficeMath)
    {
        MemoryStream emfMath = new MemoryStream();
        RectangleF mathBounds = m.GetMathRenderer().OpaqueBoundsInPoints;
        m.GetMathRenderer().Save(emfMath, emfSaveOptions);
        emfMath.Position = 0;

        // Create a shape with metafile image.
        Shape mathShape = new Shape(doc, ShapeType.Image);
        mathShape.ImageData.SetImage(emfMath);
        mathShape.Width = mathBounds.Width;
        mathShape.Height = mathBounds.Height;
        mathShape.WrapType = WrapType.Inline;

        // Insert shape right after OfficeMath.
        m.ParentNode.InsertAfter(mathShape, m);
        mathsToRemove.Add(m);
    }
}

foreach (OfficeMath m in mathsToRemove)
    m.Remove();

HtmlSaveOptions options = new HtmlSaveOptions();
options.MetafileFormat = HtmlMetafileFormat.Svg;
options.PrettyFormat = true; // For testing purposes
doc.Save(@"C:\Temp\out.html", options);
1 Like

Thanks a lot, it properly works in my case.

Previously I tried to convert with OfficeMathOutputMode = HtmlOfficeMathOutputMode.MathML as you said. But the brackets and vertical line of matrix gone missing from OfficeMath equations. For that reason I tried with SVG, Is there any way to keep those brackets and vertical line on place?

Word Input was like this:

HTML output I got:

Word File:
01.docx (12.7 KB)

Code:

string _FileName = Path.GetFileName(file.FileName);
string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);
file.SaveAs(_path);

// Load a Word file from the local drive.
var doc = new Document(_path);

string htmlPath = Path.Combine(Server.MapPath($"~/UploadedFiles/HTML/{_FileName}"), $"{_FileName}");
// Save it to HTML format.
//doc.Save(htmlPath + ".html", new HtmlSaveOptions { OfficeMathOutputMode = HtmlOfficeMathOutputMode.MathML });

@maaaruf.osl Thank you for additional information. This problem has been logged as WORDSNET-23804. We will keep you informed and let you know once it is resolved.