@@ -11,78 +11,80 @@ class Program
1111 static void Main ( string [ ] args )
1212 {
1313 //Load an existing Word document into DocIO instance.
14- FileStream fileStreamPath = new FileStream ( Path . GetFullPath ( @"Data/Template.docx" ) , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) ;
15- using ( WordDocument document = new WordDocument ( fileStreamPath , FormatType . Docx ) )
14+ using ( FileStream fileStreamPath = new FileStream ( Path . GetFullPath ( @"Data/Template.docx" ) , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
1615 {
17-
18- //Finds all the placeholder text in the Word document.
19- TextSelection [ ] textSelections = document . FindAll ( new Regex ( "<<(.*)>>" ) ) ;
20- if ( textSelections != null )
16+ using ( WordDocument document = new WordDocument ( fileStreamPath , FormatType . Docx ) )
2117 {
22- #region Insert bookmarks at placeholders
23- //Unique ID for each bookmark.
24- int bkmkId = 1 ;
25- //Collection to hold the inserted bookmarks.
26- List < string > bookmarks = new List < string > ( ) ;
27- //Iterate each text selection.
28- for ( int i = 0 ; i < textSelections . Length ; i ++ )
18+
19+ //Finds all the placeholder text in the Word document.
20+ TextSelection [ ] textSelections = document . FindAll ( new Regex ( "<<(.*)>>" ) ) ;
21+ if ( textSelections != null )
2922 {
30- #region Insert bookmark start before the placeholder
31- //Get the placeholder as WTextRange.
32- WTextRange textRange = textSelections [ i ] . GetAsOneRange ( ) ;
33- //Get the index of the placeholder text.
34- WParagraph startParagraph = textRange . OwnerParagraph ;
35- int index = startParagraph . ChildEntities . IndexOf ( textRange ) ;
36- string bookmarkName = "Bookmark_" + bkmkId ;
37- //Add new bookmark to bookmarks collection.
38- bookmarks . Add ( bookmarkName ) ;
39- //Create bookmark start.
40- BookmarkStart bkmkStart = new BookmarkStart ( document , bookmarkName ) ;
41- //Insert the bookmark start before the start placeholder.
42- startParagraph . ChildEntities . Insert ( index , bkmkStart ) ;
43- //Remove the placeholder text.
44- textRange . Text = string . Empty ;
45- #endregion
23+ #region Insert bookmarks at placeholders
24+ //Unique ID for each bookmark.
25+ int bkmkId = 1 ;
26+ //Collection to hold the inserted bookmarks.
27+ List < string > bookmarks = new List < string > ( ) ;
28+ //Iterate each text selection.
29+ for ( int i = 0 ; i < textSelections . Length ; i ++ )
30+ {
31+ #region Insert bookmark start before the placeholder
32+ //Get the placeholder as WTextRange.
33+ WTextRange textRange = textSelections [ i ] . GetAsOneRange ( ) ;
34+ //Get the index of the placeholder text.
35+ WParagraph startParagraph = textRange . OwnerParagraph ;
36+ int index = startParagraph . ChildEntities . IndexOf ( textRange ) ;
37+ string bookmarkName = "Bookmark_" + bkmkId ;
38+ //Add new bookmark to bookmarks collection.
39+ bookmarks . Add ( bookmarkName ) ;
40+ //Create bookmark start.
41+ BookmarkStart bkmkStart = new BookmarkStart ( document , bookmarkName ) ;
42+ //Insert the bookmark start before the start placeholder.
43+ startParagraph . ChildEntities . Insert ( index , bkmkStart ) ;
44+ //Remove the placeholder text.
45+ textRange . Text = string . Empty ;
46+ #endregion
4647
47- #region Insert bookmark end after the placeholder
48- i ++ ;
49- //Get the placeholder as WTextRange.
50- textRange = textSelections [ i ] . GetAsOneRange ( ) ;
51- //Get the index of the placeholder text.
52- WParagraph endParagraph = textRange . OwnerParagraph ;
53- index = endParagraph . ChildEntities . IndexOf ( textRange ) ;
54- //Create bookmark end.
55- BookmarkEnd bkmkEnd = new BookmarkEnd ( document , bookmarkName ) ;
56- //Insert the bookmark end after the end placeholder.
57- endParagraph . ChildEntities . Insert ( index + 1 , bkmkEnd ) ;
58- bkmkId ++ ;
59- //Remove the placeholder text.
60- textRange . Text = string . Empty ;
61- #endregion
48+ #region Insert bookmark end after the placeholder
49+ i ++ ;
50+ //Get the placeholder as WTextRange.
51+ textRange = textSelections [ i ] . GetAsOneRange ( ) ;
52+ //Get the index of the placeholder text.
53+ WParagraph endParagraph = textRange . OwnerParagraph ;
54+ index = endParagraph . ChildEntities . IndexOf ( textRange ) ;
55+ //Create bookmark end.
56+ BookmarkEnd bkmkEnd = new BookmarkEnd ( document , bookmarkName ) ;
57+ //Insert the bookmark end after the end placeholder.
58+ endParagraph . ChildEntities . Insert ( index + 1 , bkmkEnd ) ;
59+ bkmkId ++ ;
60+ //Remove the placeholder text.
61+ textRange . Text = string . Empty ;
62+ #endregion
6263
63- }
64- #endregion
65- #region Split bookmark content into separate documents
66- BookmarksNavigator bookmarksNavigator = new BookmarksNavigator ( document ) ;
67- int fileIndex = 1 ;
68- foreach ( string bookmark in bookmarks )
69- {
70- //Move the virtual cursor to the location before the end of the bookmark.
71- bookmarksNavigator . MoveToBookmark ( bookmark ) ;
72- //Get the bookmark content as WordDocumentPart.
73- WordDocumentPart wordDocumentPart = bookmarksNavigator . GetContent ( ) ;
74- //Save the WordDocumentPart as separate Word document.
75- using ( WordDocument newDocument = wordDocumentPart . GetAsWordDocument ( ) )
64+ }
65+ #endregion
66+ #region Split bookmark content into separate documents
67+ BookmarksNavigator bookmarksNavigator = new BookmarksNavigator ( document ) ;
68+ int fileIndex = 1 ;
69+ foreach ( string bookmark in bookmarks )
7670 {
77- //Save the Word document to file stream.
78- using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"Output/Placeholder_" + fileIndex + ".docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
71+ //Move the virtual cursor to the location before the end of the bookmark.
72+ bookmarksNavigator . MoveToBookmark ( bookmark ) ;
73+ //Get the bookmark content as WordDocumentPart.
74+ WordDocumentPart wordDocumentPart = bookmarksNavigator . GetContent ( ) ;
75+ //Save the WordDocumentPart as separate Word document.
76+ using ( WordDocument newDocument = wordDocumentPart . GetAsWordDocument ( ) )
7977 {
80- newDocument . Save ( outputFileStream , FormatType . Docx ) ;
78+ //Save the Word document to file stream.
79+ using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"Output/Placeholder_" + fileIndex + ".docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
80+ {
81+ newDocument . Save ( outputFileStream , FormatType . Docx ) ;
82+ }
8183 }
84+ fileIndex ++ ;
8285 }
83- fileIndex ++ ;
86+ #endregion
8487 }
85- #endregion
8688 }
8789 }
8890 }
0 commit comments