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
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