11using Syncfusion . DocIO ;
22using Syncfusion . DocIO . DLS ;
3- using System ;
3+ using System . Collections . Generic ;
44using System . IO ;
5+ using System . Text . RegularExpressions ;
56
67namespace Split_a_document_by_placeholder_text
78{
@@ -10,35 +11,78 @@ class Program
1011 static void Main ( string [ ] args )
1112 {
1213 //Load an existing Word document into DocIO instance.
13- FileStream fileStreamPath = new FileStream ( Path . GetFullPath ( @"../../../Template.docx" ) , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) ;
14+ FileStream fileStreamPath = new FileStream ( Path . GetFullPath ( @"../../../Data/ Template.docx" ) , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) ;
1415 using ( WordDocument document = new WordDocument ( fileStreamPath , FormatType . Docx ) )
1516 {
16- String [ ] findPlaceHolderWord = new string [ ] { "[First Content Start]" , "[Second Content Start]" , "[Third Content Start]" } ;
17- for ( int i = 0 ; i < findPlaceHolderWord . Length ; i ++ )
17+
18+ //Finds all the placeholder text in the Word document.
19+ TextSelection [ ] textSelections = document . FindAll ( new Regex ( "<<(.*)>>" ) ) ;
20+ if ( textSelections != null )
1821 {
19- //Get the start placeholder paragraph in the document.
20- WParagraph startParagraph = document . Find ( findPlaceHolderWord [ i ] , true , true ) . GetAsOneRange ( ) . OwnerParagraph ;
21- //Get the end placeholder paragraph in the document.
22- WParagraph endParagraph = document . Find ( findPlaceHolderWord [ i ] . Replace ( "Start" , "End" ) , true , true ) . GetAsOneRange ( ) . OwnerParagraph ;
23- //Get the text body.
24- WTextBody textBody = startParagraph . OwnerTextBody ;
25- //Get the start PlaceHolder index.
26- int startPlaceHolderIndex = textBody . ChildEntities . IndexOf ( startParagraph ) ;
27- //Get the end PlaceHolder index.
28- int endPlaceHolderIndex = textBody . ChildEntities . IndexOf ( endParagraph ) ;
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 ++ )
29+ {
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
46+
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
2962
30- //Create a new Word document.
31- WordDocument newDocument = new WordDocument ( ) ;
32- newDocument . AddSection ( ) ;
33- //Add the retrieved content into another new document.
34- for ( int j = startPlaceHolderIndex + 1 ; j < endPlaceHolderIndex ; j ++ )
35- newDocument . LastSection . Body . ChildEntities . Add ( textBody . ChildEntities [ j ] . Clone ( ) ) ;
36- //Save the Word document to file stream.
37- using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"../../../Result" + i + ".docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
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 )
3869 {
39-
40- newDocument . Save ( outputFileStream , FormatType . Docx ) ;
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 ( ) )
76+ {
77+ //Save the Word document to file stream.
78+ using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"../../../Placeholder_" + fileIndex + ".docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
79+ {
80+ newDocument . Save ( outputFileStream , FormatType . Docx ) ;
81+ }
82+ }
83+ fileIndex ++ ;
4184 }
85+ #endregion
4286 }
4387 }
4488 }
0 commit comments