交叉引用是对Word文档中特定位置的内容的引用,例如,可为标题、脚注、书签、尾注、编号段落等创建交叉引用。用户点击交叉引用链接之后,文档会自动跳转到指定位置。本文将以添加交叉引用到标题为例,展示如何使用Spire.Doc在Word中创建交叉引用。
C#
//创建Word文档并添加section
Document doc = new Document();
Section section = doc.AddSection();
//将内置字体样式Heading2及BodyText的字体名称更改为“宋体”(默认非中文字体)
Style s1 = doc.AddStyle(BuiltinStyle.Heading2);
s1.CharacterFormat.FontName = "宋体";
Style s2 = doc.AddStyle(BuiltinStyle.BodyText);
s2.CharacterFormat.FontName = "宋体";
//创建第一个段落并应用BodyText样式,本段用于创建交叉引用
Paragraph firstPara = section.AddParagraph();
firstPara.ApplyStyle(BuiltinStyle.BodyText);
//添加段落
for (int i = 0; i < 3; i++)
{
//添加标题段,应用Heading2样式
Paragraph paragraph = section.AddParagraph();
string headingText = string.Format("章节{0}", i+1);
paragraph.AppendText(headingText);
paragraph.ApplyStyle(BuiltinStyle.Heading2);
//添加内容段落,应用BodyText样式
paragraph = section.AddParagraph();
paragraph.AppendText("章节内容...");
paragraph.ApplyStyle(BuiltinStyle.BodyText);
}
//在标题为“章节3”的段落添加隐藏书签,隐藏书签名由“_”开头
//使用MS Word添加交叉引用到标题、脚注、尾注等特定位置时,也会自动添加隐藏书签用于定位
section.Paragraphs[5].AppendBookmarkStart("_章节3");
section.Paragraphs[5].AppendBookmarkEnd("_章节3");
//创建交叉引用域指向书签“_章节3”所在位置
Field field = new Field(doc);
field.Type = FieldType.FieldRef;
field.Code = @"REF _章节3 \p \h";
//添加域到第一段
firstPara.ChildObjects.Add(field);
//添加FieldSeparator对象到第一段
//MS Word中, 域的完整结构包括field start,field code, field separator, field result, field end
//有些域仅包含其中某些项
FieldMark fieldSeparator = new FieldMark(doc, FieldMarkType.FieldSeparator);
firstPara.ChildObjects.Add(fieldSeparator);
//添加域的显示文字
TextRange tr = new TextRange(doc);
tr.Text = "查看章节3";
tr.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;
firstPara.ChildObjects.Add(tr);
//添加FieldEnd对象到第一段, 作为域的结束
FieldMark fieldEnd = new FieldMark(doc, FieldMarkType.FieldEnd);
firstPara.ChildObjects.Add(fieldEnd);
//保存文档
doc.SaveToFile("output.docx", FileFormat.Docx2013);
VB.NET
'创建Word文档并添加section
Dim doc As Document = New Document
Dim section As Section = doc.AddSection
'将内置字体样式Heading2及BodyText的字体名称更改为“宋体”(默认非中文字体)
Dim s1 As Style = doc.AddStyle(BuiltinStyle.Heading2)
s1.CharacterFormat.FontName = "宋体"
Dim s2 As Style = doc.AddStyle(BuiltinStyle.BodyText)
s2.CharacterFormat.FontName = "宋体"
'创建第一个段落并应用BodyText样式,本段用于创建交叉引用
Dim firstPara As Paragraph = section.AddParagraph
firstPara.ApplyStyle(BuiltinStyle.BodyText)
'添加段落
Dim i As Integer = 0
Do While (i < 3)
'添加标题段,应用Heading2样式
Dim paragraph As Paragraph = section.AddParagraph
Dim headingText As String = String.Format("章节{0}", (i + 1))
paragraph.AppendText(headingText)
paragraph.ApplyStyle(BuiltinStyle.Heading2)
'
paragraph = section.AddParagraph
paragraph.AppendText("章节内容...")
paragraph.ApplyStyle(BuiltinStyle.BodyText)
i = (i + 1)
Loop
'在标题为“章节3”的段落添加隐藏书签,隐藏书签名由“_”开头
'使用MS Word添加交叉引用到标题、脚注、尾注等特定位置时,也会自动添加隐藏书签用于定位
section.Paragraphs(5).AppendBookmarkStart("_章节3")
section.Paragraphs(5).AppendBookmarkEnd("_章节3")
Dim field As Field = New Field(doc)
field.Type = FieldType.FieldRef
field.Code = "REF _章节3 \p \h"
'添加域到第一段
firstPara.ChildObjects.Add(field)
'添加FieldSeparator对象到第一段
'MS Word中, 域的完整结构包括field start,field code, field separator, field result, field end
'有些域仅包含其中某些项
Dim fieldSeparator As FieldMark = New FieldMark(doc, FieldMarkType.FieldSeparator)
firstPara.ChildObjects.Add(fieldSeparator)
'添加域的显示文字
Dim tr As TextRange = New TextRange(doc)
tr.Text = "查看章节3"
tr.CharacterFormat.UnderlineStyle = UnderlineStyle.Single
firstPara.ChildObjects.Add(tr)
'添加FieldEnd对象到第一段, 作为域的结束
Dim fieldEnd As FieldMark = New FieldMark(doc, FieldMarkType.FieldEnd)
firstPara.ChildObjects.Add(fieldEnd)
'保存文档
doc.SaveToFile("output.docx", FileFormat.Docx2013)