none
Programmatically adding math equations to Word document from C# RRS feed

  • Question

  • I am just starting to try and learn how to create MS Word documents from my Visual C# project. One thing that I really need to be able to do is add complex math equations (which is the main reason I am using Word as the output format).

    The online information seems to be a bit sparse for this. I think if I had an example with a few elements that I need I could probably figure out how to modify and adapt it. So I was wondering if someone could post a simple example that does the following (preferably with some simple comments:

    1. creates a new Word application (not opening an existing file, using the default template is fine)

    2. Changes the font to Verdana 9

    3. Write a line of text to the document that ends with the equivalent of a new paragraph mark.

    4. Write the following math equation (it seems I can't have images since this is my first question, so I'll have to describe the formula I want using latex notation, I hope that is okay):

     \psi_{c,d}^{2}  = \frac{ \sqrt{a^2+b^2} }{ \sum_0^c x } 

    5. Write a new line of text, but in the middle of that line of text include a short equation followed by more normal text.

    I think that would give me all the info I need to have the pieces fall into place to be able to do the larger project I am trying to get to work.

    Thanks.

    Saturday, May 23, 2015 8:34 PM

Answers

  • Hi DMT69

    Hmmm. Ideally, rather than automate the Word application, you should probably be leveraging the Word Open XML file format, meaning you wouldn't be automating the application, but writing the file directly. An Office Open XML file is a Zip package of XML files that the Word (or Excel or PowerPoint) application recognizes as a valid document (spreadsheet, slide show). Generally, this is much faster than automating the application interface, no need to worry about the UI interupting with messages requiring user input, etc.

    The Open XML SDK makes this comparatively easy to do as it abstracts the XML to classes with Intellisense - no need to learn the entire XML vocabularies.

    You can find out more about OOXML and the SDK at openxmldeveloper.org and there's an Open XML SDK forum on these servers.

    For the purpose you outline, whether you automate or go the OOXML route, I recommend setting up a template file that contains - at the very least - the formatting you want to use. AND this formatting should be defined in a STYLE rather than applying it directly to the text; usually, you'd redefine the NORMAL style (change the font name and size, for example).

    For the OOXML route, as the next step you'd create your basic document, as you describe, then inspect it using the Open XML SDK Productivity Tool. This can give you exactly the code required to generate the document, as it stands.

    If you really want to automate this, I can tell you how to do all the formatting and text stuff, but I've no clue about the Math part...


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, May 24, 2015 6:09 PM
    Moderator
  • I'm not aware of any examples, although you might find this article helpful:

    http://blogs.msdn.com/b/murrays/archive/2006/10/07/mathml-and-ecma-math-_2800_omml_2900_-.aspx
    Beyond that, I can only re-iterate what I mentioned in my first reply about working with the Open XML SDK Productivity Tool. Create a document with the equation (or type of equation) you want to build then inspect the code the Tool proposes for generating the document. You should find a section specific for the Open MathML, which would certainly be stored as a separate XML "part" in the document "zip package".

    Usually, you wouldn't be using the SDK together with automation - it would be the one or the other. That being said, the Word object model does have the InsertXML method which allows you to insert a string of VALID WordOpenXML. There is a tool that can convert the result produced by the SDK to the required "flat file" version of WordOpenXML. But OTOH if you're comfortable with XML you could probably construct the XML directly.

    In order to see what the valid WordOpenXML of an equation would look like, select the paragraph with the equation then have VBA output it to a separate file:

    Selection.WordOpenXML


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, May 27, 2015 5:32 PM
    Moderator

All replies

  • Hi DMT69

    Hmmm. Ideally, rather than automate the Word application, you should probably be leveraging the Word Open XML file format, meaning you wouldn't be automating the application, but writing the file directly. An Office Open XML file is a Zip package of XML files that the Word (or Excel or PowerPoint) application recognizes as a valid document (spreadsheet, slide show). Generally, this is much faster than automating the application interface, no need to worry about the UI interupting with messages requiring user input, etc.

    The Open XML SDK makes this comparatively easy to do as it abstracts the XML to classes with Intellisense - no need to learn the entire XML vocabularies.

    You can find out more about OOXML and the SDK at openxmldeveloper.org and there's an Open XML SDK forum on these servers.

    For the purpose you outline, whether you automate or go the OOXML route, I recommend setting up a template file that contains - at the very least - the formatting you want to use. AND this formatting should be defined in a STYLE rather than applying it directly to the text; usually, you'd redefine the NORMAL style (change the font name and size, for example).

    For the OOXML route, as the next step you'd create your basic document, as you describe, then inspect it using the Open XML SDK Productivity Tool. This can give you exactly the code required to generate the document, as it stands.

    If you really want to automate this, I can tell you how to do all the formatting and text stuff, but I've no clue about the Math part...


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, May 24, 2015 6:09 PM
    Moderator
  • Hi, thanks for the reply.

    I did look briefly at OpenXML but was able to make more headway with the Interop interface which I can get up and working a bit, especially for the math.

    The math is the most important part of what I am trying to do, and the whole reason I am trying to go this route.

    However, if you had some getting started code for the non-math part of the OOXML method I could take a look at that and see if I could get it working. Is the math part documented somewhere?

    Sunday, May 24, 2015 10:08 PM
  • This article will give you the basics of automating Word using C#

    https://support.microsoft.com/en-us/kb/316384?wa=wsignin1.0

    This one provides a bit more explanation and detail

    http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx

    FWIW the entry point to the Math stuff is via the Document object. Any existing Math objects can be accessed via Document.OMaths collection; this OMath object has properties and methods. There are the properties of the Document object beginning with the prefix OMath. Use the Document.OMaths.Add method to create a new equation. You can find more information in the Help files.

    I do believe, however, that you would be better off using the Open XML File format directly. It incorporates Office MathML...


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, May 25, 2015 3:18 PM
    Moderator
  • Okay, I've got the automating thing working fine now. Time to tackle the XML method.

    Is there an example somewhere of including math using the XML SDK? I mean, I understand it uses OMML, but how does that translate into including it in a document using C#?

    Wednesday, May 27, 2015 5:58 AM
  • I'm not aware of any examples, although you might find this article helpful:

    http://blogs.msdn.com/b/murrays/archive/2006/10/07/mathml-and-ecma-math-_2800_omml_2900_-.aspx
    Beyond that, I can only re-iterate what I mentioned in my first reply about working with the Open XML SDK Productivity Tool. Create a document with the equation (or type of equation) you want to build then inspect the code the Tool proposes for generating the document. You should find a section specific for the Open MathML, which would certainly be stored as a separate XML "part" in the document "zip package".

    Usually, you wouldn't be using the SDK together with automation - it would be the one or the other. That being said, the Word object model does have the InsertXML method which allows you to insert a string of VALID WordOpenXML. There is a tool that can convert the result produced by the SDK to the required "flat file" version of WordOpenXML. But OTOH if you're comfortable with XML you could probably construct the XML directly.

    In order to see what the valid WordOpenXML of an equation would look like, select the paragraph with the equation then have VBA output it to a separate file:

    Selection.WordOpenXML


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, May 27, 2015 5:32 PM
    Moderator
  • I've started using the productivity tool as suggested and it is really helpful. Of course I only sort of understand what I am doing, but it seems to work somewhat.

    I have been trying to write my own functions for making things like fractions and square roots since it is an absurdly complicated process for each little piece. I'm really surprised that there aren't already more convenient functions for doing this.

    Anyway, thanks for the tips.

    Wednesday, May 27, 2015 6:24 PM
  • Hi

    Did you get the fraction part sorted. If do can you maybe send me example. I need the fractions to display stacked the way it is done in word. I searched a lot but don't get something that will help me.

    Thursday, October 29, 2015 7:48 AM