【问题标题】:How can I insert a cross-reference page number in Novacode DocX如何在 Novacode DocX 中插入交叉引用页码
【发布时间】:2018-02-23 05:52:57
【问题描述】:

我正在使用 Novacode DocX 创建一个 Word 文档,我想在其中插入一段文本,然后稍后在文档中以“(参见第 X 页)”的形式插入对它的引用,其中 X 是动态的由 Word 生成。

在 Word 本身中,我可以通过为第一段文本创建书签并在我想要页码的位置插入交叉引用来轻松地做到这一点。

我想我知道如何使用 DocX 添加书签,但如何创建交叉引用?这在 DocX 中是否可行?

非常感谢您的帮助, 克里斯

【问题讨论】:

    标签: c# novacode-docx


    【解决方案1】:

    经过一番折腾,我终于找到了实现这一点的方法:

    internal void AddCrossReference(DocX doc, Paragraph p, string destination)
            {
                XNamespace ns= doc.Xml.Name.NamespaceName;
                XNamespace xmlSpace = doc.Xml.GetNamespaceOfPrefix("xml");
                p = p.Append(" (see pp");
                p.Xml.Add(new XElement(ns + "r", new XElement(ns + "fldChar", new XAttribute(ns + "fldCharType", "begin"))));
                p.Xml.Add(new XElement(ns + "r", new XElement(ns + "instrText", new XAttribute(xmlSpace + "space", "preserve"), String.Format(" PAGEREF {0} \\h ", destination))));
                p.Xml.Add(new XElement(ns + "r", new XElement(ns + "fldChar", new XAttribute(ns + "fldCharType", "separate"))));
                p.Xml.Add(new XElement(ns + "r", new XElement(ns + "rPr", new XElement(ns + "noProof")), new XElement(ns + "t", "1")));
                p.Xml.Add(new XElement(ns + "r", new XElement(ns + "fldChar", new XAttribute(ns + "fldCharType", "end"))));
                p = p.Append(")");
            }
    

    destination 是您要交叉引用的书签的名称。

    欢迎提出任何改进建议。

    【讨论】:

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