@@ -7,82 +7,90 @@ internal class Program
77 {
88 static void Main ( string [ ] args )
99 {
10- FileStream inputOne = new FileStream ( Path . GetFullPath ( "Data/SourceDocument.docx" ) , FileMode . Open , FileAccess . Read ) ;
11- WordDocument inputOneDoc = new WordDocument ( inputOne , FormatType . Docx ) ;
12- FileStream inputTwo = new FileStream ( Path . GetFullPath ( @"Data/DestinationDocument.docx" ) , FileMode . Open , FileAccess . Read ) ;
13- WordDocument inputTwoDoc = new WordDocument ( inputTwo , FormatType . Docx ) ;
14- DocxReplaceTextWithDocPart ( inputOneDoc , inputTwoDoc , "Text one" , "bkmk1" ) ;
15- DocxReplaceTextWithDocPart ( inputOneDoc , inputTwoDoc , "Text two" , "bkmk2" ) ;
16- FileStream output = new FileStream ( Path . GetFullPath ( "Output/Output.docx" ) , FileMode . Create , FileAccess . Write ) ;
17- inputOneDoc . Save ( output , FormatType . Docx ) ;
18- inputOneDoc . Close ( ) ;
19- inputTwoDoc . Close ( ) ;
20- inputOne . Close ( ) ;
21- inputTwo . Close ( ) ;
22- output . Close ( ) ;
10+ // Open the source Word document for reading using a file stream.
11+ using ( FileStream sourceStream = new FileStream ( Path . GetFullPath ( "Data/SourceDocument.docx" ) , FileMode . Open , FileAccess . Read ) )
12+ {
13+ // Load the source document from the stream.
14+ using ( WordDocument sourceDocument = new WordDocument ( sourceStream , FormatType . Docx ) )
15+ {
16+ // Open the destination Word document for reading using another file stream.
17+ using ( FileStream destinationStream = new FileStream ( Path . GetFullPath ( @"Data/DestinationDocument.docx" ) , FileMode . Open , FileAccess . Read ) )
18+ {
19+ // Load the destination document from the stream.
20+ using ( WordDocument destinationDocument = new WordDocument ( destinationStream , FormatType . Docx ) )
21+ {
22+ // Replace text "Text one" in the source document with content from the bookmark "bkmk1" in the destination document.
23+ DocxReplaceTextWithDocPart ( sourceDocument , destinationDocument , "Text one" , "bkmk1" ) ;
24+ // Replace text "Text two" in the source document with content from the bookmark "bkmk2" in the destination document.
25+ DocxReplaceTextWithDocPart ( sourceDocument , destinationDocument , "Text two" , "bkmk2" ) ;
26+ // Open a stream to save the modified source document as a new file.
27+ using ( FileStream output = new FileStream ( Path . GetFullPath ( "Output/Output.docx" ) , FileMode . Create , FileAccess . Write ) )
28+ {
29+ // Save the modified source document to the output stream in DOCX format.
30+ sourceDocument . Save ( output , FormatType . Docx ) ;
31+ }
32+ }
33+ }
34+ }
35+ }
2336 }
2437
38+ /// <summary>
39+ /// Replaces specific text in a Word document with bookmarked content from another document, maintaining formatting.
40+ /// </summary>
2541 private static void DocxReplaceTextWithDocPart ( WordDocument document , WordDocument sourceDoc , string tokenToFind , string textBookmark )
2642 {
2743 string bookmarkRef = textBookmark + "_bm" ;
2844
29- //Find the start token.
45+ // Find the start token in the document .
3046 TextSelection start = document . Find ( tokenToFind , true , true ) ;
3147 if ( start != null )
3248 {
3349 WTextRange startText = start . GetAsOneRange ( ) ;
3450 WParagraph startParagraph = startText . OwnerParagraph ;
35- //Get the item index of the start token in the paragraph
51+ // Get the index of the start token in the paragraph.
3652 int index = startParagraph . Items . IndexOf ( startText ) ;
37- //Remove the start token at the specified index
53+ // Remove the start token at the specified index.
3854 startParagraph . Items . Remove ( startText ) ;
39- //Create and insert a Bookmark start at the index of the start token
55+ // Create and insert a BookmarkStart at the index of the start token.
4056 BookmarkStart bookmarkStart = new BookmarkStart ( document , bookmarkRef ) ;
4157 startParagraph . Items . Insert ( index , bookmarkStart ) ;
4258 startParagraph . AppendBookmarkEnd ( bookmarkRef ) ;
4359
44- //Open the document that contains the text to replace
45- //For instance, the document contains Bookmark named "DocIO" and the contents of that
46- //bookmark should replace the content in above document
47- //Creates the bookmark navigator instance to access the bookmark
60+ // Check if the bookmark exists in the source document.
4861 if ( sourceDoc . Bookmarks . FindByName ( textBookmark ) != null )
4962 {
63+ // Access the bookmark in the source document.
5064 BookmarksNavigator bookmarksNavigator = new BookmarksNavigator ( sourceDoc ) ;
51- //Moves the virtual cursor to the location before the end of the bookmark "DocIO"
5265 bookmarksNavigator . MoveToBookmark ( textBookmark ) ;
53- //Gets the bookmark content
66+ // Get the bookmark content.
5467 WordDocumentPart wordDocumentPart = bookmarksNavigator . GetContent ( ) ;
55- //Creates the bookmark navigator instance to access the bookmark
5668 bookmarksNavigator = new BookmarksNavigator ( document ) ;
57- //Moves the virtual cursor to the location before the end of the bookmark "Bookmark"
5869 bookmarksNavigator . MoveToBookmark ( bookmarkRef ) ;
5970
60- //Get the destination para before replacing
71+ // Get the destination paragraph before replacing.
6172 WParagraph destinationPara = bookmarksNavigator . CurrentBookmark . BookmarkStart . OwnerParagraph ;
62- //Get the list style name
73+ // Store the list style, first line indent, and left indent of the paragraph.
6374 string listStyleName = destinationPara . ListFormat . CustomStyleName ;
64- //Get the first line indent value
6575 float firstLineIndent = destinationPara . ParagraphFormat . FirstLineIndent ;
66- //Get the left indent value
6776 float leftIndent = destinationPara . ParagraphFormat . LeftIndent ;
6877
69- //Replace the selected text with another Word document content
78+ // Replace the selected text with the bookmark content from the source document.
7079 bookmarksNavigator . ReplaceContent ( wordDocumentPart ) ;
71- //Apply the list style, first line indent and left indent values after replacing
80+ // Reapply the list style and indent values after replacement.
7281 destinationPara . ListFormat . ApplyStyle ( listStyleName ) ;
7382 destinationPara . ParagraphFormat . FirstLineIndent = firstLineIndent ;
7483 destinationPara . ParagraphFormat . LeftIndent = leftIndent ;
7584 }
7685 else
7786 {
78- //Creates the bookmark navigator instance to access the bookmark
87+ // If the bookmark is not found, replace the bookmark content with an empty string.
7988 BookmarksNavigator bookmarksNavigator = new BookmarksNavigator ( document ) ;
80- //Moves the virtual cursor to the location before the end of the bookmark "Bookmark"
8189 bookmarksNavigator . MoveToBookmark ( bookmarkRef ) ;
82- //Replace the selected text with another Word document content
8390 bookmarksNavigator . ReplaceBookmarkContent ( string . Empty , true ) ;
8491 }
8592 }
8693 }
94+
8795 }
8896}
0 commit comments