Articles in this section
Category / Section

How to Create .NET Core Word in Form Field of JSON and convert to PDF?

4 mins read

Syncfusion; Essential; DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can create a Word document with form fields from JSON and convert it to PDF using C#.

Steps to create a Word document with form fields from JSON and convert it to PDF:

  1. Create a new .NET Core console application project.

    Create console application in Visual Studio

  2. Install the Syncfusion.DocIORenderer.NET.Core NuGet package as a reference to your .NET Core application from NuGet.org.

    Install DocIORenderer NuGet package

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.

  1. Include the following namespaces in the Program.cs file.
    C#
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;
  1. Use the following code example to create a Word document with form fields from JSON and convert it to PDF.
    C#
// 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);
            }
        }
    }
}
  1. Use the following code example to add the text form field in the Word document.
    C#
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();
} 
  1. Use the following code example for report data with employee details and the Root object for deserializing JSON data.
    C#
/// <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; }
} 

You can download a complete working sample to create a Word document with form fields from JSON and convert it to PDF from GitHub.

Output Word document

Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

See Also

Conclusion

I hope you enjoyed learning about how to create .Net core word in form field in JSON and convert to PDF.

You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied