Hi Aspose support,
In our product we have functionality that converts equations’ content to images and replace OMathPara/OMath node with resulted image shape.
Similar to what MS Word do when converts DOCX document with equations into DOC/RTF format.
After testing this approach we were faced with the issue when an Equation has image/OLE object inside OMath/OMathPara node, after conversion the node into EMF images/OLE objects are missed from resulted image.
Please see sample documents and screenshots of the problem:
Images inside OMath:
Equations with images.zip (2.3 MB)
See results:
OMathWithImages.png (133.2 KB)
Converted by Aspose document:
Equations with images_Converted.zip (10.8 KB)
OLE objects inside OMath:
Equations with OLE objects.zip (992.9 KB)
See results:
OMathWithOLE.png (40.6 KB)
Converted by Aspose document:
Equations with OLE objects_Converted.zip (18.4 KB)
Expected behavior for both cases - document should look similar as it looks after resaving DOCX into DOC by Word (Word converts Equations into images).
Codes:
private void ReplaceFormulas(Document doc, ImageSaveOptions options, DocumentBuilder builder)
{
NodeCollection mathCollection = doc.GetChildNodes(NodeType.OfficeMath, true);
// Loop from last one to first one
for (int i = mathCollection.Count - 1; i >= 0; i--)
{
try
{
OfficeMath math = (OfficeMath)mathCollection[i];
// if node is MathPara or node is Inline Math with parent which is not Math Node, then replace with image
// Needs to differentiate inline and display formulas
if ((math.MathObjectType == MathObjectType.OMathPara) ||
((math.MathObjectType == MathObjectType.OMath) && (math.ParentNode?.NodeType != NodeType.OfficeMath)))
{
using (MemoryStream stream = new MemoryStream())
{
math.GetMathRenderer().Save(stream, options);
SizeF mathSize = math.GetMathRenderer().SizeInPoints;
builder.MoveTo(math);
Shape imageShape = builder.InsertImage(stream, mathSize.Width, mathSize.Height);
math.Remove();
}
}
}
catch
{
// Ignore exception and continue process math collection
}
}
}
The code which calls this method:
public void Replace()
{
try
{
// Save Equations into EMF format as optimal format for them
ImageSaveOptions optionsEmf = new ImageSaveOptions(SaveFormat.Emf);
optionsEmf.MetafileRenderingOptions.ScaleWmfFontsToMetafileSize = true;
optionsEmf.MetafileRenderingOptions.RenderingMode = MetafileRenderingMode.Vector;
optionsEmf.UseGdiEmfRenderer = false;
DocumentBuilder builder = new DocumentBuilder(_doc);
ReplaceFormulas(_doc, optionsEmf, builder);
}
catch (Exception e)
{
throw new DrawingReplacerException(e.Message);
}
}
Tested on Aspose.Words.dll v19.8.
Could you help with solving this problem?
The case when an Equation has only Image inside can be solved on our side (by replacing objects), but if there are both and OMath and Shape objects inside equation, we cannot solve it. So, ideally it should be handled in Aspose, as it’s done in MS Word.
Thanks in advance.