Home >Backend Development >C#.Net Tutorial >Example of adding/revoking bookmarks to Word document using VB.NET in C#

Example of adding/revoking bookmarks to Word document using VB.NET in C#

黄舟
黄舟Original
2017-09-16 11:18:512203browse

In a modern office environment, when reading or editing a long Word document, if you want to leave a mark at one or more places in the document to facilitate future search and modification, you need to insert it at the corresponding document location. Bookmarks. So for developers, how to insert bookmarks quickly and easily in 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 company. The method is very simple, as follows:

Step 1: Initialize the Document instance and load the Word document


##

Document document = new Document();
document.LoadFromFile(@"C:\Users\Administrator\Desktop\中国梦.docx ");

Step 2: Insert a bookmark between the end of the seventh paragraph and the eighth paragraph, name the bookmark as "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 above steps, just search and locate in the document, and the document will automatically locate to the current location. Set the bookmark location.

The above simple three steps can complete the insertion of bookmarks into the word document.

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\中国梦.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 System
Imports Spire.Doc
Imports 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\中国梦.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 Class
End Namespace

Similarly, you can also refer to the following operations to cancel the bookmark

Step 1: Load the Word document that needs to be revoked


Document doc = new Document();
           doc.LoadFromFile(@"C:\Users\Administrator\Desktop\中国梦(书签).docx");

Steps 2: Undo 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 canceling the bookmark, the following document effect is obtained

##As shown in the figure, the paragraph where the bookmark was originally inserted has been cancelled.

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\中国梦(书签).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.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\中国梦(书签).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 Class
End Namespace

The above is the detailed content of Example of adding/revoking bookmarks to Word document using VB.NET in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn