C #/VB. NET Add/Remove bookmarks to/from Word documents,
In a modern office environment, when reading or editing long Word documents, you want to mark one or more of them to facilitate future search and modification, you need to insert bookmarks at the corresponding document location. For developers, how to quickly and easily insert bookmarks in the C # Or VB. NET language environment? Let me share my experience. Here I used a Free Word component (Free Spire. Doc for. NET) released by E-iceblue. The method is simple as follows:
Step 1: Initialize DocumentInstance and load WordDocument
Document document = new Document (); document. LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ chinam.docx ");
Step 2: Insert a bookmark between the end of section 7 and section 8 and name it "C #. bookmark "."
Section section = document.Sections[0];section.Paragraphs[7].AppendBookmarkStart("C#.bookmark");section.Paragraphs[8].AppendBookmarkEnd("C#.bookmark ");
Step 3: Save the file
document.SaveToFile("Bookmark.docx", FileFormat.Docx);System.Diagnostics.Process.Start("Bookmark.docx");
After completing the preceding steps, you can find and locate in the document. The document automatically locates the current bookmarks.
The above three steps can be used to insert a Word document bookmarks.
The complete code is as follows for your 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 \ chinam.docx"); // Insert Bookmark 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 \ chinam.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, you can also perform the following operations to remove bookmarks.
Step 1: load the WordDocument
Document doc = new Document (); doc. LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ Chinese Dream ().docx ");
Step 2: remove existing bookmarks
doc.Bookmarks.RemoveAt(0);
Step 3: Save the file
doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx); System.Diagnostics.Process.Start("Remove Bookmark.docx");
After the bookmarks are removed, the following documents are displayed:
The paragraph that was originally inserted with the bookmarks has been removed from the bookmarks
The complete code is as follows:
C #:
Using Spire. doc; namespace Removing {class Program {static void Main (string [] args) {// Load Document doc = new Document (); doc. loadFromFile (@ "C: \ Users \ Administrator \ Desktop \ Chinese Dream (.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 \ Chinese Dream ().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 demonstration of word insertion and bookmarks revocation. I hope this sharing will inspire you. Thank you for reading this article!