|
| 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