With the new InsertHtml support for MathML in 16.4 it should be possible to use Aspose.Words to create a PNG file from MathML. I’m halfway there but need some help:
Firstly, I do this to get my mathML into a Word document:
var mathMl = @"<math xmlns=""http://www.w3.org/1998/Math/MathML"">123×<mi mathvariant=""normal"">π";
Document doc = new Document();
var builder = new DocumentBuilder(doc);
builder.InsertHtml(mathMl);
Great - works well, now the task is to extract that as an image. The post here:
private static void DocEquationToPng(Document doc, string targetFilename)
{
// Based on code from https://forum.aspose.com/t/55152 DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
NodeCollection equations = doc.GetChildNodes(NodeType.OfficeMath, true);
OfficeMath targetEq = (OfficeMath)equations\[0\];
OfficeMath clonedEq = (OfficeMath)targetEq.Clone(true);
// TODO: The following fails
// enumerator.Current = collector.GetEntity(targetEq);
// Instead here the parent node is used
enumerator.Current = collector.GetEntity(targetEq.ParentNode);
Shape container = new Shape(doc, ShapeType.TextBox);
// TODO: how to set the TextBox size automatically? Currently using:
// container.Width = 1.5 * enumerator.Rectangle.Width;
// container.Height = 1.5 * enumerator.Rectangle.Height;
// is not effective therefore using fixed values for now.
container.Width = 200;
container.Height = 200;
container.TextBox.FitShapeToText = true; // Seems to automatically set width?
Paragraph paragraph = new Paragraph(doc);
paragraph.ParagraphFormat.ClearFormatting();
paragraph.AppendChild(clonedEq);
container.AppendChild(paragraph);
builder.MoveToDocumentStart();
builder.InsertNode(container);
doc.UpdatePageLayout();
container.GetShapeRenderer().Save(targetFilename, new ImageSaveOptions(SaveFormat.Png));
container.Remove();
}
There are problems as marked with TODO. My questions are:
How can we get the correct size to set the text box?
How can we set the size of the output. Currently the equation output is too small.https://forum.aspose.com/t/55152