|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using Syncfusion.DocIO; |
| 5 | +using Syncfusion.DocIO.DLS; |
| 6 | +using Syncfusion.DocIORenderer; |
| 7 | +using Syncfusion.Pdf; |
| 8 | + |
| 9 | +namespace Read_JSON_data_set_values_in_form_fields |
| 10 | +{ |
| 11 | + class Program |
| 12 | + { |
| 13 | + static void Main(string[] args) |
| 14 | + { |
| 15 | + // Read and deserialize JSON data. |
| 16 | + var jsonString = File.ReadAllText(Path.GetFullPath("Data/ReportData.json")); |
| 17 | + var data = JsonConvert.DeserializeObject<Root>(jsonString); |
| 18 | + |
| 19 | + using (WordDocument document = new WordDocument()) |
| 20 | + { |
| 21 | + ////Get the text from json and replace in Word document |
| 22 | + //Adds new section to the document |
| 23 | + IWSection section = document.AddSection(); |
| 24 | + //Adds new paragraph to the section |
| 25 | + WParagraph paragraph = section.AddParagraph().AppendText("General Information") as WParagraph; |
| 26 | + section.AddParagraph(); |
| 27 | + |
| 28 | + AddTextFormField(section, "EmployeeID", data.Reports[0].EmployeeID); |
| 29 | + AddTextFormField(section, "Name", data.Reports[0].Name); |
| 30 | + AddTextFormField(section, "PhoneNumber", data.Reports[0].PhoneNumber); |
| 31 | + AddTextFormField(section, "Location", data.Reports[0].Location); |
| 32 | + |
| 33 | + //Creates an instance of DocIORenderer. |
| 34 | + using (DocIORenderer renderer = new DocIORenderer()) |
| 35 | + { |
| 36 | + //Converts Word document into PDF document. |
| 37 | + using (PdfDocument pdfDocument = renderer.ConvertToPDF(document)) |
| 38 | + { |
| 39 | + //Saves the PDF file to file system. |
| 40 | + using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.pdf"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) |
| 41 | + { |
| 42 | + pdfDocument.Save(outputStream); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + /// <summary> |
| 49 | + /// Adds a labeled text form field to the specified section. |
| 50 | + /// </summary> |
| 51 | + private static void AddTextFormField(IWSection section, string label, string value) |
| 52 | + { |
| 53 | + WParagraph paragraph = section.AddParagraph() as WParagraph; |
| 54 | + IWTextRange text = paragraph.AppendText(label + ": "); |
| 55 | + text.CharacterFormat.Bold = true; |
| 56 | + |
| 57 | + WTextFormField textField = paragraph.AppendTextFormField(null); |
| 58 | + textField.Type = TextFormFieldType.RegularText; |
| 59 | + textField.CharacterFormat.FontName = "Calibri"; |
| 60 | + textField.Text = value; |
| 61 | + textField.CalculateOnExit = true; |
| 62 | + |
| 63 | + section.AddParagraph(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Represents report data with employee details. |
| 69 | + /// </summary> |
| 70 | + public class Reports |
| 71 | + { |
| 72 | + [JsonProperty("EmployeeID")] public string EmployeeID { get; set; } |
| 73 | + [JsonProperty("Name")] public string Name { get; set; } |
| 74 | + [JsonProperty("PhoneNumber")] public string PhoneNumber { get; set; } |
| 75 | + [JsonProperty("Location")] public string Location { get; set; } |
| 76 | + |
| 77 | + } |
| 78 | + /// <summary> |
| 79 | + /// Root object for deserializing JSON data. |
| 80 | + /// </summary> |
| 81 | + public class Root |
| 82 | + { |
| 83 | + [JsonProperty("Reports")] public List<Reports> Reports { get; set; } |
| 84 | + } |
| 85 | +} |
0 commit comments