本文介绍如何使用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