Exemplo n.º 1
0
        public static void MathML2Word()
        {
            XslCompiledTransform xslTransform = new XslCompiledTransform();

            xslTransform.Load(@"C:\Program Files (x86)\Microsoft Office\Office14\MML2OMML.xsl");

            // Load the file containing your MathML presentation markup.
            using (XmlReader reader = XmlReader.Create(File.Open("../../../test1.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);

                    //创建Word文档(Microsoft.Office.Interop.Word)
                    Microsoft.Office.Interop.Word._Application WordApp = new Application();
                    WordApp.Visible = true;
                    using (WordprocessingDocument package = WordprocessingDocument.Create("../../../template.docx", WordprocessingDocumentType.Document))
                    {
                        // Add a new main document part.
                        package.AddMainDocumentPart();

                        // Create the Document DOM.
                        package.MainDocumentPart.Document =
                            new DocumentFormat.OpenXml.Wordprocessing.Document(
                                new Body(
                                    new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
                                        new Run(
                                            new Text("  ")))));

                        // Save changes to the main document part.
                        package.MainDocumentPart.Document.Save();
                    }

                    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();
                            currentRun.InsertAt(runProperties2, 0);
                        }
                        par.Append(om);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>追加officeMathMl 到指定的word文档
        /// </summary>
        /// <param name="officeMathMl"></param>
        /// <param name="filePath"></param>
        public void WritOfficeMathMLToWord(string officeMathMl)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // Create a OfficeMath instance from the
                // OfficeMathML xml.
                DocumentFormat.OpenXml.Math.OfficeMath om =
                  new DocumentFormat.OpenXml.Math.OfficeMath(officeMathMl);

                // Add the OfficeMath instance to our
                // word template.

                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);

            }
        }
Exemplo n.º 3
0
        /// <summary>追加officeMathMl 到指定的word文档
        /// </summary>
        /// <param name="officeMathMl"></param>
        /// <param name="filePath"></param>
        public void WritOfficeMathMLToWord(string officeMathMl, Run par)
        {
            DocumentFormat.OpenXml.Math.OfficeMath om =
              new DocumentFormat.OpenXml.Math.OfficeMath(officeMathMl);

            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);
        }
Exemplo n.º 4
0
 public OpenXMLExpression(OfficeMath source, SyntaxTree builtUpExpression)
 {
     this.sourceElement  = source;
     this.expressionTree = builtUpExpression;
 }