【问题标题】:OpenXML SDK and MathMLOpenXML SDK 和 MathML
【发布时间】:2012-06-12 09:13:01
【问题描述】:

我使用 MathML 创建一些数据块,我需要通过 OpenXML SDK 将其插入到 docx 文件中。我听说这是可能的,但我没有做到。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: openxml docx equation mathml


    【解决方案1】:

    据我所知,OpenXml SDK 不支持开箱即用的表示 MathML。

    相反,OpenXml SDK 支持 Office MathML。 因此,要将演示文稿 MathML 插入到 word 文档中,我们首先需要 将演示文稿 MathML 转换为 Office MathML。

    幸运的是,微软提供了一个 XSL 文件(称为 MML2OMML.xsl)来转换表示 MathML 到 Office MathML。文件 MML2OMML.xsl 位于 %ProgramFiles%\Microsoft Office\Office12 下。 结合 .Net Framework 类 XslCompiledTransform 我们可以将演示文稿 MathML 转换为 Office MathML。

    下一步是从转换后的 MathML 创建一个OfficeMath 对象。 OfficeMath 类表示包含 WordprocessingML 的运行,应像处理 Office Open XML Math 一样处理。 更多信息请参考MSDN

    MathML 演示文稿不包含字体信息。为了得到一个好的结果 我们必须将字体信息添加到创建的OfficeMath 对象中。

    在最后一步中,我们必须将OfficeMath 对象添加到我们的word 文档中。 在下面的示例中,我只是在 a 中搜索第一个 Paragraph 名为 template.docx 的 word 文档,并将 OfficeMath 对象添加到找到的段落中。

    XslCompiledTransform xslTransform = new XslCompiledTransform();
    
    // The MML2OMML.xsl file is located under 
    // %ProgramFiles%\Microsoft Office\Office12\
    xslTransform.Load("MML2OMML.xsl");
    
    // 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();
    
        Console.Out.WriteLine(officeML);
    
        // 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 =
            wordDoc.MainDocumentPart.Document.Body.Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().FirstOrDefault();        
    
          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" };        
            runProperties2.Append(runFonts2);
    
            currentRun.InsertAt(runProperties2, 0);
          }
    
          par.Append(om);
        }
      }
    }
    

    【讨论】:

    • 非常感谢您的回答!我非常感谢!我已经尝试过你的代码并且成功了!你救了我!再次感谢!
    • @user1450792:不客气!请通过单击答案旁边的复选标记将其从空心切换为绿色来接受我的答案。
    • 很好的解决方案@Hans。但是我该怎么做相反的事情。我想将带有 OMML 的 word 文件的 xml 内容转换为包含 MML 的纯文本..!?
    • @serene:请看我对你问题的回答。
    • @Hans,嗨!谢谢你,它确实有效!我想通过 C# 将 MathML 转换为 Open Xml Math 作为功能区区域中的插件,但我找不到像“par.Append(om);”这样的方法在 PIA 中,我不知道如何将 WordprocessingDocument 类型转换为 Globals.ThisAddIn.Application.ActiveDocument 类型。有没有办法在插件或右键中执行相同的方法,可以将这些MathMl实时成串或一个接一个地转换为MS方程,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    相关资源
    最近更新 更多