Skip to content

Commit a01c853

Browse files
Added sample for Mail-merge-with-JSON
1 parent 93f8f98 commit a01c853

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mail-merge-with-JSON", "Mail-merge-with-JSON\Mail-merge-with-JSON.csproj", "{D3AF529E-DB54-4294-A876-DD42E1E472D0}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {58137FF9-5AE1-4514-9929-3A8A7DA1DFEB}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "Employee": [{"EmployeeId":1001,"Name":"Peter","Phone":"+122-2222222","City":"London"}]}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<RootNamespace>Mail_merge_with_JSON</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Newtonsoft.Json" Version="*" />
11+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using Newtonsoft.Json.Linq;
6+
7+
namespace Mail_merge_with_JSON
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"../../../Input.docx"), FileMode.Open, FileAccess.ReadWrite))
14+
{
15+
//Open the template document.
16+
using (WordDocument document = new WordDocument(fileStream, FormatType.Docx))
17+
{
18+
//Get the Json data details as DataTable
19+
List<object> jsonData = GetJsonData();
20+
21+
// Perform mail merge with the prepared data table.
22+
document.MailMerge.Execute(jsonData);
23+
24+
//Create file stream.
25+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Result.docx"), FileMode.Create, FileAccess.ReadWrite))
26+
{
27+
//Save the Word document to file stream.
28+
document.Save(outputStream, FormatType.Docx);
29+
}
30+
}
31+
}
32+
}
33+
#region Helper methods
34+
/// <summary>
35+
/// Prepares the data table from JSON data for processing.
36+
/// </summary>
37+
private static List<object> GetJsonData()
38+
{
39+
//Reads the JSON object from JSON file.
40+
JObject jsonObject = JObject.Parse(File.ReadAllText(Path.GetFullPath(@"../../../Json Data.json")));
41+
//Converts JSON object to Dictionary.
42+
IDictionary<string, object> data = GetData(jsonObject);
43+
return data["Employee"] as List<object>;
44+
}
45+
46+
/// <summary>
47+
/// Gets data from JSON object.
48+
/// </summary>
49+
/// <param name="jsonObject">JSON object.</param>
50+
/// <returns>Dictionary of data.</returns>
51+
private static IDictionary<string, object> GetData(JObject jsonObject)
52+
{
53+
Dictionary<string, object> dictionary = new Dictionary<string, object>();
54+
foreach (var item in jsonObject)
55+
{
56+
object keyValue = null;
57+
if (item.Value is JArray)
58+
keyValue = GetData((JArray)item.Value);
59+
else if (item.Value is JToken)
60+
keyValue = ((JToken)item.Value).ToObject<string>();
61+
dictionary.Add(item.Key, keyValue);
62+
}
63+
return dictionary;
64+
}
65+
/// <summary>
66+
/// Gets array of items from JSON array.
67+
/// </summary>
68+
/// <param name="jArray">JSON array.</param>
69+
/// <returns>List of objects.</returns>
70+
private static List<object> GetData(JArray jArray)
71+
{
72+
List<object> jArrayItems = new List<object>();
73+
foreach (var item in jArray)
74+
{
75+
object keyValue = null;
76+
if (item is JObject)
77+
keyValue = GetData((JObject)item);
78+
jArrayItems.Add(keyValue);
79+
}
80+
return jArrayItems;
81+
}
82+
#endregion
83+
}
84+
}

0 commit comments

Comments
 (0)