@@ -7,7 +7,7 @@ namespace Replace_Merge_field_with_HTML
77{
88 class Program
99 {
10- static Dictionary < WParagraph , Dictionary < int , string > > paraToInsertHTML = new Dictionary < WParagraph , Dictionary < int , string > > ( ) ;
10+ static Dictionary < WParagraph , List < KeyValuePair < int , string > > > paraToInsertHTML = new Dictionary < WParagraph , List < KeyValuePair < int , string > > > ( ) ;
1111 static void Main ( string [ ] args )
1212 {
1313 //Opens the template document.
@@ -45,11 +45,15 @@ public static void MergeFieldEvent(object sender, MergeFieldEventArgs args)
4545 WParagraph paragraph = args . CurrentMergeField . OwnerParagraph ;
4646 //Gets the current merge field index in the current paragraph.
4747 int mergeFieldIndex = paragraph . ChildEntities . IndexOf ( args . CurrentMergeField ) ;
48- //Maintain HTML in collection.
49- Dictionary < int , string > fieldValues = new Dictionary < int , string > ( ) ;
50- fieldValues . Add ( mergeFieldIndex , args . FieldValue . ToString ( ) ) ;
51- //Maintain paragraph in collection.
52- paraToInsertHTML . Add ( paragraph , fieldValues ) ;
48+ // Check if this paragraph already has an entry in the dictionary.
49+ // If not, create a new list to store field index and value pairs.
50+ if ( ! paraToInsertHTML . TryGetValue ( paragraph , out var fields ) )
51+ {
52+ fields = new List < KeyValuePair < int , string > > ( ) ;
53+ paraToInsertHTML [ paragraph ] = fields ;
54+ }
55+ // Add the current merge field's index and its value to the list
56+ fields . Add ( new KeyValuePair < int , string > ( mergeFieldIndex , args . FieldValue . ToString ( ) ) ) ;
5357 //Set field value as empty.
5458 args . Text = string . Empty ;
5559 }
@@ -82,18 +86,25 @@ private static DataTable GetDataTable()
8286 private static void InsertHtml ( )
8387 {
8488 //Iterates through each item in the dictionary.
85- foreach ( KeyValuePair < WParagraph , Dictionary < int , string > > dictionaryItems in paraToInsertHTML )
89+ foreach ( KeyValuePair < WParagraph , List < KeyValuePair < int , string > > > dictionaryItems in paraToInsertHTML )
8690 {
87- WParagraph paragraph = dictionaryItems . Key as WParagraph ;
88- Dictionary < int , string > values = dictionaryItems . Value as Dictionary < int , string > ;
89- //Iterates through each value in the dictionary.
90- foreach ( KeyValuePair < int , string > valuePair in values )
91+ // Get the paragraph where HTML needs to be inserted.
92+ WParagraph paragraph = dictionaryItems . Key ;
93+ // Get the list of (index, HTML string) pairs for this paragraph.
94+ List < KeyValuePair < int , string > > values = dictionaryItems . Value ;
95+ // Iterate through the list in reverse order
96+ for ( int i = values . Count - 1 ; i >= 0 ; i -- )
9197 {
92- int index = valuePair . Key ;
93- string fieldValue = valuePair . Value ;
98+ // Get the index of the merge field within the paragraph.
99+ int index = values [ i ] . Key ;
100+ // Get the HTML content to insert.
101+ string fieldValue = values [ i ] . Value ;
102+ // Get paragraph position.
103+ int paragraphIndex = paragraph . OwnerTextBody . ChildEntities . IndexOf ( paragraph ) ;
94104 //Inserts HTML string at the same position of mergefield in Word document.
95- paragraph . OwnerTextBody . InsertXHTML ( fieldValue , paragraph . OwnerTextBody . ChildEntities . IndexOf ( paragraph ) , index ) ;
105+ paragraph . OwnerTextBody . InsertXHTML ( fieldValue , paragraphIndex , index ) ;
96106 }
107+
97108 }
98109 paraToInsertHTML . Clear ( ) ;
99110 }
0 commit comments