【问题标题】:Novacode Docx EquationNovacode Docx 方程
【发布时间】:2016-07-07 00:49:20
【问题描述】:

我使用 Novacode.Docx 类来生成 Word 文件。我想插入方程,但默认方法用字符串制作方程。 How i can write sqrt block,例如?谢谢。

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
      }
   }
}

【问题讨论】:

    标签: novacode-docx


    【解决方案1】:

    我最近偶然发现了这个问题,我发现的方法是从段落中编辑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();
    

    【讨论】:

      猜你喜欢
      • 2016-08-17
      • 2017-06-22
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 2018-09-14
      相关资源
      最近更新 更多