C # manipulating Word bookmarks (i) inserting, deleting bookmarks

Source: Internet
Author: User

Profile

Bookmark settings can help us to quickly locate a paragraph of text, easy to use, but also save time. The most common way to bookmark a Word document is to insert a bookmark directly into a text, in the following example, in addition to the method of inserting bookmarks, we will introduce some of the ways to manipulate bookmarks, including inserting pictures, tables, and bookmarks, deleting bookmarks, and so on. The example highlights are as follows:

    1. Insert Bookmark
    2. Insert Picture to Bookmark
    3. Insert Table to Bookmark
    4. Delete Bookmark
      4.1 Deleting bookmarks
      4.2 Delete the paragraph text where the bookmark is located
Using tools
    • Free Spire.doc for. NET 6.3 (Community Edition)
    • Visual Studio
      PS: after downloading and installing the class library, note that adding reference SPIRE.DOC.DL to the project program, DLL files can be obtained in the Bin folder of the installation path.
"Example 1" Insert Bookmark

C#

using System;using Spire.Doc;using Spire.Doc.Documents;namespace WordBookmark{    class Bookmark    {        static void Main(string[] args)        {            //实例化Document类,加载文档            Document document = new Document();            document.LoadFromFile("test.docx");            //插入书签到指定段落,并命名书签            Section section = document.Sections[0];            section.Paragraphs[1].AppendBookmarkStart("Bookmark1");            section.Paragraphs[2].AppendBookmarkEnd("Bookmark1");            //保存并打开文档             document.SaveToFile("InsertBookmark.docx", FileFormat.Docx);            System.Diagnostics.Process.Start("InsertBookmark.docx");        }    }}

Test results:

"Example 2" inserts a picture into the paragraph where the bookmark is located

C#

Using spire.doc;using spire.doc.documents;using spire.doc.fields;using system.drawing;namespace InsertImgToBookmark _doc{class Program {static void Main (string[] args) {//Create instance, load document document DOC            ument = new Document (); Document.            LoadFromFile ("Test.docx");            Instantiate the Bookmarksnavigator class, specifying the bookmark that you want to add the picture to "Bookmark1" bookmarksnavigator bn = new Bookmarksnavigator (document); Bn.            MoveToBookmark ("Bookmark1", true, true); Add a paragraph, load the picture and insert it into paragraph section section0 = document.            AddSection (); Paragraph Paragraph = Section0.            Addparagraph ();            Image image = Image.FromFile ("Img.png"); Docpicture picture = paragraph.            Appendpicture (image); Bn.            InsertParagraph (paragraph); Document.            Sections.remove (Section0);            Save the document and open string output = "Result.docx"; Document.            SaveToFile (output, fileformat.docx); System.Diagnostics.Process.Start (Output); }    }}

Picture Insert Effect:

"Example 3" inserts a table into the paragraph where the bookmark is located

C#

Using spire.doc;using spire.doc.documents;using System.data;namespace inserttabletobookmark_doc{class Program {            static void Main (string[] args) {//Create document, load file doc = new document (); Doc.            LoadFromFile ("Test.docx");            Create Table Object Table table = new Table (doc, True);            Create analog data DataTable dt = new DataTable (); Dt.            Columns.Add ("PI", typeof (String)); Dt.            Columns.Add ("Type", typeof (String)); Dt.            Columns.Add ("Nov.", typeof (String)); Dt.                                 Columns.Add ("YTD", typeof (String)); Dt.            Rows.Add (new string[] {"PI", "Type", "Nov.", "YTD"}); Dt.            Rows.Add (new string[] {"1", "BAIC ec-series", "15.719", "64.910"}); Dt.            Rows.Add (new string[] {"2", "Tesla Model S", "3.889", "46.951"}); Dt.            Rows.Add (new string[] {"3", "Nissan Leaf", "2.817", "46.423"}); Populate the table with data. Resetcells (dt. RowS.count, dt.            Columns.count); for (int i = 0; i < dt. Rows.Count; i++) {for (int j = 0; j < dt. Columns.count; J + +) {table. Rows[i]. CELLS[J]. Addparagraph (). AppendText (dt. ROWS[I][J].                ToString ());            }}//Gets the specified bookmark location bookmarksnavigator navigator = new Bookmarksnavigator (DOC); Navigator.            MoveToBookmark ("origin2"); Add the table to Textbodypart textbodypart part = navigator.            Getbookmarkcontent (); Part.            Bodyitems.add (table); Replaces the bookmark content navigator.            Replacebookmarkcontent (part); Save and open file Doc.            SaveToFile ("Output.docx", fileformat.docx2013);        System.Diagnostics.Process.Start ("Output.docx"); }    }}

Table Insert Effect:

"Example 4" deleting bookmarks

C#

using Spire.Doc;using Spire.Doc.Documents;namespace Removing{    class Program    {        static void Main(string[] args)        {            //实例化Document类,加载文档            Document doc = new Document();            doc.LoadFromFile("sample.docx");            //创建BookmarksNavigator实例            BookmarksNavigator navigator = new BookmarksNavigator(doc);            //指向特定的书签            navigator.MoveToBookmark("origin2");            //删除书签中的内容            navigator.DeleteBookmarkContent(false);            //删除书签            doc.Bookmarks.Remove(doc.Bookmarks.FindByName("bookmark1"));            //保存并打开文档            doc.SaveToFile("RemoveBookmark.docx", FileFormat.Docx);            System.Diagnostics.Process.Start("RemoveBookmark.docx");        }    }}

Test results:
The test document is as follows


After you delete a bookmark:

The above content for this "C # operation Word Bookmark Method" Introduction, if you want to reprint, please specify the source.

C # manipulating Word bookmarks (i) inserting, deleting bookmarks

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.