Layout Data

Layout data is data applied to a DLEX file during the layout process that generates a PDF report. During runtime, layout data is dynamically applied to a static DLEX file to generate a PDF report. You can provide layout data from several different sources, including databases, JSON files, reflection to process enumerable data and data lists, and event-driven data.

Understanding DLEX files is a prerequisite to understanding layout data. When you add layout data, the names in the layout data must match the names the DLEX file.

Top Level Layout Data

For simple name/value data, use the LayoutData class combined with the Add method. Use single-value data to add values to DLEX files containing non-repeatable data such as headers, footers, and titles.

Refer to Fill Out a Form to learn how to create this report using Designer.

dlex Figure 1. Data in form-fill.dlex DLEX file.

DocumentLayout layoutReport = new DocumentLayout("form-fill.dlex");
LayoutData layoutData = new LayoutData();
layoutData.Add("FirstNameAndI", "Alex B.");
layoutData.Add("LastName", "Smith");
layoutData.Add("SSN", "123-45-6789");
layoutData.Add("HomeAddress", "456 Green Road");
layoutData.Add("CityStateZip", "Somewhere, Earth  12345");
layoutData.Add("Allowances", "2");
layoutData.Add("IsSingle", true);
Document document = layoutReport.Layout(layoutData);
document.Author = "DynamicPDF ReportWriter";
document.Title = "Form Fill Example";
document.Draw(outputPath);
Dim layoutReport As New DocumentLayout("form-fill.dlex")
Dim layoutData As New LayoutData()
layoutData.Add("FirstNameAndI", "Alex B.")
layoutData.Add("LastName", "Smith")
layoutData.Add("SSN", "123-45-6789")
layoutData.Add("HomeAddress", "456 Green Road")
layoutData.Add("CityStateZip", "Somewhere, Earth  12345")
layoutData.Add("Allowances", "2")
layoutData.Add("IsSingle", True)
Dim document As Document = layoutReport.Layout(layoutData)
document.Author = "DynamicPDF ReportWriter"
document.Title = "Form Fill Example"
document.Draw(outputPath)

completed doc Figure 2. Completed PDF report.

Dynamic Layout Data

For repeatable data, such as rows in a table, use the LayoutData class combined with an Add method that contains an data collection such as an array or enumerable list of objects. The following sources can provide layout data to a DLEX file to generate a report.

report Figure 3. Completed report for dynamic layout data examples.

Refer to Create a Report with Cover Page to learn how to create this report using Designer.

Figure 4 illustrates the corresponding fields in the DLEX used by the examples below to populate the layout data.

dlex Figure 4. Data elements in report-with-cover-page.dlex.

JSON

The following example illustrates using JSON as the provided layout data.

Core Suite relies upon the Newtonsoft framework for processing JSON data.

{
  "ReportCreatedFor":"John Doe",
  "Products": [
    {
      "ProductID": 17,
      "ProductName": "Alice Mutton",
      "QuantityPerUnit": "20 - 1 kg tins",
      "UnitPrice": 39,
      "Discontinued": true
    },
    {
      "ProductID": 3,
      "ProductName": "Aniseed Syrup",
      "QuantityPerUnit": "12 - 550 ml bottles",
      "UnitPrice": 10,
      "Discontinued": false
    }
      ]
}
string data = "report-with-cover-page.json";
var jsonData = JsonConvert.DeserializeObject(data);
DocumentLayout layoutReport = new DocumentLayout("report-with-cover-page.dlex");
LayoutData layoutData = new LayoutData(jsonData);
Document document = layoutReport.Layout(layoutData);
document.Draw(outputPath);
Dim data As String = File.ReadAllText("report-with-cover-page.json")
Dim jsonData = JsonConvert.DeserializeObject(data)
Dim layoutReport As New DocumentLayout("report-with-cover-page.dlex")
Dim layoutData As New LayoutData(jsonData)
Dim document As Document = layoutReport.Layout(layoutData)
document.Draw(outputPath)

Data Objects

You can use data objects as layout data. In the following example the Add method uses reflection to obtain the list of Product instances.

DocumentLayout layoutReport = new DocumentLayout(Util.GetPath("report-with-cover-page.dlex"));
NameValueLayoutData layoutData = new NameValueLayoutData();
layoutData.Add("ReportCreatedFor", "Alex Smith");
layoutData.Add("Products", ProductsData.Products);
Document document = layoutReport.Layout(layoutData);
document.Author = "DynamicPDF ReportWriter";
document.Title = "Simple Report Example";
document.Draw(outputPath);
Dim layoutReport As New DocumentLayout"report-with-cover-page.dlex")
Dim layoutData As New NameValueLayoutData()
layoutData.Add("ReportCreatedFor", "Alex Smith")
layoutData.Add("Products", ProductsData.Products)
Dim document As Document = layoutReport.Layout(layoutData)
document.Author = "DynamicPDF ReportWriter"
document.Title = "Simple Report Example"
document.Draw(outputPath)

LINQ

Language Integrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages. When using XML data, an easy way to process that data is through a LINQ query. For example, consider an XML dataset to use as a report's layout data.

XElement root = XElement.Load("ProductList.xml");
var products = root.Elements("Products")
	.Select(p => new
    {
        ProductID = (int)p.Attribute("ProductID"),
        ProductName = (string)p.Attribute("ProductName"),
        QuantityPerUnit = (string)p.Attribute("QuantityPerUnit"),
        UnitPrice = (decimal)p.Attribute("UnitPrice")
    });

DocumentLayout layoutReport = new DocumentLayout("report-with-cover-page.dlex");
LayoutData layoutData = new LayoutData();
layoutData.Add("ReportCreatedFor", "John Doe");
layoutData.Add("Products", products);
Document document = layoutReport.Layout(layoutData);
document.Draw(outputPath);
Dim root As XElement = XElement.Load("ProductList.xml")
Dim products = From p In root.Elements("Products")
Select New With {
	.ProductID = CInt(p.Attribute("ProductID")),
	.ProductName = CStr(p.Attribute("ProductName")),
	.QuantityPerUnit = CStr(p.Attribute("QuantityPerUnit")),
	.UnitPrice = CDec(p.Attribute("UnitPrice"))
	}
Dim layoutReport As New DocumentLayout(Util.GetPath("Resources/DLEXs/report-with-cover-page.dlex"))
Dim layoutData As New LayoutData()
layoutData.Add("ReportCreatedFor", "John Doe")
layoutData.Add("Products", products)
Dim document As Document = layoutReport.Layout(layoutData)
document.Draw(outputPath)

Database (TableReportData)

Use the TableReportData class to use a record set from a DataTable.

string sql = "SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice FROM Products";

DataTable dataTable = new DataTable();
using (SqlConnection connection = new SqlConnection(CONNECTION_STRING))
	{
    	using (SqlCommand command = new SqlCommand(sql, connection))
        {
            connection.Open();
            using (SqlDataReader reader = command.ExecuteReader())
            {
            	dataTable.Load(reader);
            }
        }
	}
DocumentLayout layoutReport = new DocumentLayout("report-with-cover-page.dlex");
NameValueLayoutData layoutData = new NameValueLayoutData();
DataTableReportData data = new DataTableReportData(dataTable);
layoutData.Add("ReportCreatedFor", "John Doe");
layoutData.Add("Products", data);
Document document = layoutReport.Layout(layoutData);
document.Draw(outputPath);
Dim sql As String = "SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice FROM Products"
Dim dataTable As New DataTable()

Using connection As New SqlConnection(CONNECTION_STRING)
	Using command As New SqlCommand(sql, connection)
		connection.Open()
		Using reader As SqlDataReader = command.ExecuteReader()
			dataTable.Load(reader)
		End Using
	End Using
End Using

Dim layoutReport As New DocumentLayout("report-with-cover-page.dlex")
Dim layoutData As New NameValueLayoutData()
Dim data As New DataTableReportData(dataTable)
layoutData.Add("ReportCreatedFor", "John Doe")
layoutData.Add("Products", data)
Dim document As Document = layoutReport.Layout(layoutData)
document.Draw(outputPath)

Database (DataReaderReportData)

Use the DataReaderReportData class to use a record set from a SQL recordset.

string sql = "SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice FROM Products";
Document document = null;
DocumentLayout layoutReport = new DocumentLayout("report-with-cover-page.dlex");
NameValueLayoutData layoutData = new NameValueLayoutData();
layoutData.Add("ReportCreatedFor", "John Doe");
	using (SqlConnection connection = new SqlConnection(CONNECTION_STRING))
    {
        using (SqlCommand command = new SqlCommand(sql, connection))
        {
            connection.Open();
            using (SqlDataReader reader = command.ExecuteReader())
            {
                layoutData.Add("Products", new DataReaderReportData(connection, reader));
                document = layoutReport.Layout(layoutData);
            }
        }
    }
document.Draw(outputPath);
Dim sql As String = "SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice FROM Products"
Dim document As Document = Nothing
Dim layoutReport As New DocumentLayout("report-with-cover-page.dlex")
Dim layoutData As New NameValueLayoutData()
layoutData.Add("ReportCreatedFor", "John Doe")

Using connection As New SqlConnection(CONNECTION_STRING)
    Using command As New SqlCommand(sql, connection)
        connection.Open()
        Using reader As SqlDataReader = command.ExecuteReader()
layoutData.Add("Products", New DataReaderReportData(connection, reader))
document = layoutReport.Layout(layoutData)
        End Using
    End Using
End Using

document.Draw(outputPath)

Database JSON

You can also serialize a SQL query result into JSON and use the data as layout data. For example, the following code relies on the FOR JSON syntax for creating a JSON dataset using SQL Server (see Format query results as JSON with FOR JSON).

string queryWithForJson = "SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice "
    + "FROM Products for json auto, root('Products')";
using (var conn = new SqlConnection(CONNECTION_STRING))
{
    using (var cmd = new SqlCommand(queryWithForJson, conn)
           {
               conn.Open();
               var jsonResult = new StringBuilder();
               var reader = cmd.ExecuteReader();
               if (!reader.HasRows) {
                   jsonResult.Append("[]");
               } else {
                   while (reader.Read()) {
jsonResult.Append(reader.GetValue(0).ToString());
                   }
               }
               var jsonData = JsonConvert.DeserializeObject(jsonResult.ToString());
               DocumentLayout layoutReport = new DocumentLayout("report-with-cover-page.dlex");
               LayoutData layoutData = new LayoutData(jsonData);
               layoutData.Add("ReportCreatedFor", "John Doe");
               Document document = layoutReport.Layout(layoutData);
               document.Draw("report-forjson-json-layout-data-output.pdf");
                                                                }
}
Dim queryWithForJson As String = "SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice FROM Products FOR JSON AUTO, ROOT('Products')"

Using conn As New SqlConnection(CONNECTION_STRING)
	Using cmd As New SqlCommand(queryWithForJson, conn)
		conn.Open()
  		Dim jsonResult As New StringBuilder()
  		Dim reader As SqlDataReader = cmd.ExecuteReader()
		If Not reader.HasRows Then
			jsonResult.Append("[]")
		Else
			While reader.Read()
 				jsonResult.Append(
 				reader.GetValue(0).ToString())
			End While
  		End If
  		
  		Dim jsonData = JsonConvert.DeserializeObject(jsonResult.ToString())
  		Dim layoutReport As New
  		DocumentLayout("report-with-cover-page.dlex")
  		Dim layoutData As New LayoutData(jsonData)
  		layoutData.Add("ReportCreatedFor", "John Doe")
  		Dim document As Document = layoutReport.Layout(layoutData)
  		document.Draw(outputPath)
	End Using
End Using

Enumerable Objects (EnumerableReportData)

Core Suite also provides the EnumerableReportData reader to create layout data for a report.

DocumentLayout layoutReport = new DocumentLayout("report-with-cover-page.dlex);
Products products = new Products();
EnumerableReportData enumData = new(products);
LayoutData layoutData = new();
layoutData.Add("Products", enumData);
layoutData.Add("ReportCreatedFor", "Alex Smith");
Document document = layoutReport.Layout(layoutData);
document.Author = "DynamicPDF ReportWriter";
document.Title = "Simple Report Example";
document.Draw(outputPath);
Dim layoutReport As DocumentLayout = New DocumentLayout("report-with-cover-page.dlex")

Dim products As Products = New Products()
Dim enumData As EnumerableReportData = New EnumerableReportData(products)
Dim layoutData As LayoutData = New LayoutData()
layoutData.Add("Products", enumData)
layoutData.Add("ReportCreatedFor", "Alex Smith")

Dim document As Document = layoutReport.Layout(layoutData)
document.Author = "DynamicPDF ReportWriter"
document.Title = "Simple Report Example"
document.Draw(outputPath)

In this topic