我正在使用Novacode DocX创建一个Word文档,我想在其中插入一段文本,然后在文档中以“(参见第X页)”的形式插入对它的引用,其中X是动态的由Word生成。
在Word本身,我可以通过为第一段文本创建书签并在我想要页码的位置插入交叉引用来轻松完成此操作。
我想我知道如何使用DocX添加书签,但如何创建交叉引用?这在DocX中甚至可能吗?
非常感谢您的帮助, 克里斯
答案 0 :(得分:2)
经过一番摆弄,我终于找到了实现这个目标的方法:
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
是您想要交叉引用的书签的名称。
欢迎任何建议的改进。