Generating Dynamic Documents From Existing Template

Generate the PDF/Word/Excel/PowerPoint dynamically (Office Automation/Word Automation) from the template. (From JSON data source).

Introduction

During our development journey, we may encounter various requirements for dynamic document generation from the pre-existing template. For example, we generate a dynamic resume (PDF/Word) with existing employee details and generate dynamic invoices from the various invoice templates.

We have an easy, wonderful, and affordable third-party library to achieve the requirements related to document processing through programming.

This article will show the third-party library used to generate the documents from the pre-existing template.

Templater

Templater is a library designed to generate documents from existing Office templates. This allows you to create beautiful reports in a fraction of the time compared to other solutions.

Templater's unique minimal and stable interface allows trivial integration into third-party apps without custom coding.

By leveraging Microsoft Office tools, designers and domain experts will be much more productive. Your product can get a distinguishing feature through the customization of templates by application users.

Templater understands most Word, Excel, and PowerPoint features, allowing companies, non-profits, and individuals worldwide to create documents/spreadsheets/presentations most cost-effectively and efficiently. I found this library very comfortable and affordable to work in after my long search on the internet.

It is beneficial if we need to generate the document dynamically from our system using respective document templates. We can source the data to the document as a JSON file.

We will see implementation detail below,

Step 1

First, we need to finalize our document template and place respective placeholders to fill our dynamic data from the JSON source; Values given inside [[]] double square brackets will be considered placeholders. If we pass the same value from JSON, it will take that as dynamic data. Iteration values should be given in the following format [[rootname.valuename]]

Template Sample

Generating dynamic documents

Step 2

Create the JSON data according to the template placeholders we discussed above,

Please check below JSON data for the above template,

{
  "user": {
    "name": "Bob Barley",
    "age": "42"
  },
  "beers": [
    {
      "name": "Rare Bourbon County Brand Stout",
      "brewery": "Goose Island Beer Co.",
      "type": "Stout",
      "rating": 4.5,
      "abv": 0.13
    },
    {
      "name": "Ožujsko",
      "brewery": "Zagrebacka Pivovara",
      "type": "Euro Lager",
      "rating": 1.5,
      "abv": 0.0499
    },

    {
      "name": "Vukovarsko",
      "brewery": "Vukovarska pivovara",
      "type": "Pale Lager",
      "rating": 2.5,
      "abv": 0.045
    },

    {
      "name": "Pale Ale",
      "brewery": "Zmajska Pivovara d.o.o.",
      "type": "American Pale Ale",
      "rating": 3.5,
      "abv": 0.053
    },

    {
      "name": "Porter",
      "brewery": "Zmajska Pivovara d.o.o.",
      "type": "Porter",
      "rating": 3.5,
      "abv": 0.065
    },

    {
      "name": "APA",
      "brewery": "Nova Runda",
      "type": "American Pale Ale",
      "rating": 3.0,
      "abv": 0.053
    },

    {
      "name": "Brale",
      "brewery": "Nova Runda",
      "type": "American Pale Ale",
      "rating": 3.5,
      "abv": 0.049
    },

    {
      "name": "Double Barrel Hunahpu's",
      "brewery": "Cigar City Brewing",
      "type": "American imperial",
      "rating": 4.5,
      "abv": 0.115
    },

    {
      "name": "Karlovacko Pivo",
      "brewery": "Karlovacka Pivovara",
      "type": "Pale Lager",
      "rating": 2.0,
      "abv": 0.05
    }
  ]
}

Step 3

Execute the Templater code to generate the document from the JSON data using the document template we are passing,

using (var doc = NGS.Templater.Configuration.Builder.Include(ColorConverter).Build().Open(in2, "docx", out2))

  {
                doc.Process(ds);
  }

Please refer here for the complete code, https://github.com/ngs-doo/TemplaterExamples.

Step 4,

We can find the generated PDF document below, 

Generating dynamic documents

Above I have given an example of generating Word or pdf from a Word template. Similarly, we can do it for all the document formats like Excel and PowerPoint.

Code Sample for Excel file generation from the template,

using NGS.Templater;

class Program
{
    static void Main(string[] args)
    {
        using (var document = Configuration.Factory.Open("Spreadsheet.xlsx"))
        {
            document.Process(new { Teacher = "Dominic", Age = 45 });
            document.Process(new [] {
                new { Name = "Parker" },
                new { Name = "Mason" },
                new { Name = "Robert" },
                new { Name = "Michael" }
            });
        }
    }
}

For more code samples and costing information, you can check the below link- https://templater.info/

Thank you.