Skip to content

Commit cb1dbe0

Browse files
Merge pull request #502 from SyncfusionExamples/ES-1001738_Replace_HTML_string_while-mailmerge
ES-1001738-Added DocIO Sample for replace HTML string with Merge field
2 parents 054dab2 + 2e5f9c8 commit cb1dbe0

3 files changed

Lines changed: 75 additions & 42 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: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Replace_merge_field_with_HTML
88
{
99
class Program
1010
{
11-
static Dictionary<WParagraph, Dictionary<int, string>> paraToInsertHTML = new Dictionary<WParagraph, Dictionary<int, string>>();
11+
static Dictionary<WParagraph, List<KeyValuePair<int, string>>> paraToInsertHTML = new Dictionary<WParagraph, List<KeyValuePair<int, string>>>();
1212
static void Main(string[] args)
1313
{
1414
// Open the template Word document.
@@ -51,11 +51,15 @@ public static void MergeFieldEvent(object sender, MergeFieldEventArgs args)
5151
WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
5252
//Gets the current merge field index in the current paragraph.
5353
int mergeFieldIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField);
54-
//Maintain HTML in collection.
55-
Dictionary<int, string> fieldValues = new Dictionary<int, string>();
56-
fieldValues.Add(mergeFieldIndex, args.FieldValue.ToString());
57-
//Maintain paragraph in collection.
58-
paraToInsertHTML.Add(paragraph, fieldValues);
54+
// Check if this paragraph already has an entry in the dictionary.
55+
// If not, create a new list to store field index and value pairs.
56+
if (!paraToInsertHTML.TryGetValue(paragraph, out var fields))
57+
{
58+
fields = new List<KeyValuePair<int, string>>();
59+
paraToInsertHTML[paragraph] = fields;
60+
}
61+
// Add the current merge field's index and its value to the list
62+
fields.Add(new KeyValuePair<int, string>(mergeFieldIndex, args.FieldValue.ToString()));
5963
//Set field value as empty.
6064
args.Text = string.Empty;
6165
}
@@ -88,18 +92,25 @@ private static DataTable GetDataTable()
8892
private static void InsertHtml()
8993
{
9094
//Iterates through each item in the dictionary.
91-
foreach (KeyValuePair<WParagraph, Dictionary<int, string>> dictionaryItems in paraToInsertHTML)
95+
foreach (KeyValuePair<WParagraph, List<KeyValuePair<int, string>>> dictionaryItems in paraToInsertHTML)
9296
{
93-
WParagraph paragraph = dictionaryItems.Key as WParagraph;
94-
Dictionary<int, string> values = dictionaryItems.Value as Dictionary<int, string>;
95-
//Iterates through each value in the dictionary.
96-
foreach (KeyValuePair<int, string> valuePair in values)
97+
// Get the paragraph where HTML needs to be inserted.
98+
WParagraph paragraph = dictionaryItems.Key;
99+
// Get the list of (index, HTML string) pairs for this paragraph.
100+
List<KeyValuePair<int, string>> values = dictionaryItems.Value;
101+
// Iterate through the list in reverse order
102+
for (int i = values.Count - 1; i >= 0; i--)
97103
{
98-
int index = valuePair.Key;
99-
string fieldValue = valuePair.Value;
104+
// Get the index of the merge field within the paragraph.
105+
int index = values[i].Key;
106+
// Get the HTML content to insert.
107+
string fieldValue = values[i].Value;
108+
// Get paragraph position.
109+
int paragraphIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
100110
//Inserts HTML string at the same position of mergefield in Word document.
101-
paragraph.OwnerTextBody.InsertXHTML(fieldValue, paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph), index);
111+
paragraph.OwnerTextBody.InsertXHTML(fieldValue, paragraphIndex, index);
102112
}
113+
103114
}
104115
paraToInsertHTML.Clear();
105116
}

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)