1

I use Novacode.Docx class for Word files generation. I want insert equation but default method made equation with string. How i can write sqrt block, for example? Thanks.

using System;
using Novacode;

namespace Program1
{
   class MyClass
   {
      Novacode.DocX Doc;
      MyClass()
      {
         Doc = Novacode.DocX.Create("C:\\1.docx", DocumentTypes.Document);
         Doc.InsertEquation(""); // <- This method insert string
      }
   }
}
Serogriff
  • 11
  • 2

1 Answers1

1

I stumbled upon this problem recently, and the way I found was to edit the xml from the paragraph, since it appears that the DocX doesn't have this function.

To do so, I created a .docx file in Word with the elements I wanted, changed the extension to .zip, and read the xml from the word\document.xml file. So, for a square root, for example, the code in C# is the following:

DocX doc = DocX.Create("testeDocument.docx");
Paragraph eqParagraph = doc.InsertEquation("");
XElement xml = eqParagraph.Xml;
XNamespace mathNamespace = "http://schemas.openxmlformats.org/officeDocument/2006/math";
XElement omath = xml.Descendants(mathNamespace + "oMath").First();
omath.Elements().Remove();
XElement sqrt= new XElement(mathNamespace + "rad");

XElement deg = new XElement(mathNamespace + "deg");

XElement radProp = new XElement(mathNamespace + "radPr");
XElement degHide = new XElement(mathNamespace + "degHide");
degHide.Add(new XAttribute(mathNamespace + "val", 1));
radProp.Add(degHide);
sqrt.Add(radProp);

sqrt.Add(deg);

XElement rad = new XElement(mathNamespace + "e");
rad.Add(new XElement(mathNamespace + "r", new XElement(mathNamespace + "t", "this goes inside the sqrt")));
sqrt.Add(rad);

omath.Add(sqrt);
doc.Save();
Magnetron
  • 7,495
  • 1
  • 25
  • 41