Example: Repeating an arbitrary content |
Docentric Toolkit Help
Send Feedback |
In this report, we want to write a list of products with some product details (e.g. name, category, price etc.) using Word bulleting feature. Data model used for all examples is exposed here.
Initially, we are creating a report template. Follow the common preparation steps to start building the template from scratch:
- Open a new blank Word document and enable it for templating
- Turn on Data Sources and Elements Explorers
- Define default data source as the Product class
After that, you should be at the following point:
We should emphasize that we want to build the template binding LD Elements
to a list of Product instead of to one single instance
of class Product:
In the Word document (template) insert a bullet and while it is marked, insert a
LD List element. Bind List element to default
data source that represents collection of products leaving the Path property
empty:
Set the sorting of the future rendered list of products through Order By
property of the List element (about sorting collections you can read
here):
Within the List element insert two LD Field elements
bound to Category.Name and Name
properties of class Product, a type of
IEnumerable<Product> (binding source of their parent List element):
Finally, finished template (with Design Mode switched on) looks like:
Next, save the template and generate the report. Report generation takes place programmatically,
from your application that is using (has a reference to) the LD ReportEngine dll.
This code looks like following:
// Instancing report engine, by assigning the data source
DocumentGenerator dg = new DocumentGenerator(DataAccess.GetProducts());
// Generating report by specifying the report template and the resulting
report (as file paths)
DocumentGenerationResult result = dg.GenerateDocument("example7.docx", "example7_output.docx");
// Examining potentially errors
if (result.HasErrors)
{
foreach (Error
error in result.Errors) Console.Out.WriteLine(error.Message);
}
It is also possible to invoke the method GenerateDocument with same parameters typed as System.IO.Stream.