Examples in this chapter demonstrate step by step document generation process, from template design in Microsoft Word to writing the necessary code that populates a template with data. Each example shows how to use various features in the context of different scenarios.

All examples use .NET Objects as data sources. The data model used for these examples is depicetd on the picture below:

Data Model Class Diagram

For the majority of the examples only couple of lines of code is needed in order to populate the corresponding templates:
Each of examples, because of simplicity, uses following code to generate report based on a template:

// Instancing report engine, by assigning the data source. DocumentGenerator dg = new DocumentGenerator(DataAccess.GetOrderById(7)); // Generating report by specifying the report template and the resulting report (as file paths) dg.GenerateDocument("example.docx", "example_output.docx");

// Instancing report engine, by assigning the data source
DocumentGenerator dg = new DocumentGenerator(DataAccess.GetOrderById(7));
// Generating report by specifying the report template and the resulting report (as file paths)
dg.GenerateDocument("example.docx", "example_output.docx");

Certainly, it is not necessary to read template from the file system and write generated report to the file system; more often you would want to perform generation by using these parameters (both the template and the output generated document) typed as System.IO.Stream. For example, your code could look like the following:

using (FileStream templateStream = new FileStream("example.docx", FileMode.Open))
{
     DocumentGenerator dg = new DocumentGenerator(DataAccess.GetOrderById(7));
     MemoryStream outputDocumentStream = new MemoryStream();
     dg.GenerateDocument(templateStream, outputDocumentStream);
     ...
}

This scenario is handy if, for example, the templates are stored in a database; then you can read them into Stream. Generated report in a form of Stream can be useful, for example, for instantly showing them in front of the user and later, for saving to a document storage (database, document systems etc.).