在現代辦公環境中,閱讀或者編輯較長篇幅的Word文檔時,想要在文檔中某一處或者幾處留下標記,方便日後尋找、修改時,需要在相對應的文檔位置插入書籤。那對於開發人員而言,在C#或者VB.NET語言環境中,如何來快速、簡便的插入書籤呢,我分享一下我的經驗。這裡我用到了一款E-iceblue公司發布的一款免費的Word組件(Free Spire.Doc for .NET),方法很簡單,如下:
步驟一:初始化Document執行個體並載入Word文檔
Document document = new Document();document.LoadFromFile(@"C:\Users\Administrator\Desktop\中國夢.docx ");
步驟二:於第七段末和第八段間插入書籤,命名書籤為“C#.bookmark”
Section section = document.Sections[0];section.Paragraphs[7].AppendBookmarkStart("C#.bookmark");section.Paragraphs[8].AppendBookmarkEnd("C#.bookmark ");
步驟三:儲存檔案
document.SaveToFile("Bookmark.docx", FileFormat.Docx);System.Diagnostics.Process.Start("Bookmark.docx");
完成後以上步驟後,文檔中尋找定位即可,文檔自動定位到當前所設書籤位置。
以上簡單三個步驟即可完成對word文檔書籤插入。
完整代碼如下,供參考:
C#
using System;using Spire.Doc;using Spire.Doc.Documents;namespace WordBookmark{ class Bookmark { static void Main(string[] args) { //Load Document Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\中國夢.docx "); //Insert Bookmark Section section = document.Sections[0]; section.Paragraphs[7].AppendBookmarkStart(".NETFramework");section.Paragraphs[8].AppendBookmarkEnd(".NETFramework"); //Save and Launch document.SaveToFile("Bookmark.docx", FileFormat.Docx); System.Diagnostics.Process.Start("Bookmark.docx"); } }}
VB.NET:
Imports SystemImports Spire.DocImports Spire.Doc.DocumentsNamespace WordBookmark Class Bookmark Private Shared Sub Main(ByVal args() As String) 'Load Document Dim document As Document = New Document document.LoadFromFile("C:\Users\Administrator\Desktop\中國夢.docx ") 'Insert Bookmark Dim section As Section = document.Sections(0) section.Paragraphs(7).AppendBookmarkStart(".NETFramework") section.Paragraphs(8).AppendBookmarkEnd(".NETFramework") 'Save and Launch document.SaveToFile("Bookmark.docx", FileFormat.Docx) System.Diagnostics.Process.Start("Bookmark.docx") End Sub End ClassEnd Namespace
同樣的,撤銷書籤也可以參考執行我下面的操作
步驟一:載入需要撤銷書籤的Word文檔
Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\中國夢(書籤).docx");
步驟二:撤銷已有書籤
doc.Bookmarks.RemoveAt(0);
步驟三:儲存檔案
doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx); System.Diagnostics.Process.Start("Remove Bookmark.docx");
撤銷書籤後,得到以下文檔效果
,原本插入書籤的段落已撤銷書籤
完整代碼如下
C#:
using Spire.Doc;namespace Removing{ class Program { static void Main(string[] args) { //Load Document Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\中國夢(書籤).docx "); //Remove Bookmark doc.Bookmarks.RemoveAt(0); //Save and Launch doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx); System.Diagnostics.Process.Start("Remove Bookmark.docx"); } }}
VB.NET:
Imports Spire.DocNamespace Removing Class Program Private Shared Sub Main(ByVal args() As String) 'Load Document Dim doc As Document = New Document doc.LoadFromFile("C:\Users\Administrator\Desktop\中國夢(書籤).docx ") 'Remove Bookmark doc.Bookmarks.RemoveAt(0) 'Save and Launch doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx) System.Diagnostics.Process.Start("Remove Bookmark.docx") End Sub End ClassEnd Namespace