Skip to content

Commit fb5952b

Browse files
Merge pull request #398 from SyncfusionExamples/ES-825473-Read-JSON-data-set-values-in-form-fields
ES-825473- Add the sample Read-JSON-data-set-values-in-form-fields
2 parents bc85013 + 80baf84 commit fb5952b

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-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}") = "Read-JSON-data-set-values-in-form-fields", "Read-JSON-data-set-values-in-form-fields\Read-JSON-data-set-values-in-form-fields.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
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Reports": [
3+
{
4+
"EmployeeID": "1001",
5+
"Name": "Peter",
6+
"PhoneNumber": "+122-2222222",
7+
"Location": "London",
8+
}
9+
]
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Read_JSON_data_set_values_in_form_fields</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
11+
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="Data\ReportData.json">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Output\.gitkeep">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
23+
</Project>

0 commit comments

Comments
 (0)