OpenXML SDK y MathML

I use MathML to create some data blocks and I need to insert it throught OpenXML SDK into docx file. I've heard it is possible, but I didn't manage it. Could somebody help me with this problem?

preguntado el 12 de junio de 12 a las 10:06

1 Respuestas

As far as I know, the OpenXml SDK does not support presentation MathML out of the box.

Instead, the OpenXml SDK supports Office MathML. So, to insert presentation MathML into a word document we first have to transform the presentation MathML into Office MathML.

Fortunately, Microsoft provides a XSL file (called MML2OMML.xsl) to transform presentation MathML into Office MathML. The file MML2OMML.xsl is located under %ProgramFiles%\Microsoft Office\Office12. In conjunction with the .Net Framework class XslCompiledTransform we are able to transform presentation MathML into Office MathML.

El siguiente paso es crear un OfficeMath object from the transformed MathML. The OfficeMath class represents a run containing WordprocessingML which shall be handled as though it was Office Open XML Math. For more info please refer to MSDN.

The presentation MathML does not contain font information. To get a nice result we must add font information to the created OfficeMath objeto.

In the last step we have to add the OfficeMath object to our word document. In the example below I simply search for the first Paragraph in a word document called template.docx y agregue el OfficeMath object to the found paragraph.

XslCompiledTransform xslTransform = new XslCompiledTransform();

// The MML2OMML.xsl file is located under 
// %ProgramFiles%\Microsoft Office\Office12\

// Load the file containing your MathML presentation markup.
using (XmlReader reader = XmlReader.Create(File.Open("mathML.xml", FileMode.Open)))
  using (MemoryStream ms = new MemoryStream())
    XmlWriterSettings settings = xslTransform.OutputSettings.Clone();

    // Configure xml writer to omit xml declaration.
    settings.ConformanceLevel = ConformanceLevel.Fragment;
    settings.OmitXmlDeclaration = true;

    XmlWriter xw = XmlWriter.Create(ms, settings);

    // Transform our MathML to OfficeMathML
    xslTransform.Transform(reader, xw);
    ms.Seek(0, SeekOrigin.Begin);

    StreamReader sr = new StreamReader(ms, Encoding.UTF8);

    string officeML = sr.ReadToEnd();


    // Create a OfficeMath instance from the
    // OfficeMathML xml.
    DocumentFormat.OpenXml.Math.OfficeMath om =
      new DocumentFormat.OpenXml.Math.OfficeMath(officeML);

    // Add the OfficeMath instance to our 
    // word template.
    using (WordprocessingDocument wordDoc =
      WordprocessingDocument.Open("template.docx", true))
      DocumentFormat.OpenXml.Wordprocessing.Paragraph par =

      foreach (var currentRun in om.Descendants<DocumentFormat.OpenXml.Math.Run>())
        // Add font information to every run.
        DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties2 =
          new DocumentFormat.OpenXml.Wordprocessing.RunProperties();

        RunFonts runFonts2 = new RunFonts() { Ascii = "Cambria Math", HighAnsi = "Cambria Math" };        

        currentRun.InsertAt(runProperties2, 0);


Respondido el 12 de junio de 12 a las 21:06

Thanks a lot for your answer! I very appreciate it! I've already tried your code and I had success! You saved me! Thanks again! - user1450792

@user1450792: You're welcome! Please accept my answer by clicking on on the check mark beside the answer to toggle it from hollow to green. - Hans

Very good solution @Hans. But how can i do the reverse. I want to convert the xml content of word file with OMML to plain text containing MML..!? - sereno

@serene: Please, see my answer to your question. - Hans

@Hans, Hi! thank you and it does work! I want to convert MathML to Open Xml Math as an addin in the Ribbon area by C#, but I can't find a method like "par.Append(om);" in PIA, and I don't know how to transform the WordprocessingDocument type to Globals.ThisAddIn.Application.ActiveDocument type. Is there anyway to perform the same method in a addin or a right-click button which can real-time transform these MathMl into MS equation in bunches or one-by-one, thank you! - archenoo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.