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

黄舟
Release: 2017-09-16 11:18:51
Original
2162 people have browsed it

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 ");
Copy after login

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 ");
Copy after login

Step 3: Save the file


document.SaveToFile("Bookmark.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Bookmark.docx");
Copy after login

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");
        }
    }
}
Copy after login

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
Copy after login

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");
Copy after login

Steps 2: Undo existing bookmarks

##

doc.Bookmarks.RemoveAt(0);
Copy after login

Step 3: Save the file

doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Remove Bookmark.docx");
Copy after login

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");
        }
    }
}
Copy after login


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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template