In a modern office environment, when you are reading or editing a long-length Word document, you want to leave a marker somewhere or several in the document, so that you can find and modify it later, you need to insert a bookmark at the corresponding document location. So for developers, how to quickly and easily insert bookmarks in a C # or vb.net language environment, I share my experience. Here I have used a e-iceblue company released a free word component (Spire.doc for. NET), the method is simple, as follows:
Step one : Initialize the document instance and load Word documents
Document document = new document ();d ocument. LoadFromFile (@ "C:\Users\Administrator\Desktop\ Chinese Dream. docx");
Step two : Insert a bookmark between the seventh and eighth paragraphs and name the bookmark "C#.bookmark "
Section section = document. Sections[0];section. PARAGRAPHS[7]. Appendbookmarkstart ("C#.bookmark"); PARAGRAPHS[8]. Appendbookmarkend ("C#.bookmark");
Step three : Save the file
Document. SaveToFile ("Bookmark.docx", fileformat.docx); System.Diagnostics.Process.Start ("Bookmark.docx");
Once you have completed the above steps, find the location in your document, and the document will automatically navigate to the location of the bookmark you are currently setting.
The above simple three steps to complete the Word document bookmark insertion.
The complete code is as follows, for reference:
C#
Using system;using spire.doc;using spire.doc.documents;namespace wordbookmark{ class Bookmark { static void Main (string[] args) { //load document document = new document (); Document. LoadFromFile (@ "C:\Users\Administrator\Desktop\ Chinese Dream. docx"); Insert Bookmark section Section = document. Sections[0]; Section. PARAGRAPHS[7]. Appendbookmarkstart (". NETFramework "); 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\ Chinese Dream. 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
Similarly, the Undo bookmark can also refer to perform the following actions
Step one : Load a word that requires a bookmark to be undone Document
Document doc = new document (); Doc. LoadFromFile (@ "C:\Users\Administrator\Desktop\ Chinese Dream (bookmark). docx");
Step Two : Undo an existing bookmark
Doc. Bookmarks.removeat (0);
Step three : Save the file
Doc. SaveToFile ("Remove bookmark.docx", fileformat.docx); System.Diagnostics.Process.Start ("Remove bookmark.docx");
After you undo the bookmark, you get the following document effect
, the previously inserted paragraph has been bookmarked
The complete code is as follows
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\ Chinese Dream (bookmark). 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 Share D Sub Main (ByVal args () as String) ' Load document Dim doc as Document = New document Doc. LoadFromFile ("C:\Users\Administrator\Desktop\ Chinese Dream (bookmark). 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