Skip to content

Commit f6cbae0

Browse files
authored
Merge pull request #6 from DharaniThangarasu/Modify-replace-merge-field-with-HTML-string-sample
Modified the example
2 parents 84e3d46 + c9b88c8 commit f6cbae0

File tree

1 file changed

+41
-13
lines changed
  • Replace-Merge-field-with-HTML/Console-App-.NET-Framework/Replace-Merge-field-with-HTML

1 file changed

+41
-13
lines changed

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

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
using Syncfusion.DocIO.DLS;
2+
using System.Collections.Generic;
23
using System.Data;
34
using System.IO;
45

56
namespace Replace_Merge_field_with_HTML
67
{
78
class Program
89
{
10+
static Dictionary<WParagraph, Dictionary<int, string>> paraToInsertHTML = new Dictionary<WParagraph, Dictionary<int, string>>();
911
static void Main(string[] args)
1012
{
11-
//Opens the template document
13+
//Opens the template document.
1214
using (WordDocument document = new WordDocument(Path.GetFullPath(@"../../Template.docx")))
1315
{
14-
//Creates mail merge events handler to replace merge field with HTML
16+
//Creates mail merge events handler to replace merge field with HTML.
1517
document.MailMerge.MergeField += new MergeFieldEventHandler(MergeFieldEvent);
16-
//Gets data to perform mail merge
18+
//Gets data to perform mail merge.
1719
DataTable table = GetDataTable();
18-
//Performs the mail merge
20+
//Performs the mail merge.
1921
document.MailMerge.Execute(table);
20-
//Removes mail merge events handler
22+
//Append HTML to paragraph.
23+
InsertHtml();
24+
//Removes mail merge events handler.
2125
document.MailMerge.MergeField -= new MergeFieldEventHandler(MergeFieldEvent);
22-
//Saves the Word document instance
26+
//Saves the Word document instance.
2327
document.Save(Path.GetFullPath(@"../../Sample.docx"));
2428
}
2529
System.Diagnostics.Process.Start(Path.GetFullPath(@"../../Sample.docx"));
@@ -37,13 +41,16 @@ public static void MergeFieldEvent(object sender, MergeFieldEventArgs args)
3741
{
3842
if (args.FieldName.Equals("ProductList"))
3943
{
40-
string text = args.FieldValue as string;
44+
//Gets the current merge field owner paragraph.
4145
WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
42-
int paraIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
43-
int fieldIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField);
44-
//Appends HTML string at the specified position of the document body contents
45-
paragraph.OwnerTextBody.InsertXHTML(args.FieldValue.ToString(), paraIndex, fieldIndex);
46-
//Resets the field value
46+
//Gets the current merge field index in the current paragraph.
47+
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);
53+
//Set field value as empty.
4754
args.Text = string.Empty;
4855
}
4956
}
@@ -64,11 +71,32 @@ private static DataTable GetDataTable()
6471
datarow["CustomerName"] = "Nancy Davolio";
6572
datarow["Address"] = "59 rue de I'Abbaye, Reims 51100, France";
6673
datarow["Phone"] = "1-888-936-8638";
67-
//Reads HTML string from the file
74+
//Reads HTML string from the file.
6875
string htmlString = File.ReadAllText(@"../../File.html");
6976
datarow["ProductList"] = htmlString;
7077
return dataTable;
7178
}
79+
/// <summary>
80+
/// Append HTML to paragraph.
81+
/// </summary>
82+
private static void InsertHtml()
83+
{
84+
//Iterates through each item in the dictionary.
85+
foreach (KeyValuePair<WParagraph, Dictionary<int, string>> dictionaryItems in paraToInsertHTML)
86+
{
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+
{
92+
int index = valuePair.Key;
93+
string fieldValue = valuePair.Value;
94+
//Inserts HTML string at the same position of mergefield in Word document.
95+
paragraph.OwnerTextBody.InsertXHTML(fieldValue, paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph), index);
96+
}
97+
}
98+
paraToInsertHTML.Clear();
99+
}
72100
#endregion
73101
}
74102
}

0 commit comments

Comments
 (0)