To use the Docentric Toolkit Report Engine from a .Net assembly the Report Engine assembly must first be referenced.

The Report Engine is represented by the Docentric.Word.DocumentGenerator class. In order to generate a document using the DocumentGenerator class follow these steps:

  1. Obtain the objects/values needed for each data source defined by the document template. Different kinds of data sources require different types of objects to be passed as values for the defined data sources:

    • For the .Net Object kind of a data source the object can be any type of .Net object. The following code segment loads a collection of Customer objects from the database.
      IEnumerable<Customer> customers = DataAccess.GetCustomerById(15);
    • For the XML kind of a data source the object can be an instance of type XDocument or XElement from the namespace System.Xml.Linq. The following code segment loads an XML file containing customers data.
      XElement customers = XElement.Load("Customers.xml");
  2. Create an instance of the DocumentGenerator class using the constructor that accepts the value for the default data source.
    DocumentGenerator dg = new DocumentGenerator(customer);
  3. If a report template also defines named data sources, You must set values (objects) for them as well.
    dg.SetNamedDataSourceValue("Products", products);
    The first parameter is the name of the named data source (set on the template), while the other one is the value for the data source.
  4. Optionally, if you want to interfere into the generation process to override elements' values or changing elements' properties before they take effect on the rendered document, you can subscribe to the DocumentGenerator.ElementInitialize event. This event is raised for each Docentric element instance in the report. It is mostly used for advanced data shaping and overwriting values (initially set as a result of data binding during document generation) or setting values for the unbound Docentric elements.
    dg.ElementInitialize += new ElementInitializeHandler(dg_ElementInitialize); void dg_ElementInitialize(object sender, ElementInitializeEventArgs e) { ... }
  5. On the DocumentGenerator instance call the GenerateDocument method. This method accepts the report template argument and the output document argument. According to your needs you can choose among two ovrloads: one that accepts Stream objects:
    DocumentGenerationResult result = dg.GenerateDocument(templateStream, outputStream);
    and another one that accepts file paths:
    DocumentGenerationResult result = dg.GenerateDocument("MyTemplate.docx", "MyReport.docx");
  6. Optionally, you can check the result returned from the DocumentGenerator method to check for any errors or warnings that might occured during the document generation.
    if (result.HasErrors) { foreach (Error error in result.Errors) Console.WriteLine(error); }