C # manipulating Word bookmarks (ii)--reading, replacing word bookmarks

Source: Internet
Author: User

In the previous article that describes how C # inserts word bookmarks, insert pictures or tables into Word, delete word bookmarks, and so on, this article continues to describe the way in which C # operates word bookmarks. The following example will describe

    • How C # reads Word bookmarks
    • How C # replaces word bookmarks
Tool use
    • Spire.doc for. NET 6.1
    • Visual Studio Sample Code

      Bookmark content in the original document:

1. Read Word bookmarks

"C #"

Using spire.doc;using spire.doc.documents;using spire.doc.fields;using system;namespace GetTextOfBookmark_Doc{class program {static void Main (string[] args) {//Instantiating document class, loading test documents doc = NE            W Document (); Doc.            LoadFromFile ("Test.docx");            Initializes the Bookmarknavigator class object Bookmarksnavigator Navigator = new Bookmarksnavigator (DOC); Navigates to the specified bookmark location to get the document content of the bookmark location navigator.            MoveToBookmark ("Bookmark1"); Textbodypart Textbodypart = navigator.            Getbookmarkcontent ();            Iterates through the child items in the bookmark content and extracts the text information into a string type variable, string text = null;                    foreach (var item in Textbodypart.bodyitems) {if (item is Paragraph) { foreach (Var childobject in (item as Paragraph).                            Childobjects) {if (Childobject is TextRange) { Text + = (Childobject as TextRaNge).                        Text;            }}}}//console output text Console.WriteLine (text);        Console.ReadLine (); }    }}

The results read as follows:

2. Replace Bookmark contents

"C #"

Using spire.doc;using spire.doc.documents;using spire.doc.fields;namespace editorreplacebookmark_doc{class Program {static void Main (string[] args) {//Creating instance of document class, loading document document = new DOCU            ment (); Document.            LoadFromFile ("Test.docx"); Section sec = document. AddSection ();//Add section sec. Addparagraph (). AppendText ("Welcome back, \ n My friend!"); Add paragraph to section and add string contents//Get paragraph content paragraphbase firstreplacementparagraph = sec. Paragraphs[0].            Items.firstitem as Paragraphbase; Paragraphbase lastreplacementparagraph = sec. Paragraphs[sec. PARAGRAPHS.COUNT-1].            Items.lastitem as Paragraphbase; Instantiate classes Textbodyselection and Textbodypart textbodyselection selection = new Textbodyselection (firstreplacementparagr            APh, lastreplacementparagraph);            Textbodypart part = new Textbodypart (selection); Bookmarksnavigator bookmarknavigator = new BookmarksnavigAtor (document);//Instantiate Bookmarksnavigator class Bookmarknavigator.movetobookmark ("Bookmark1", true, true);//Navigate to Bookmark "book Mark1 "The position of the paragraph where bookmarknavigator.deletebookmarkcontent (true);//Delete the contents of the original bookmark location Bookmarknavigator.replaceb Ookmarkcontent (part, True, true);//replaces the contents of the original bookmark with the contents of the newly added paragraph and preserves the formatting//removal of the section document.            Sections.remove (SEC); Save the document and open it.            SaveToFile ("Replace bookmark. docx");        System.Diagnostics.Process.Start ("Replace bookmark. docx"); }    }}

Test results:

The above is the C # operation Word bookmark function of the supplementary introduction, if need to reprint, please specify the source.
(End of this article)

C # manipulating Word bookmarks (ii)--reading, replacing word 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.