Skip to content

Commit 1d8122e

Browse files
Merge pull request #352 from SyncfusionExamples/ES-904874-NestedGroup-with-json-data
ES-904874- Add sample in Mail merge folder for NestedGroup-with-json-data
2 parents 4d61d00 + 9f5bbf6 commit 1d8122e

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-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}") = "NestedGroup-MailMerge-with-JSON", "NestedGroup-MailMerge-with-JSON\NestedGroup-MailMerge-with-JSON.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
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+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.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 = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"Organizations": [
3+
{
4+
"BranchName": "UK Office",
5+
"Address": "120 Hanover Sq.",
6+
"City": "London",
7+
"ZipCode": "WX1 6LT",
8+
"Country": "UK",
9+
"Departments": [
10+
{
11+
"DepartmentName": "Marketing",
12+
"Supervisor": "Nancy Davolio",
13+
"Employees": [
14+
{
15+
"EmployeeName": "Thomas Hardy",
16+
"EmployeeID": "1001",
17+
"JoinedDate": "05/27/1996"
18+
},
19+
{
20+
"EmployeeName": "Maria Anders",
21+
"EmployeeID": "1002",
22+
"JoinedDate": "04/10/1998"
23+
}
24+
]
25+
},
26+
{
27+
"DepartmentName": "Production",
28+
"Supervisor": "Andrew Fuller",
29+
"Employees": [
30+
{
31+
"EmployeeName": "Elizabeth Lincoln",
32+
"EmployeeID": "1003",
33+
"JoinedDate": "05/15/1996"
34+
},
35+
{
36+
"EmployeeName": "Antonio Moreno",
37+
"EmployeeID": "1004",
38+
"JoinedDate": "04/22/1996"
39+
}
40+
]
41+
}
42+
]
43+
}
44+
]
45+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>NestedGroup_MailMerge_with_JSON</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
11+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="Data\Data.json">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Data\Template.docx">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
<None Update="Output\.gitkeep">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using Newtonsoft.Json.Linq;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIO.DLS;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
7+
namespace NestedGroup_MailMerge_with_JSON
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.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+
//Creates the mail merge data table in order to perform mail merge
21+
MailMergeDataTable dataTable = new MailMergeDataTable("Organizations", jsonData);
22+
// Perform mail merge with the prepared data table.
23+
document.MailMerge.ExecuteNestedGroup(dataTable);
24+
//Create file stream.
25+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/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+
34+
#region Helper methods
35+
/// <summary>
36+
/// Prepares the data table from JSON data for processing.
37+
/// </summary>
38+
private static List<object> GetJsonData()
39+
{
40+
//Reads the JSON object from JSON file.
41+
JObject jsonObject = JObject.Parse(File.ReadAllText(Path.GetFullPath(@"Data/Data.json")));
42+
//Converts JSON object to Dictionary.
43+
44+
45+
IDictionary<string, object> data = GetData(jsonObject);
46+
return data["Organizations"] as List<object>;
47+
}
48+
49+
/// <summary>
50+
/// Gets data from JSON object.
51+
/// </summary>
52+
/// <param name="jsonObject">JSON object.</param>
53+
/// <returns>Dictionary of data.</returns>
54+
private static IDictionary<string, object> GetData(JObject jsonObject)
55+
{
56+
Dictionary<string, object> dictionary = new Dictionary<string, object>();
57+
foreach (var item in jsonObject)
58+
{
59+
object keyValue = null;
60+
if (item.Value is JArray)
61+
keyValue = GetData((JArray)item.Value);
62+
else if (item.Value is JToken)
63+
keyValue = ((JToken)item.Value).ToObject<string>();
64+
dictionary.Add(item.Key, keyValue);
65+
}
66+
return dictionary;
67+
}
68+
/// <summary>
69+
/// Gets array of items from JSON array.
70+
/// </summary>
71+
/// <param name="jArray">JSON array.</param>
72+
/// <returns>List of objects.</returns>
73+
private static List<object> GetData(JArray jArray)
74+
{
75+
List<object> jArrayItems = new List<object>();
76+
foreach (var item in jArray)
77+
{
78+
object keyValue = null;
79+
if (item is JObject)
80+
keyValue = GetData((JObject)item);
81+
jArrayItems.Add(keyValue);
82+
}
83+
return jArrayItems;
84+
}
85+
#endregion
86+
}
87+
}

0 commit comments

Comments
 (0)