diff --git a/Mail-Merge/Replace-Merge-field-with-HTML/.NET Framework/Replace-Merge-field-with-HTML/Program.cs b/Mail-Merge/Replace-Merge-field-with-HTML/.NET Framework/Replace-Merge-field-with-HTML/Program.cs index c88748e90..4ad37e99b 100644 --- a/Mail-Merge/Replace-Merge-field-with-HTML/.NET Framework/Replace-Merge-field-with-HTML/Program.cs +++ b/Mail-Merge/Replace-Merge-field-with-HTML/.NET Framework/Replace-Merge-field-with-HTML/Program.cs @@ -7,7 +7,7 @@ namespace Replace_Merge_field_with_HTML { class Program { - static Dictionary> paraToInsertHTML = new Dictionary>(); + static Dictionary>> paraToInsertHTML = new Dictionary>>(); static void Main(string[] args) { //Opens the template document. @@ -45,11 +45,15 @@ public static void MergeFieldEvent(object sender, MergeFieldEventArgs args) WParagraph paragraph = args.CurrentMergeField.OwnerParagraph; //Gets the current merge field index in the current paragraph. int mergeFieldIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField); - //Maintain HTML in collection. - Dictionary fieldValues = new Dictionary(); - fieldValues.Add(mergeFieldIndex, args.FieldValue.ToString()); - //Maintain paragraph in collection. - paraToInsertHTML.Add(paragraph, fieldValues); + // Check if this paragraph already has an entry in the dictionary. + // If not, create a new list to store field index and value pairs. + if (!paraToInsertHTML.TryGetValue(paragraph, out var fields)) + { + fields = new List>(); + paraToInsertHTML[paragraph] = fields; + } + // Add the current merge field's index and its value to the list + fields.Add(new KeyValuePair(mergeFieldIndex, args.FieldValue.ToString())); //Set field value as empty. args.Text = string.Empty; } @@ -82,18 +86,25 @@ private static DataTable GetDataTable() private static void InsertHtml() { //Iterates through each item in the dictionary. - foreach (KeyValuePair> dictionaryItems in paraToInsertHTML) + foreach (KeyValuePair>> dictionaryItems in paraToInsertHTML) { - WParagraph paragraph = dictionaryItems.Key as WParagraph; - Dictionary values = dictionaryItems.Value as Dictionary; - //Iterates through each value in the dictionary. - foreach (KeyValuePair valuePair in values) + // Get the paragraph where HTML needs to be inserted. + WParagraph paragraph = dictionaryItems.Key; + // Get the list of (index, HTML string) pairs for this paragraph. + List> values = dictionaryItems.Value; + // Iterate through the list in reverse order + for (int i = values.Count - 1; i >= 0; i--) { - int index = valuePair.Key; - string fieldValue = valuePair.Value; + // Get the index of the merge field within the paragraph. + int index = values[i].Key; + // Get the HTML content to insert. + string fieldValue = values[i].Value; + // Get paragraph position. + int paragraphIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph); //Inserts HTML string at the same position of mergefield in Word document. - paragraph.OwnerTextBody.InsertXHTML(fieldValue, paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph), index); + paragraph.OwnerTextBody.InsertXHTML(fieldValue, paragraphIndex, index); } + } paraToInsertHTML.Clear(); } diff --git a/Mail-Merge/Replace-Merge-field-with-HTML/.NET/Replace-merge-field-with-HTML/Program.cs b/Mail-Merge/Replace-Merge-field-with-HTML/.NET/Replace-merge-field-with-HTML/Program.cs index d965a2301..ab3935f6d 100644 --- a/Mail-Merge/Replace-Merge-field-with-HTML/.NET/Replace-merge-field-with-HTML/Program.cs +++ b/Mail-Merge/Replace-Merge-field-with-HTML/.NET/Replace-merge-field-with-HTML/Program.cs @@ -8,7 +8,7 @@ namespace Replace_merge_field_with_HTML { class Program { - static Dictionary> paraToInsertHTML = new Dictionary>(); + static Dictionary>> paraToInsertHTML = new Dictionary>>(); static void Main(string[] args) { // Open the template Word document. @@ -51,11 +51,15 @@ public static void MergeFieldEvent(object sender, MergeFieldEventArgs args) WParagraph paragraph = args.CurrentMergeField.OwnerParagraph; //Gets the current merge field index in the current paragraph. int mergeFieldIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField); - //Maintain HTML in collection. - Dictionary fieldValues = new Dictionary(); - fieldValues.Add(mergeFieldIndex, args.FieldValue.ToString()); - //Maintain paragraph in collection. - paraToInsertHTML.Add(paragraph, fieldValues); + // Check if this paragraph already has an entry in the dictionary. + // If not, create a new list to store field index and value pairs. + if (!paraToInsertHTML.TryGetValue(paragraph, out var fields)) + { + fields = new List>(); + paraToInsertHTML[paragraph] = fields; + } + // Add the current merge field's index and its value to the list + fields.Add(new KeyValuePair(mergeFieldIndex, args.FieldValue.ToString())); //Set field value as empty. args.Text = string.Empty; } @@ -88,18 +92,25 @@ private static DataTable GetDataTable() private static void InsertHtml() { //Iterates through each item in the dictionary. - foreach (KeyValuePair> dictionaryItems in paraToInsertHTML) + foreach (KeyValuePair>> dictionaryItems in paraToInsertHTML) { - WParagraph paragraph = dictionaryItems.Key as WParagraph; - Dictionary values = dictionaryItems.Value as Dictionary; - //Iterates through each value in the dictionary. - foreach (KeyValuePair valuePair in values) + // Get the paragraph where HTML needs to be inserted. + WParagraph paragraph = dictionaryItems.Key; + // Get the list of (index, HTML string) pairs for this paragraph. + List> values = dictionaryItems.Value; + // Iterate through the list in reverse order + for (int i = values.Count - 1; i >= 0; i--) { - int index = valuePair.Key; - string fieldValue = valuePair.Value; + // Get the index of the merge field within the paragraph. + int index = values[i].Key; + // Get the HTML content to insert. + string fieldValue = values[i].Value; + // Get paragraph position. + int paragraphIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph); //Inserts HTML string at the same position of mergefield in Word document. - paragraph.OwnerTextBody.InsertXHTML(fieldValue, paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph), index); + paragraph.OwnerTextBody.InsertXHTML(fieldValue, paragraphIndex, index); } + } paraToInsertHTML.Clear(); } diff --git a/Mail-Merge/Replace-Merge-field-with-HTML/Blazor/Blazor-WASM-app/Replace-Merge-field-with-HTML/Pages/DocIO.razor b/Mail-Merge/Replace-Merge-field-with-HTML/Blazor/Blazor-WASM-app/Replace-Merge-field-with-HTML/Pages/DocIO.razor index 5d38936d1..c45e31d6c 100644 --- a/Mail-Merge/Replace-Merge-field-with-HTML/Blazor/Blazor-WASM-app/Replace-Merge-field-with-HTML/Pages/DocIO.razor +++ b/Mail-Merge/Replace-Merge-field-with-HTML/Blazor/Blazor-WASM-app/Replace-Merge-field-with-HTML/Pages/DocIO.razor @@ -13,7 +13,7 @@ @code { @functions { - static Dictionary> paraToInsertHTML = new Dictionary>(); + static Dictionary>> paraToInsertHTML = new Dictionary>>(); async void ReplaceHTML() { using (Stream inputStream = await client.GetStreamAsync("Data/Template.docx")) @@ -61,11 +61,15 @@ WParagraph paragraph = args.CurrentMergeField.OwnerParagraph; //Gets the current merge field index in the current paragraph. int mergeFieldIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField); - //Maintain HTML in collection. - Dictionary fieldValues = new Dictionary(); - fieldValues.Add(mergeFieldIndex, args.FieldValue.ToString()); - //Maintain paragraph in collection. - paraToInsertHTML.Add(paragraph, fieldValues); + // Check if this paragraph already has an entry in the dictionary. + // If not, create a new list to store field index and value pairs. + if (!paraToInsertHTML.TryGetValue(paragraph, out var fields)) + { + fields = new List>(); + paraToInsertHTML[paragraph] = fields; + } + // Add the current merge field's index and its value to the list + fields.Add(new KeyValuePair(mergeFieldIndex, args.FieldValue.ToString())); //Set field value as empty. args.Text = string.Empty; } @@ -96,18 +100,25 @@ private static void InsertHtml() { //Iterates through each item in the dictionary. - foreach (KeyValuePair> dictionaryItems in paraToInsertHTML) + foreach (KeyValuePair>> dictionaryItems in paraToInsertHTML) { - WParagraph paragraph = dictionaryItems.Key as WParagraph; - Dictionary values = dictionaryItems.Value as Dictionary; - //Iterates through each value in the dictionary. - foreach (KeyValuePair valuePair in values) + // Get the paragraph where HTML needs to be inserted. + WParagraph paragraph = dictionaryItems.Key; + // Get the list of (index, HTML string) pairs for this paragraph. + List> values = dictionaryItems.Value; + // Iterate through the list in reverse order + for (int i = values.Count - 1; i >= 0; i--) { - int index = valuePair.Key; - string fieldValue = valuePair.Value; + // Get the index of the merge field within the paragraph. + int index = values[i].Key; + // Get the HTML content to insert. + string fieldValue = values[i].Value; + // Get paragraph position. + int paragraphIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph); //Inserts HTML string at the same position of mergefield in Word document. - paragraph.OwnerTextBody.InsertXHTML(fieldValue, paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph), index); + paragraph.OwnerTextBody.InsertXHTML(fieldValue, paragraphIndex, index); } + } paraToInsertHTML.Clear(); }