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
New Document ();d ocument. LoadFromFile (@ "");
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 ("");
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#
usingSystem;usingSpire.doc;usingSpire.Doc.Documents;namespacewordbookmark{classBookmark {Static voidMain (string[] args) { //Load DocumentDocument document =NewDocument (); Document. LoadFromFile (@"C:\Users\Administrator\Desktop\ Chinese Dream. docx"); //Insert BookmarkSection section = document. sections[0]; Section. paragraphs[7]. Appendbookmarkstart (". NETFramework"); paragraphs[8]. Appendbookmarkend (". NETFramework"); //Save and LaunchDocument. 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 DocumentDim Document as document =New Document document. LoadFromFile ("C:\Users\Administrator\Desktop\ Chinese Dream. docx") 'Insert BookmarkDim section as section = document. Sections (0) section. Paragraphs (7). Appendbookmarkstart (". NETFramework") section. Paragraphs (8). Appendbookmarkend (". NETFramework") 'Save and LaunchDocument. 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
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 # :
usingSpire.doc;namespaceremoving{classProgram {Static voidMain (string[] args) { //Load DocumentDocument doc =NewDocument (); Doc. LoadFromFile (@"C:\Users\Administrator\Desktop\ Chinese Dream (bookmark). docx"); //Remove BookmarkDoc. Bookmarks.removeat (0); //Save and LaunchDoc. 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 Strin g)'Load DocumentDim doc as Document =New Document Doc. LoadFromFile ("C:\Users\Administrator\Desktop\ Chinese Dream (bookmark). docx") 'Remove BookmarkDoc. Bookmarks.removeat (0) 'Save and LaunchDoc. SaveToFile ("Remove Bookmark.docx", Fileformat.docx) System.Diagnostics.Process.Start ("Remove Bookmark.docx") End Sub end Classend Namespace
The above is my word to insert and undo the operation of the bookmark demo, I hope this share of your inspiration, thanks to read!
C#/vb.net Add/Revoke bookmarks to a Word document