in a modern office environment, read or edit a longer length of Word document, you want to leave a tag somewhere in the document, or a few places, to make it easier to find, modify, and insert a bookmark at a relative 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 use a free Word component released by E-iceblue company (for. NET), the method is simple, as follows:
Step One: Initialize 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.
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/06/8B/wKiom1m5-XWS9kKqAAFyvgf4kro746.png "title=" Shuqian.png "alt=" Wkiom1m5-xws9kkqaafyvgf4kro746.png "/>
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 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.documents namespace 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 ();d OC. 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, the text is as follows
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/A5/3C/wKioL1m5-8WzpAzkAAFhpy-Y7Ms514.png "title=" Removebookmark.png "alt=" Wkiol1m5-8wzpazkaafhpy-y7ms514.png "/>
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);Nbsp; system.diagnostics.process.start ("Remove bookmark.docx "); } }}
vb.net
Imports spire.doc namespace 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\ 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
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!
This article is from the "Mochou" blog, make sure to keep this source http://miayo.blog.51cto.com/13270370/1965181
C#/vb.net Add/Revoke bookmarks to a Word document