Is it possible to convert Officemath to Image through API?

Hi ,

I am using Aspose latest version for document conversion. Is it possible to save officemath ( Equations ) as image using Aspose API ?

Hi Anbu,

Thanks for your inquiry.

Unfortunately, there is no way to convert a particular OfficeMath equation in the document to image. As a workaround, you can copy the particular node(s) into an empty document and then convert this document to Image. You can also export this temporary document to HTML format and catch the images in IImageSavingCallback.ImageSaving method. You can also insert OfficeMath equation inside a temporary TextBox Shape and then render that Shape to an individual raster or vector image or to a Graphics object using ShapeRenderer Class.

Please let me know if I can be of any further assistance.

Best regards,

You say " You can also insert OfficeMath equation inside a temporary TextBox Shape and then render that Shape to an individual raster or vector image or to a Graphics object using ShapeRenderer Class.".

DocumentBase doc = officeMath.Document;
Shape container = new Shape(doc, ShapeType.TextBox);
officeMath.ParentNode.InsertAfter(container, officeMath);
Paragraph paragraph = new Paragraph(doc);
container.AppendChild(paragraph);
paragraph.AppendChild(officeMath.Clone(true));
container.TextBox.FitShapeToText = true;

I’ve been trying to achieve this but i only get an small empty image. Can you maybe provide working sample code for this technique?

Just tried the other method you mentioned with the following code:

Document tmpDoc = new Document();
tmpDoc.ImportNode(officeMath, true, ImportFormatMode.KeepSourceFormatting);
tmpDoc.Save("D:\Test.png", SaveFormat.Png);

But this results in only a big white (and empty) image.
The attachment contains a document i use for testing, at the bottom half you’ll see the math-object i’m trying to render.

Hi Anbu,

Thanks for the additional information. Please try running the following code for rendering a OfficeMath equation to image format:

Document doc = new Document(@"C:\Temp\NPI-249-The+Importer+Image+Challenge+2.docx");
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);
enumerator.Current = collector.GetEntity(targetEq);
Shape container = new Shape(doc, ShapeType.TextBox);
container.Width = 1.5 * enumerator.Rectangle.Width;
container.Height = 1.5 * enumerator.Rectangle.Height;
container.TextBox.FitShapeToText = true;
Paragraph paragraph = new Paragraph(doc);
paragraph.ParagraphFormat.ClearFormatting();
paragraph.AppendChild(clonedEq);
container.AppendChild(paragraph);
builder.MoveToDocumentStart();
builder.InsertNode(container);
doc.UpdatePageLayout();
container.GetShapeRenderer().Save(@"C:\Temp\out.png", new ImageSaveOptions(SaveFormat.Png));
container.Remove();

I hope, this helps.

Best regards,