1+ using Syncfusion . DocIO ;
2+ using Syncfusion . DocIO . DLS ;
3+
4+ namespace Get_bookmark_content_one_by_one
5+ {
6+ class Program
7+ {
8+ static void Main ( string [ ] args )
9+ {
10+ using ( FileStream fileStream = new FileStream ( Path . GetFullPath ( @"Data/Template.docx" ) , FileMode . Open , FileAccess . ReadWrite ) )
11+ {
12+ //Loads an existing Word document into DocIO instance.
13+ using ( WordDocument document = new WordDocument ( fileStream , FormatType . Automatic ) )
14+ {
15+ //Finds the first occurrence of a particular text in the document
16+ TextSelection textSelection = document . Find ( "they are considered one of the world's most loved animals." , false , true ) ;
17+ //Gets the found text as single text range
18+ WTextRange textRange = textSelection . GetAsOneRange ( ) ;
19+ //Add bookmark to the selected text range.
20+ //Create bookmarkstart and bookmarkend instance.
21+ int indexOfText = textRange . OwnerParagraph . Items . IndexOf ( textRange ) ;
22+ BookmarkStart bookmarkStart = new BookmarkStart ( document , "bkmk1" ) ;
23+ BookmarkEnd bookmarkEnd = new BookmarkEnd ( document , "bkmk1" ) ;
24+ //Add bookmarkstart before selected text.
25+ textRange . OwnerParagraph . Items . Insert ( indexOfText , bookmarkStart ) ;
26+ //Add bookmarkend after selected text
27+ textRange . OwnerParagraph . Items . Insert ( indexOfText + 2 , bookmarkEnd ) ;
28+ textSelection = document . Find ( "The table below lists the main characteristics the giant panda shares with bears and red pandas." , false , true ) ;
29+ //Gets the found text as single text range
30+ textRange = textSelection . GetAsOneRange ( ) ;
31+ //Add bookmark to the selected text range.
32+ //Create bookmarkstart and bookmarkend instance.
33+ indexOfText = textRange . OwnerParagraph . Items . IndexOf ( textRange ) ;
34+ bookmarkStart = new BookmarkStart ( document , "bkmk2" ) ;
35+ bookmarkEnd = new BookmarkEnd ( document , "bkmk2" ) ;
36+ //Add bookmarkstart before selected text.
37+ textRange . OwnerParagraph . Items . Insert ( indexOfText , bookmarkStart ) ;
38+ //Add bookmarkend after selected text
39+ textRange . OwnerParagraph . Items . Insert ( indexOfText + 2 , bookmarkEnd ) ;
40+ textSelection = document . Find ( "Did you know that the giant panda may actually be a raccoon" , false , true ) ;
41+ //Gets the found text as single text range
42+ textRange = textSelection . GetAsOneRange ( ) ;
43+ //Add bookmark to the selected text range.
44+ //Create bookmarkstart and bookmarkend instance.
45+ indexOfText = textRange . OwnerParagraph . Items . IndexOf ( textRange ) ;
46+ bookmarkStart = new BookmarkStart ( document , "bkmk3" ) ;
47+ bookmarkEnd = new BookmarkEnd ( document , "bkmk3" ) ;
48+ //Add bookmarkstart before selected text.
49+ textRange . OwnerParagraph . Items . Insert ( indexOfText , bookmarkStart ) ;
50+ //Add bookmarkend after selected text
51+ textRange . OwnerParagraph . Items . Insert ( indexOfText + 2 , bookmarkEnd ) ;
52+
53+ //Get all bookmarks from Word document using FindAllItemsByProperty
54+ //Find all bkmarkStart by EntityType in Word document.
55+ List < Entity > bkmarkStarts = document . FindAllItemsByProperty ( EntityType . BookmarkStart , null , null ) ;
56+ //Create an list Bookmarks of type string
57+ List < string > BookmarksContent = new List < string > ( ) ;
58+ //Iterate bookmarkCollection to get the bookmark content.
59+ foreach ( Entity bkmarkStart in bkmarkStarts )
60+ {
61+ BookmarkStart book = bkmarkStart as BookmarkStart ;
62+ //Get the bookmark name
63+ string name = book . Name ;
64+ //Creates the bookmark navigator instance to access the bookmark
65+ BookmarksNavigator bookmarkNavigator = new BookmarksNavigator ( document ) ;
66+ //Moves the virtual cursor to the location before the end of the bookmark
67+ bookmarkNavigator . MoveToBookmark ( name ) ;
68+ //Gets the bookmark content as worddocument
69+ WordDocumentPart part = bookmarkNavigator . GetContent ( ) ;
70+ WordDocument tempDoc = part . GetAsWordDocument ( ) ;
71+ //Get the bookmark content from the document.
72+ string text = tempDoc . GetText ( ) ;
73+ //Adds the bookmark content into the list
74+ BookmarksContent . Add ( text ) ;
75+ Console . WriteLine ( "Bookmark content: " ) ;
76+ Console . WriteLine ( text ) ;
77+ tempDoc . Close ( ) ;
78+ tempDoc . Dispose ( ) ;
79+ part . Close ( ) ;
80+
81+ }
82+ Console . ReadLine ( ) ;
83+ //Creates file stream.
84+ using ( FileStream outputStream = new FileStream ( Path . GetFullPath ( @"Output/Result.docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
85+ {
86+ //Saves the Word document to file stream.
87+ document . Save ( outputStream , FormatType . Docx ) ;
88+ }
89+ }
90+ }
91+ }
92+ }
93+ }
0 commit comments