我最近偶然发现了这个问题,我发现的方法是从段落中编辑xml,因为DocX似乎没有这个功能。
为此,我在 Word 中创建了一个包含所需元素的 .docx 文件,将扩展名更改为 .zip,然后从word\document.xml
文件中读取 xml。因此,例如,对于平方根,C# 中的代码如下:
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();