none
Math and Equation Editor Equations - Unable to Save as WMF RRS feed

  • Question

  • Hi All,

    I'm new to these technologies and I struck up with automating the process of Export Equations from Word Doc file. To do so, I used OpenXML SDK in C#. And I found some difficulties & referred some guides and come across the discussion which was very helpful for me. It was lying in Google Search :how-to-read-equations-from-wordembeddings-folder(unable to include link here). When I tried using the code it works like a charm. But while testing some other docx files, some equations are old i.e created by Equation editor 3(earlier tested sample docx files has Equaion.DSMT4, having no issue) and the WMF files generated was empty data contents. 

    If I used to open the Docx file and double click the equation which has been opened in Mathtype and simply trigger the Update "xxx.docx" from file menu. Saved the document. Again run, the equation was perfect in WMF. And understood it's due to not updated of the old equation format to mathtype. But I'm in need to do all in fly. 

    It ate almost 2 days of my time. Still no clue on how to achieve it. Could you please guide me on this?

    I've tried

    using (WordprocessingDocument doc = WordprocessingDocument.Open(convertedDocx, true)) { MainDocumentPart mainDoc = doc.MainDocumentPart; Document document = mainDoc.Document; int i = 0; String prefix = @"$dm_"; String eqnPrefix = "Eqn"; foreach(DocumentFormat.OpenXml.Wordprocessing.Paragraph p in document.Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>()) { foreach (Ovml.OleObject oleObject in p.Descendants<Ovml.OleObject>()) { if (oleObject.ProgId == "Equation.DSMT4" || oleObject.ProgId == "Equation.3") { foreach (V.Shape shape in document.Descendants<V.Shape>()) { if (shape.Id.Value == oleObject.ShapeId.Value) { if (p.InnerText.ToString().Trim().Split(' ').Length <= 5) { prefix = @"$dm_"; } else { prefix = @"$im_"; } i++; String eqnCount = i.ToString(); while (eqnCount.Length < 3) { eqnCount = "0" + eqnCount; } V.ImageData datas = shape.Descendants<V.ImageData>().FirstOrDefault(); var id = datas.RelationshipId; var imagePart = mainDoc.GetPartById(id.Value); Stream data = imagePart.GetStream(FileMode.Open, FileAccess.Read); //export the image

    //(Some stuff needed here to handle Equation.3) using (Stream file = File.Create(targetpath + eqnPrefix + eqnCount + ".wmf")) { CopyStream(data, file); } //shape.OptionalString = prefix + eqnName + (i).ToString(); shape.Parent.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text(prefix + eqnPrefix + eqnCount + ".wmf")); //shape.InnerXml = prefix + eqnName + (i).ToString(); shape.Remove(); } } } } } doc.MainDocumentPart.Document.Save(); //using (FileStream fileStream = new FileStream(clearedDoc, FileMode.Create)) //{ // doc.MainDocumentPart.Document.Save(fileStream); //} }


    Thanks & Regards,

    K. Velprakash

    Friday, April 28, 2017 5:37 AM

All replies

  • Hello,

    Are your documents created by Office for Mac?  For the Equations in Word2007-2016 in Windows, their ProgIds are Equation.3, so the code works fine for me.

    I could not create an old format document as yours and we don't have Mac to test, so we could not reproduce your issue and find causes and solution. I suggest you share us a sample document which equations are old, created by Equation editor 3. You could upload the file into OneDrive, share the document and paste the shared link here.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 1, 2017 6:06 AM
    Moderator
  • Hi Celeste,

    Actually the file was received from client and I'm not sure it has been created from MAC! I've uploaded the sample file to onedrive and find the link below.

    https://1drv.ms/w/s!AlPqOxD5s5U1a8izeCkaH7eH74k

    The sample document contains 216 equations and While running the code, only 6 equations are exported with well format and remaining 210 equations are being exported & while try opening in Mathtype, WMF files are showing "The File does not contain Equation data" which seems it's formatting in not fine with this type.

    Viewing in MS Word files also, if I select the Equation Object, it shows "Double click to open in Equation Editor 3.0" in status bar(For 210 equations). And while selecting other 6 Equations, it shows "Double click to open in Mathtype 6.0".

    I've Mathtype 6.9 Licensed in my system.

    Could you please review this?

    Regards,

    Vels.

    Tuesday, May 2, 2017 4:33 AM
  • Hello,

    All the equations could be exported and I could open them in Paint. Are they empty if opened in Paint?

    According to your description, I think you could export them as well, but the issue is old equations could not be opened in Mathtype. 

    To check if it could be updated programmatically using Open XML library, I would suggest you manually update one and compare files to check the difference and generate code.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 2, 2017 7:34 AM
    Moderator
  • Hi Celeste,

    I would like to Thank u for your valuable time that u spend for me.

    Yes! All the equations can be able to open in paint. But not in Mathtype. I tried manually double click a Microsoft Equation 3.0, it opened in Mathtype, simply I Click File menu in Mathtype, then Update "Test1.doc" and close Mathtype. Then Saved the Document and run my Solution file where I used Open XML, the exported new equation is fine which opened in Mathtype too. The code block I used to do was mentioned in my first post. 

    Could you please help me on adding the auto update or conversion in the code block!

    Regards,

    Vels.


    • Edited by sarvel Tuesday, May 2, 2017 8:33 AM
    Tuesday, May 2, 2017 8:27 AM
  • Hello,

    Mathtype is third party software, I think Open XML library or Office object model do not provide any method to update or do the conversion. If there are Mathtype APIs available to achieve that, you might need to use Mathtype APIs in your project. I would suggest you get support from 

    https://www.dessci.com/en/support/mathtype/default.asp

    Thanks for your understanding.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 2, 2017 9:28 AM
    Moderator