本文介绍如何使用spire.doc在word文档中添加超链接链接到文档内的某个书签。
c#
using spire.doc;
using spire.doc.documents;
namespace wordcodeexamples
{
class program
{
static void main(string[] args)
{
//创建document对象
document doc = new document();
//添加两个节
section section1 = doc.addsection();
section section2 = doc.addsection();
//在第二节中添加一个段落,并在段落上添加书签“mybookmark”
paragraph bookmarkparagrapg = section2.addparagraph();
bookmarkparagrapg.appendtext("这里有一个书签mybookmark");
bookmarkstart start = bookmarkparagrapg.appendbookmarkstart("mybookmark");
bookmarkparagrapg.items.insert(0, start);
bookmarkparagrapg.appendbookmarkend("mybookmark");
//在第一节中添加段落,并添加一个链接到书签的超链接
paragraph paragraph = section1.addparagraph();
paragraph.appendtext("链接到书签: ");
paragraph.appendhyperlink("mybookmark", "跳转至指定书签", hyperlinktype.bookmark);
//保存文档
doc.savetofile("linktobookmark.docx", fileformat.docx2013);
}
}
}
vb.net
imports spire.doc
imports spire.doc.documents
namespace wordcodeexamples
class program
shared sub main(byval args() as string)
'创建document对象
document doc = new document()
'添加两个节
dim section1 as section = doc.addsection()
dim section2 as section = doc.addsection()
'在第二节中添加一个段落,并在段落上添加书签“mybookmark”
dim bookmarkparagrapg as paragraph = section2.addparagraph()
bookmarkparagrapg.appendtext("这里有一个书签mybookmark")
dim start as bookmarkstart = bookmarkparagrapg.appendbookmarkstart("mybookmark")
bookmarkparagrapg.items.insert(0, start)
bookmarkparagrapg.appendbookmarkend("mybookmark")
'在第一节中添加段落,并添加一个链接到书签的超链接
dim paragraph as paragraph = section1.addparagraph()
paragraph.appendtext("链接到书签: ")
paragraph.appendhyperlink("mybookmark", "跳转至指定书签", hyperlinktype.bookmark)
'保存文档
doc.savetofile("linktobookmark.docx", fileformat.docx2013)
end sub
end class
end namespace