vb.net instances of adding/revoking bookmarks to Word documents in C #

Source: Internet
Author: User
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


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.

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            = 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.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\ 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 ();           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 # :


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);            System.Diagnostics.Process.Start ("Remove bookmark.docx");}}}    

VB.net:


imports Spire.docnamespace removing class program Private Share D 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 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.