22using Syncfusion . DocIO ;
33using System . Data ;
44
5- // Dictionary to maintain paragraph and corresponding merge field index with HTML content.
5+ //Dictionary to maintain paragraph and corresponding merge field index with HTML content.
66Dictionary < WParagraph , Dictionary < int , string > > paraToInsertHTML = new Dictionary < WParagraph , Dictionary < int , string > > ( ) ;
77
88using ( FileStream fileStream = new FileStream ( Path . GetFullPath ( @"Data/Template.docx" ) , FileMode . Open , FileAccess . ReadWrite ) )
99{
1010 //Opens the template Word document.
1111 using ( WordDocument document = new WordDocument ( fileStream , FormatType . Automatic ) )
1212 {
13- //Registers a handler for the MergeField event to replace merge field with HTML content .
13+ //Creates the mail merge events handler to replace merge field with HTML.
1414 document . MailMerge . MergeField += new MergeFieldEventHandler ( MergeFieldEvent ) ;
15- //Retrieves data to perform mail merge.
15+ //Gets data to perform the mail merge.
1616 DataTable table = GetDataTable ( ) ;
17- //Executes mail merge with the data source .
17+ //Performs the mail merge .
1818 document . MailMerge . Execute ( table ) ;
19- //Inserts the HTML content into the corresponding paragraph.
19+ //Append HTML to paragraph.
2020 InsertHtml ( ) ;
21- //Removes the event handler after mail merge.
21+ //Removes the mail merge events handler .
2222 document . MailMerge . MergeField -= new MergeFieldEventHandler ( MergeFieldEvent ) ;
2323 using ( FileStream outputStream = new FileStream ( Path . GetFullPath ( @"Output/Result.docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
2424 {
25- //Saves the Modified Word document
25+ //Saves the Modified Word document.
2626 document . Save ( outputStream , FormatType . Docx ) ;
2727 }
2828 }
@@ -35,22 +35,22 @@ void MergeFieldEvent(object sender, MergeFieldEventArgs args)
3535{
3636 if ( args . FieldName . Equals ( "Logo" ) )
3737 {
38- //Gets the current paragraph containing the merge field.
38+ //Gets the current merge field owner paragraph .
3939 WParagraph paragraph = args . CurrentMergeField . OwnerParagraph ;
40- //Gets the index of the current merge field within the paragraph.
40+ //Gets the current merge field index in the current paragraph.
4141 int mergeFieldIndex = paragraph . ChildEntities . IndexOf ( args . CurrentMergeField ) ;
42- //Creates a dictionary to store the HTML content for the merge field .
42+ //Maintain HTML in collection .
4343 Dictionary < int , string > fieldValues = new Dictionary < int , string > ( ) ;
4444 fieldValues . Add ( mergeFieldIndex , args . FieldValue . ToString ( ) ) ;
45- //Adds the paragraph and HTML content to the collection.
45+ //Maintain paragraph in collection.
4646 paraToInsertHTML . Add ( paragraph , fieldValues ) ;
47- //Sets the merge field text as empty, so it is replaced with HTML .
47+ //Set field value as empty.
4848 args . Text = string . Empty ;
4949 }
5050}
5151
5252/// <summary>
53- /// Retrieves a data table for the mail merge operation.
53+ /// Get a data table for the mail merge operation.
5454/// </summary>
5555DataTable GetDataTable ( )
5656{
@@ -67,7 +67,7 @@ DataTable GetDataTable()
6767 datarow [ "Address" ] = "59 rue de I'Abbaye, Reims 51100, France" ;
6868 datarow [ "Phone" ] = "1-888-936-8638" ;
6969
70- //Reads HTML content from a file and assigns it to the "Logo" field .
70+ //Reads HTML string from the file .
7171 string htmlString = File . ReadAllText ( Path . GetFullPath ( @"Data/File.html" ) ) ;
7272 datarow [ "Logo" ] = htmlString ;
7373
@@ -79,24 +79,24 @@ DataTable GetDataTable()
7979/// </summary>
8080void InsertHtml ( )
8181{
82- //Iterates through each paragraph and field value in the dictionary.
82+ //Iterates through each item in the dictionary.
8383 foreach ( KeyValuePair < WParagraph , Dictionary < int , string > > dictionaryItems in paraToInsertHTML )
8484 {
8585 WParagraph paragraph = dictionaryItems . Key as WParagraph ;
8686 Dictionary < int , string > values = dictionaryItems . Value as Dictionary < int , string > ;
87-
87+ //Iterates through each value in the dictionary.
8888 foreach ( KeyValuePair < int , string > valuePair in values )
8989 {
9090 int index = valuePair . Key ;
9191 string fieldValue = valuePair . Value ;
9292
93- //Hooks the ImageNodeVisited event to resolve images within HTML content.
93+ //Subscribe the ImageNodeVisited event to resolve images within HTML content.
9494 paragraph . Document . HTMLImportSettings . ImageNodeVisited += OpenImage ;
9595
96- //Inserts the HTML content at the position of the merge field in the paragraph .
96+ //Inserts an HTML string at the same position of mergefield in a Word document .
9797 paragraph . OwnerTextBody . InsertXHTML ( fieldValue , paragraph . OwnerTextBody . ChildEntities . IndexOf ( paragraph ) , index ) ;
9898
99- //Unhooks the ImageNodeVisited event after processing.
99+ //Unsubscribe the ImageNodeVisited event after processing.
100100 paragraph . Document . HTMLImportSettings . ImageNodeVisited -= OpenImage ;
101101 }
102102 }
@@ -110,5 +110,5 @@ void InsertHtml()
110110void OpenImage ( object sender , ImageNodeVisitedEventArgs args )
111111{
112112 //Reads the image from the specified URI path and assigns it to the image stream.
113- args . ImageStream = System . IO . File . OpenRead ( args . Uri ) ;
113+ args . ImageStream = File . OpenRead ( args . Uri ) ;
114114}
0 commit comments