Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES-825473- Add the sample Read-JSON-data-set-values-in-form-fields #398

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31911.196
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {58137FF9-5AE1-4514-9929-3A8A7DA1DFEB}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Reports": [
{
"EmployeeID": "1001",
"Name": "Peter",
"PhoneNumber": "+122-2222222",
"Location": "London",
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;

namespace Read_JSON_data_set_values_in_form_fields
{
class Program
{
static void Main(string[] args)
{
// Read and deserialize JSON data.
var jsonString = File.ReadAllText(Path.GetFullPath("Data/ReportData.json"));
var data = JsonConvert.DeserializeObject<Root>(jsonString);

using (WordDocument document = new WordDocument())
{
////Get the text from json and replace in Word document
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
WParagraph paragraph = section.AddParagraph().AppendText("General Information") as WParagraph;
section.AddParagraph();

AddTextFormField(section, "EmployeeID", data.Reports[0].EmployeeID);
AddTextFormField(section, "Name", data.Reports[0].Name);
AddTextFormField(section, "PhoneNumber", data.Reports[0].PhoneNumber);
AddTextFormField(section, "Location", data.Reports[0].Location);

//Creates an instance of DocIORenderer.
using (DocIORenderer renderer = new DocIORenderer())
{
//Converts Word document into PDF document.
using (PdfDocument pdfDocument = renderer.ConvertToPDF(document))
{
//Saves the PDF file to file system.
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.pdf"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
pdfDocument.Save(outputStream);
}
}
}
}
}
/// <summary>
/// Adds a labeled text form field to the specified section.
/// </summary>
private static void AddTextFormField(IWSection section, string label, string value)
{
WParagraph paragraph = section.AddParagraph() as WParagraph;
IWTextRange text = paragraph.AppendText(label + ": ");
text.CharacterFormat.Bold = true;

WTextFormField textField = paragraph.AppendTextFormField(null);
textField.Type = TextFormFieldType.RegularText;
textField.CharacterFormat.FontName = "Calibri";
textField.Text = value;
textField.CalculateOnExit = true;

section.AddParagraph();
}
}

/// <summary>
/// Represents report data with employee details.
/// </summary>
public class Reports
{
[JsonProperty("EmployeeID")] public string EmployeeID { get; set; }
[JsonProperty("Name")] public string Name { get; set; }
[JsonProperty("PhoneNumber")] public string PhoneNumber { get; set; }
[JsonProperty("Location")] public string Location { get; set; }

}
/// <summary>
/// Root object for deserializing JSON data.
/// </summary>
public class Root
{
[JsonProperty("Reports")] public List<Reports> Reports { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Read_JSON_data_set_values_in_form_fields</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>

<ItemGroup>
<None Update="Data\ReportData.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading