交叉引用是对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)