Skip to content

Commit 2e5f9c8

Browse files
ES- 1001738changes added
1 parent bd4ccca commit 2e5f9c8

3 files changed

Lines changed: 51 additions & 29 deletions

File tree

  • Mail-Merge/Replace-Merge-field-with-HTML
    • .NET Framework/Replace-Merge-field-with-HTML
    • .NET/Replace-merge-field-with-HTML
    • Blazor/Blazor-WASM-app/Replace-Merge-field-with-HTML/Pages

Mail-Merge/Replace-Merge-field-with-HTML/.NET Framework/Replace-Merge-field-with-HTML/Program.cs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Mail-Merge/Replace-Merge-field-with-HTML/.NET/Replace-merge-field-with-HTML/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void MergeFieldEvent(object sender, MergeFieldEventArgs args)
5959
paraToInsertHTML[paragraph] = fields;
6060
}
6161
// Add the current merge field's index and its value to the list
62-
fields.Add(new KeyValuePair<int, string>(mergeFieldIndex, args.FieldValue?.ToString()));
62+
fields.Add(new KeyValuePair<int, string>(mergeFieldIndex, args.FieldValue.ToString()));
6363
//Set field value as empty.
6464
args.Text = string.Empty;
6565
}

Mail-Merge/Replace-Merge-field-with-HTML/Blazor/Blazor-WASM-app/Replace-Merge-field-with-HTML/Pages/DocIO.razor

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@code {
1515
@functions {
16-
static Dictionary<WParagraph, Dictionary<int, string>> paraToInsertHTML = new Dictionary<WParagraph, Dictionary<int, string>>();
16+
static Dictionary<WParagraph, List<KeyValuePair<int, string>>> paraToInsertHTML = new Dictionary<WParagraph, List<KeyValuePair<int, string>>>();
1717
async void ReplaceHTML()
1818
{
1919
using (Stream inputStream = await client.GetStreamAsync("Data/Template.docx"))
@@ -61,11 +61,15 @@
6161
WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
6262
//Gets the current merge field index in the current paragraph.
6363
int mergeFieldIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField);
64-
//Maintain HTML in collection.
65-
Dictionary<int, string> fieldValues = new Dictionary<int, string>();
66-
fieldValues.Add(mergeFieldIndex, args.FieldValue.ToString());
67-
//Maintain paragraph in collection.
68-
paraToInsertHTML.Add(paragraph, fieldValues);
64+
// Check if this paragraph already has an entry in the dictionary.
65+
// If not, create a new list to store field index and value pairs.
66+
if (!paraToInsertHTML.TryGetValue(paragraph, out var fields))
67+
{
68+
fields = new List<KeyValuePair<int, string>>();
69+
paraToInsertHTML[paragraph] = fields;
70+
}
71+
// Add the current merge field's index and its value to the list
72+
fields.Add(new KeyValuePair<int, string>(mergeFieldIndex, args.FieldValue.ToString()));
6973
//Set field value as empty.
7074
args.Text = string.Empty;
7175
}
@@ -96,18 +100,25 @@
96100
private static void InsertHtml()
97101
{
98102
//Iterates through each item in the dictionary.
99-
foreach (KeyValuePair<WParagraph, Dictionary<int, string>> dictionaryItems in paraToInsertHTML)
103+
foreach (KeyValuePair<WParagraph, List<KeyValuePair<int, string>>> dictionaryItems in paraToInsertHTML)
100104
{
101-
WParagraph paragraph = dictionaryItems.Key as WParagraph;
102-
Dictionary<int, string> values = dictionaryItems.Value as Dictionary<int, string>;
103-
//Iterates through each value in the dictionary.
104-
foreach (KeyValuePair<int, string> valuePair in values)
105+
// Get the paragraph where HTML needs to be inserted.
106+
WParagraph paragraph = dictionaryItems.Key;
107+
// Get the list of (index, HTML string) pairs for this paragraph.
108+
List<KeyValuePair<int, string>> values = dictionaryItems.Value;
109+
// Iterate through the list in reverse order
110+
for (int i = values.Count - 1; i >= 0; i--)
105111
{
106-
int index = valuePair.Key;
107-
string fieldValue = valuePair.Value;
112+
// Get the index of the merge field within the paragraph.
113+
int index = values[i].Key;
114+
// Get the HTML content to insert.
115+
string fieldValue = values[i].Value;
116+
// Get paragraph position.
117+
int paragraphIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
108118
//Inserts HTML string at the same position of mergefield in Word document.
109-
paragraph.OwnerTextBody.InsertXHTML(fieldValue, paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph), index);
119+
paragraph.OwnerTextBody.InsertXHTML(fieldValue, paragraphIndex, index);
110120
}
121+
111122
}
112123
paraToInsertHTML.Clear();
113124
}

0 commit comments

Comments
 (0)