1+ using Syncfusion . DocIO ;
2+ using Syncfusion . DocIO . DLS ;
3+
4+ namespace Rename_bookmark
5+ {
6+ class Program
7+ {
8+ static void Main ( string [ ] args )
9+ {
10+ using ( FileStream fileStreamPath = new FileStream ( Path . GetFullPath ( @"Data/Template.docx" ) , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
11+ {
12+ //Opens an existing Word document.
13+ using ( WordDocument document = new WordDocument ( fileStreamPath , FormatType . Automatic ) )
14+ {
15+ //Replace Bookmark name
16+ ReplaceBookmarkName ( document , "Northwind" , "New_Bookmark" ) ;
17+ //Creates file stream.
18+ using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"Output/Output.docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
19+ {
20+ //Saves the Word document to file stream.
21+ document . Save ( outputFileStream , FormatType . Docx ) ;
22+ }
23+ }
24+ }
25+ }
26+ #region Replace Bookmark name
27+ /// <summary>
28+ /// Replace bookmark name
29+ /// </summary>
30+ /// <param name="document">Input Word document.</param>
31+ /// <param name="existingbookmarkName">The name of the bookmark to replace.</param>
32+ /// <param name="replaceBookmarkName">The new name for the bookmark.</param>
33+
34+ private static void ReplaceBookmarkName ( WordDocument document , string existingbookmarkName , string replaceBookmarkName )
35+ {
36+ //Gets the bookmark instance by using FindByName method of BookmarkCollection with bookmark name
37+ Bookmark bookmark = document . Bookmarks . FindByName ( existingbookmarkName ) ;
38+ // No bookmark found, return immediately
39+ if ( bookmark == null )
40+ return ;
41+ //Gets owner paragraph of the bookmark
42+ WParagraph bookmarkStartParagraph = bookmark . BookmarkStart . Owner as WParagraph ;
43+ //Gets index of the bookmark start and end
44+ int boomarkStartIndex = bookmarkStartParagraph . ChildEntities . IndexOf ( bookmark . BookmarkStart ) ;
45+ int boomarkEndIndex = 0 ;
46+ //Gets bookmark end paragraph
47+ WParagraph bookmarkEndParagraph = bookmark . BookmarkEnd . Owner as WParagraph ;
48+ //Checks whether the bookmark start and end is in same paragraph
49+ if ( bookmarkEndParagraph == bookmarkStartParagraph )
50+ boomarkEndIndex = bookmarkStartParagraph . ChildEntities . IndexOf ( bookmark . BookmarkEnd ) ;
51+ else
52+ boomarkEndIndex = bookmarkEndParagraph . ChildEntities . IndexOf ( bookmark . BookmarkEnd ) ;
53+ //Removes the bookmark from Word document.
54+ document . Bookmarks . Remove ( bookmark ) ;
55+ //Inserts new bookmark in place of deleted bookmark
56+ bookmarkStartParagraph . ChildEntities . Insert ( boomarkStartIndex , bookmarkStartParagraph . AppendBookmarkStart ( replaceBookmarkName ) ) ;
57+ //Inserts bookmark end in corresponding paragraph.
58+ if ( bookmarkEndParagraph == bookmarkStartParagraph )
59+ bookmarkStartParagraph . ChildEntities . Insert ( boomarkEndIndex , bookmarkStartParagraph . AppendBookmarkEnd ( replaceBookmarkName ) ) ;
60+ else
61+ bookmarkEndParagraph . ChildEntities . Insert ( boomarkEndIndex , bookmarkEndParagraph . AppendBookmarkEnd ( replaceBookmarkName ) ) ;
62+ }
63+ #endregion
64+ }
65+ }
0 commit comments