11using Syncfusion . DocIO ;
22using Syncfusion . DocIO . DLS ;
33
4- namespace Find_text_add_bookmarks
4+ namespace FindTextAddBookmarks
55{
66 class Program
77 {
88 static void Main ( string [ ] args )
99 {
1010 using ( FileStream fileStream = new FileStream ( Path . GetFullPath ( @"Data/Template.docx" ) , FileMode . Open , FileAccess . ReadWrite ) )
1111 {
12- //Loads an existing Word document into DocIO instance.
1312 using ( WordDocument document = new WordDocument ( fileStream , FormatType . Automatic ) )
1413 {
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 ) ;
14+ // Define texts and corresponding bookmark names
15+ var textBookmarkPairs = new Dictionary < string , string >
16+ {
17+ { "they are considered one of the world's most loved animals." , "bkmk1" } ,
18+ { "The table below lists the main characteristics the giant panda shares with bears and red pandas." , "bkmk2" } ,
19+ { "Did you know that the giant panda may actually be a raccoon" , "bkmk3" }
20+ } ;
5221
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 )
22+ // Add bookmarks to specified texts
23+ foreach ( var pair in textBookmarkPairs )
6024 {
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 ( ) ;
25+ AddBookmarkToText ( document , pair . Key , pair . Value ) ;
26+ }
8027
28+ // Retrieve and display bookmark contents
29+ List < string > bookmarksContent = GetBookmarkContents ( document ) ;
30+ foreach ( var content in bookmarksContent )
31+ {
32+ Console . WriteLine ( "Bookmark content: " ) ;
33+ Console . WriteLine ( content ) ;
8134 }
82- Console . ReadLine ( ) ;
83- //Creates file stream.
35+
36+ // Save the modified document
8437 using ( FileStream outputStream = new FileStream ( Path . GetFullPath ( @"Output/Result.docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
8538 {
86- //Saves the Word document to file stream.
8739 document . Save ( outputStream , FormatType . Docx ) ;
8840 }
8941 }
9042 }
9143 }
44+ /// <summary>
45+ /// Adds a bookmark to a specific text in the document.
46+ /// </summary>
47+ private static void AddBookmarkToText ( WordDocument document , string searchText , string bookmarkName )
48+ {
49+ TextSelection textSelection = document . Find ( searchText , false , true ) ;
50+ if ( textSelection != null )
51+ {
52+ WTextRange textRange = textSelection . GetAsOneRange ( ) ;
53+ int indexOfText = textRange . OwnerParagraph . Items . IndexOf ( textRange ) ;
54+ textRange . OwnerParagraph . Items . Insert ( indexOfText , new BookmarkStart ( document , bookmarkName ) ) ;
55+ textRange . OwnerParagraph . Items . Insert ( indexOfText + 2 , new BookmarkEnd ( document , bookmarkName ) ) ;
56+ }
57+ }
58+
59+ /// <summary>
60+ /// Retrieves all bookmark contents from the document.
61+ /// </summary>
62+ private static List < string > GetBookmarkContents ( WordDocument document )
63+ {
64+ List < string > bookmarkContents = new List < string > ( ) ;
65+ foreach ( Entity entity in document . FindAllItemsByProperty ( EntityType . BookmarkStart , null , null ) )
66+ {
67+ if ( entity is BookmarkStart bookmarkStart )
68+ {
69+ var bookmarkNavigator = new BookmarksNavigator ( document ) ;
70+ bookmarkNavigator . MoveToBookmark ( bookmarkStart . Name ) ;
71+ WordDocumentPart part = bookmarkNavigator . GetContent ( ) ;
72+ WordDocument tempDoc = part . GetAsWordDocument ( ) ;
73+ bookmarkContents . Add ( tempDoc . GetText ( ) ) ;
74+
75+ tempDoc . Close ( ) ;
76+ tempDoc . Dispose ( ) ;
77+ part . Close ( ) ;
78+ }
79+ }
80+ return bookmarkContents ;
81+ }
9282 }
93- }
83+ }
0 commit comments