none
OpenXML SDK and MathML RRS feed

  • Question

  •  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?
    Monday, June 11, 2012 6:38 PM

Answers

  • Very nice post, but its's very complicated, and not very flexible.

    I see you tried to help me very much, I very appreciate it, but it's not the same what I need.

    I found more appropriate answer here.

    Sorry for disturbing and many thanks for your attention


    • Marked as answer by Lacinho Wednesday, June 13, 2012 8:54 AM
    • Edited by Lacinho Wednesday, June 13, 2012 9:02 AM
    Wednesday, June 13, 2012 8:54 AM

All replies

  • Hi,

    I create a Word document which contains math fomular: sin⁡(a+b)=sin⁡a  cos⁡b+cos⁡a  sin⁡b. In productivity tool, I reflect the document and get below code. FYI. focus on GenerateMainDocumentPart1Content method below:

    using DocumentFormat.OpenXml.Packaging;
    using Ap = DocumentFormat.OpenXml.ExtendedProperties;
    using Vt = DocumentFormat.OpenXml.VariantTypes;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Wordprocessing;
    using M = DocumentFormat.OpenXml.Math;
    using Ovml = DocumentFormat.OpenXml.Vml.Office;
    using V = DocumentFormat.OpenXml.Vml;
    using A = DocumentFormat.OpenXml.Drawing;
    namespace GeneratedCode
    {
        public class GeneratedClass
        {
            // Creates a WordprocessingDocument.
            public void CreatePackage(string filePath)
            {
                using(WordprocessingDocument package = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document))
                {
                    CreateParts(package);
                }
            }
            // Adds child parts and generates content of the specified part.
            private void CreateParts(WordprocessingDocument document)
            {
                ExtendedFilePropertiesPart extendedFilePropertiesPart1 = document.AddNewPart<ExtendedFilePropertiesPart>("rId3");
                GenerateExtendedFilePropertiesPart1Content(extendedFilePropertiesPart1);
                MainDocumentPart mainDocumentPart1 = document.AddMainDocumentPart();
                GenerateMainDocumentPart1Content(mainDocumentPart1);
                DocumentSettingsPart documentSettingsPart1 = mainDocumentPart1.AddNewPart<DocumentSettingsPart>("rId3");
                GenerateDocumentSettingsPart1Content(documentSettingsPart1);
                StylesWithEffectsPart stylesWithEffectsPart1 = mainDocumentPart1.AddNewPart<StylesWithEffectsPart>("rId2");
                GenerateStylesWithEffectsPart1Content(stylesWithEffectsPart1);
                StyleDefinitionsPart styleDefinitionsPart1 = mainDocumentPart1.AddNewPart<StyleDefinitionsPart>("rId1");
                GenerateStyleDefinitionsPart1Content(styleDefinitionsPart1);
                ThemePart themePart1 = mainDocumentPart1.AddNewPart<ThemePart>("rId6");
                GenerateThemePart1Content(themePart1);
                FontTablePart fontTablePart1 = mainDocumentPart1.AddNewPart<FontTablePart>("rId5");
                GenerateFontTablePart1Content(fontTablePart1);
                WebSettingsPart webSettingsPart1 = mainDocumentPart1.AddNewPart<WebSettingsPart>("rId4");
                GenerateWebSettingsPart1Content(webSettingsPart1);
                SetPackageProperties(document);
            }
    ******
    // Generates content of mainDocumentPart1.
            private void GenerateMainDocumentPart1Content(MainDocumentPart mainDocumentPart1)
            {
                Document document1 = new Document(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 wp14" }  };
                document1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
                document1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
                document1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
                document1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
                document1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
                document1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
                document1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
                document1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
                document1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
                document1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                document1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
                document1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
                document1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
                document1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
                document1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
                Body body1 = new Body();
                BookmarkStart bookmarkStart1 = new BookmarkStart(){ Name = "_GoBack", Id = "0" };
                BookmarkEnd bookmarkEnd1 = new BookmarkEnd(){ Id = "0" };
                Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "005E5FDA", RsidRunAdditionDefault = "005E5FDA" };
                M.Paragraph paragraph2 = new M.Paragraph();
                M.OfficeMath officeMath1 = new M.OfficeMath();
                M.MathFunction mathFunction1 = new M.MathFunction();
                M.FunctionProperties functionProperties1 = new M.FunctionProperties();
                M.ControlProperties controlProperties1 = new M.ControlProperties();
                RunProperties runProperties1 = new RunProperties();
                RunFonts runFonts1 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                Italic italic1 = new Italic();
                runProperties1.Append(runFonts1);
                runProperties1.Append(italic1);
                controlProperties1.Append(runProperties1);
                functionProperties1.Append(controlProperties1);
                M.FunctionName functionName1 = new M.FunctionName();
                M.Run run1 = new M.Run();
                M.RunProperties runProperties2 = new M.RunProperties();
                M.Style style1 = new M.Style(){ Val = M.StyleValues.Plain };
                runProperties2.Append(style1);
                RunProperties runProperties3 = new RunProperties();
                RunFonts runFonts2 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                runProperties3.Append(runFonts2);
                M.Text text1 = new M.Text();
                text1.Text = "sin";
                run1.Append(runProperties2);
                run1.Append(runProperties3);
                run1.Append(text1);
                functionName1.Append(run1);
                M.Base base1 = new M.Base();
                M.Delimiter delimiter1 = new M.Delimiter();
                M.DelimiterProperties delimiterProperties1 = new M.DelimiterProperties();
                M.ControlProperties controlProperties2 = new M.ControlProperties();
                RunProperties runProperties4 = new RunProperties();
                RunFonts runFonts3 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                Italic italic2 = new Italic();
                runProperties4.Append(runFonts3);
                runProperties4.Append(italic2);
                controlProperties2.Append(runProperties4);
                delimiterProperties1.Append(controlProperties2);
                M.Base base2 = new M.Base();
                M.Run run2 = new M.Run();
                RunProperties runProperties5 = new RunProperties();
                RunFonts runFonts4 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                runProperties5.Append(runFonts4);
                M.Text text2 = new M.Text();
                text2.Text = "a+b";
                run2.Append(runProperties5);
                run2.Append(text2);
                base2.Append(run2);
                delimiter1.Append(delimiterProperties1);
                delimiter1.Append(base2);
                base1.Append(delimiter1);
                mathFunction1.Append(functionProperties1);
                mathFunction1.Append(functionName1);
                mathFunction1.Append(base1);
                M.Run run3 = new M.Run();
                RunProperties runProperties6 = new RunProperties();
                RunFonts runFonts5 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                runProperties6.Append(runFonts5);
                M.Text text3 = new M.Text();
                text3.Text = "=";
                run3.Append(runProperties6);
                run3.Append(text3);
                M.MathFunction mathFunction2 = new M.MathFunction();
                M.FunctionProperties functionProperties2 = new M.FunctionProperties();
                M.ControlProperties controlProperties3 = new M.ControlProperties();
                RunProperties runProperties7 = new RunProperties();
                RunFonts runFonts6 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                Italic italic3 = new Italic();
                runProperties7.Append(runFonts6);
                runProperties7.Append(italic3);
                controlProperties3.Append(runProperties7);
                functionProperties2.Append(controlProperties3);
                M.FunctionName functionName2 = new M.FunctionName();
                M.Run run4 = new M.Run();
                M.RunProperties runProperties8 = new M.RunProperties();
                M.Style style2 = new M.Style(){ Val = M.StyleValues.Plain };
                runProperties8.Append(style2);
                RunProperties runProperties9 = new RunProperties();
                RunFonts runFonts7 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                runProperties9.Append(runFonts7);
                M.Text text4 = new M.Text();
                text4.Text = "sin";
                run4.Append(runProperties8);
                run4.Append(runProperties9);
                run4.Append(text4);
                functionName2.Append(run4);
                M.Base base3 = new M.Base();
                M.Run run5 = new M.Run();
                RunProperties runProperties10 = new RunProperties();
                RunFonts runFonts8 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                runProperties10.Append(runFonts8);
                M.Text text5 = new M.Text();
                text5.Text = "a";
                run5.Append(runProperties10);
                run5.Append(text5);
                base3.Append(run5);
                mathFunction2.Append(functionProperties2);
                mathFunction2.Append(functionName2);
                mathFunction2.Append(base3);
                M.MathFunction mathFunction3 = new M.MathFunction();
                M.FunctionProperties functionProperties3 = new M.FunctionProperties();
                M.ControlProperties controlProperties4 = new M.ControlProperties();
                RunProperties runProperties11 = new RunProperties();
                RunFonts runFonts9 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                Italic italic4 = new Italic();
                runProperties11.Append(runFonts9);
                runProperties11.Append(italic4);
                controlProperties4.Append(runProperties11);
                functionProperties3.Append(controlProperties4);
                M.FunctionName functionName3 = new M.FunctionName();
                M.Run run6 = new M.Run();
                M.RunProperties runProperties12 = new M.RunProperties();
                M.Style style3 = new M.Style(){ Val = M.StyleValues.Plain };
                runProperties12.Append(style3);
                RunProperties runProperties13 = new RunProperties();
                RunFonts runFonts10 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                runProperties13.Append(runFonts10);
                M.Text text6 = new M.Text();
                text6.Text = "cos";
                run6.Append(runProperties12);
                run6.Append(runProperties13);
                run6.Append(text6);
                functionName3.Append(run6);
                M.Base base4 = new M.Base();
                M.Run run7 = new M.Run();
                RunProperties runProperties14 = new RunProperties();
                RunFonts runFonts11 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                runProperties14.Append(runFonts11);
                M.Text text7 = new M.Text();
                text7.Text = "b";
                run7.Append(runProperties14);
                run7.Append(text7);
                base4.Append(run7);
                mathFunction3.Append(functionProperties3);
                mathFunction3.Append(functionName3);
                mathFunction3.Append(base4);
                M.Run run8 = new M.Run();
                RunProperties runProperties15 = new RunProperties();
                RunFonts runFonts12 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                runProperties15.Append(runFonts12);
                M.Text text8 = new M.Text();
                text8.Text = "+";
                run8.Append(runProperties15);
                run8.Append(text8);
                M.MathFunction mathFunction4 = new M.MathFunction();
                M.FunctionProperties functionProperties4 = new M.FunctionProperties();
                M.ControlProperties controlProperties5 = new M.ControlProperties();
                RunProperties runProperties16 = new RunProperties();
                RunFonts runFonts13 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                Italic italic5 = new Italic();
                runProperties16.Append(runFonts13);
                runProperties16.Append(italic5);
                controlProperties5.Append(runProperties16);
                functionProperties4.Append(controlProperties5);
                M.FunctionName functionName4 = new M.FunctionName();
                M.Run run9 = new M.Run();
                M.RunProperties runProperties17 = new M.RunProperties();
                M.Style style4 = new M.Style(){ Val = M.StyleValues.Plain };
                runProperties17.Append(style4);
                RunProperties runProperties18 = new RunProperties();
                RunFonts runFonts14 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                runProperties18.Append(runFonts14);
                M.Text text9 = new M.Text();
                text9.Text = "cos";
                run9.Append(runProperties17);
                run9.Append(runProperties18);
                run9.Append(text9);
                functionName4.Append(run9);
                M.Base base5 = new M.Base();
                M.Run run10 = new M.Run();
                RunProperties runProperties19 = new RunProperties();
                RunFonts runFonts15 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                runProperties19.Append(runFonts15);
                M.Text text10 = new M.Text();
                text10.Text = "a";
                run10.Append(runProperties19);
                run10.Append(text10);
                base5.Append(run10);
                mathFunction4.Append(functionProperties4);
                mathFunction4.Append(functionName4);
                mathFunction4.Append(base5);
                M.MathFunction mathFunction5 = new M.MathFunction();
                M.FunctionProperties functionProperties5 = new M.FunctionProperties();
                M.ControlProperties controlProperties6 = new M.ControlProperties();
                RunProperties runProperties20 = new RunProperties();
                RunFonts runFonts16 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                Italic italic6 = new Italic();
                runProperties20.Append(runFonts16);
                runProperties20.Append(italic6);
                controlProperties6.Append(runProperties20);
                functionProperties5.Append(controlProperties6);
                M.FunctionName functionName5 = new M.FunctionName();
                M.Run run11 = new M.Run();
                M.RunProperties runProperties21 = new M.RunProperties();
                M.Style style5 = new M.Style(){ Val = M.StyleValues.Plain };
                runProperties21.Append(style5);
                RunProperties runProperties22 = new RunProperties();
                RunFonts runFonts17 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                runProperties22.Append(runFonts17);
                M.Text text11 = new M.Text();
                text11.Text = "sin";
                run11.Append(runProperties21);
                run11.Append(runProperties22);
                run11.Append(text11);
                functionName5.Append(run11);
                M.Base base6 = new M.Base();
                M.Run run12 = new M.Run();
                RunProperties runProperties23 = new RunProperties();
                RunFonts runFonts18 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
                runProperties23.Append(runFonts18);
                M.Text text12 = new M.Text();
                text12.Text = "b";
                run12.Append(runProperties23);
                run12.Append(text12);
                base6.Append(run12);
                mathFunction5.Append(functionProperties5);
                mathFunction5.Append(functionName5);
                mathFunction5.Append(base6);
                officeMath1.Append(mathFunction1);
                officeMath1.Append(run3);
                officeMath1.Append(mathFunction2);
                officeMath1.Append(mathFunction3);
                officeMath1.Append(run8);
                officeMath1.Append(mathFunction4);
                officeMath1.Append(mathFunction5);
                paragraph2.Append(officeMath1);
                paragraph1.Append(paragraph2);
                Paragraph paragraph3 = new Paragraph(){ RsidParagraphMarkRevision = "00967160", RsidParagraphAddition = "00B110F0", RsidRunAdditionDefault = "005E5FDA" };
                Paragraph paragraph4 = new Paragraph(){ RsidParagraphAddition = "00967160", RsidRunAdditionDefault = "00967160" };
                Paragraph paragraph5 = new Paragraph(){ RsidParagraphAddition = "00967160", RsidRunAdditionDefault = "00967160" };
                Paragraph paragraph6 = new Paragraph(){ RsidParagraphAddition = "00967160", RsidRunAdditionDefault = "00967160" };
                SectionProperties sectionProperties1 = new SectionProperties(){ RsidR = "00967160" };
                PageSize pageSize1 = new PageSize(){ Width = (UInt32Value)12240U, Height = (UInt32Value)15840U };
                PageMargin pageMargin1 = new PageMargin(){ Top = 1440, Right = (UInt32Value)1800U, Bottom = 1440, Left = (UInt32Value)1800U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U };
                Columns columns1 = new Columns(){ Space = "720" };
                DocGrid docGrid1 = new DocGrid(){ LinePitch = 360 };
                sectionProperties1.Append(pageSize1);
                sectionProperties1.Append(pageMargin1);
                sectionProperties1.Append(columns1);
                sectionProperties1.Append(docGrid1);
                body1.Append(bookmarkStart1);
                body1.Append(bookmarkEnd1);
                body1.Append(paragraph1);
                body1.Append(paragraph3);
                body1.Append(paragraph4);
                body1.Append(paragraph5);
                body1.Append(paragraph6);
                body1.Append(sectionProperties1);
                document1.Append(body1);
                mainDocumentPart1.Document = document1;
            }
    ****
    } //class
    }//namespace


    Forrest Guo | MSDN Community Support | Feedback to manager

    Wednesday, June 13, 2012 7:39 AM
    Moderator
  • Very nice post, but its's very complicated, and not very flexible.

    I see you tried to help me very much, I very appreciate it, but it's not the same what I need.

    I found more appropriate answer here.

    Sorry for disturbing and many thanks for your attention


    • Marked as answer by Lacinho Wednesday, June 13, 2012 8:54 AM
    • Edited by Lacinho Wednesday, June 13, 2012 9:02 AM
    Wednesday, June 13, 2012 8:54 AM
  • Nice to know you resolved it.


    Forrest Guo | MSDN Community Support | Feedback to manager

    Wednesday, June 13, 2012 9:01 AM
    Moderator