)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Create RDL and RDLC reports programmatically

Platform: WPF |
Control: ReportWriter |
Published Date: November 5, 2018 |
Last Revised Date: May 23, 2019

The Syncfusion reporting component allows you to create and export RDL and RDLC reports programmatically using C# without the help of a visual report designer. The ReportDefinition class is defined as an object model of a report. You can create, add, and modify the report properties; report sections like header and footer; and report items in the report definition instance. The following code snippet illustrates how to create a basic report object model.

 

Initialize report definition

 

ReportDefinition report = new ReportDefinition();
report.ReportSections = new ReportSections();
var reportSection = new ReportSection();
report.ReportSections.Add(reportSection);
reportSection.Body = CreateBody();
reportSection.Page = new Syncfusion.RDL.DOM.Page();
reportSection.Page.PageHeader = CreateHeader();
reportSection.Page.PageFooter = CreateFooter();
reportSection.Page.Style = AddStyle();
reportSection.Page.PageHeight = "4in";
reportSection.Page.PageWidth = "6in";
reportSection.Width = new Syncfusion.RDL.DOM.Size("6in");
report.ReportUnitType = "Inch";
report.RDLType = RDLType.RDL2010;

 

Create a report page header

 

The following code snippet guides you in creating a PageHeader report section and setting values to its properties.

 

PageHeader pageHeader = new PageHeader();
pageHeader.Height = new Syncfusion.RDL.DOM.Size("0.59167in"); 
pageHeader.ReportItems = new ReportItems();
pageHeader.PrintOnFirstPage = true;
pageHeader.PrintOnLastPage = true;

 

Create a report page footer

 

The following code snippet guides you in creating a PageFooter report section and setting values to its properties.

 

PageFooter pageFooter = new PageFooter();
pageFooter.Height = new Syncfusion.RDL.DOM.Size("0.59167in");
pageFooter.ReportItems = new ReportItems();
pageFooter.PrintOnFirstPage = true;
pageFooter.PrintOnLastPage = true;

 

Initialize a report body object and set values to its properties like height, styles, and report items as shown in the following code snippet.

 

var body = new Body();
body.Height = new Syncfusion.RDL.DOM.Size("2.03333in");
body.Style = AddStyle();
body.ReportItems = new ReportItems();

 

Using the following code snippets, you can create a text box report item and assign values for its properties like name, styles, paragraphs, and dimension values. You can also create multiple report items.

 

var textBox = new Syncfusion.RDL.DOM.TextBox();
textBox.Style = new Syncfusion.RDL.DOM.Style();
textBox.Height = new Syncfusion.RDL.DOM.Size(height);
textBox.Width = new Syncfusion.RDL.DOM.Size(width);
textBox.Left = new Syncfusion.RDL.DOM.Size(left);
textBox.Top = new Syncfusion.RDL.DOM.Size(top);
textBox.Name = "TextBox1";
textBox.Paragraphs = new Paragraphs();
Syncfusion.RDL.DOM.Paragraph paragraph = new Syncfusion.RDL.DOM.Paragraph();
TextRuns runs = new TextRuns();
TextRun run = new TextRun();
run.Style = new Syncfusion.RDL.DOM.Style();
run.Style.FontStyle = "Default";
run.Style.TextAlign = "Center";
run.Style.FontFamily = "Arial";
run.Style.FontSize = new Syncfusion.RDL.DOM.Size("10pt");
run.Value = text;
runs.Add(run);
paragraph.Style = new Syncfusion.RDL.DOM.Style();
paragraph.Style.VerticalAlign = "Top";
paragraph.Style.TextAlign = "Center";
paragraph.TextRuns = runs;
textBox.Paragraphs.Add(paragraph);
textBox.Style.TextAlign = "Center";
textBox.Style.VerticalAlign = "Top";

 

After the report definition is created, it can be converted to a stream or saved into a local file using the XmlSerializer’s Deserialize method. In the following code snippet, the SaveSerialize local method has been created to serialize the report object model to create a report file (.rdl / .rdlc).

 

private void SaveSerialize(string reportPath, ReportDefinition report)
{
    string nameSpace = "http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition";
 
    if (report.RDLType == RDLType.RDL2010)
    {
        nameSpace = "http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition";
    }
    else if (report.RDLType == RDLType.RDL2016)
    {
        nameSpace = "http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition";
    }
 
    XmlSerializer xs2 = new XmlSerializer(typeof(ReportDefinition), nameSpace);
    XmlSerializerNamespaces xs = new XmlSerializerNamespaces();
    xs.Add("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
 
    if (report.RDLType == RDLType.RDL2016)
    {
        xs.Add("df", "http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition/defaultfontfamily");
    }
 
    try
    { 
        TextWriter ws2 = new StreamWriter(reportPath);
        xs2.Serialize(ws2, report, xs);
        ws2.Close();
    }
    catch { }
}

 

You can render the report through the Report Viewer or Report Writer, after the report definition or stream is created. The following code snippet illustrates how to export the report into PDF format through the Report Writer using report definition.

 

ReportWriter reportWriter = new ReportWriter();
reportWriter.LoadReport(reportDefinition);
reportWriter.Save(fileName, WriterFormat.PDF);

 

The following code snippet illustrates how to export a report into PDF format through the Report Writer using the report path if it is saved in a local file.

 

ReportWriter reportWriter = new ReportWriter(path);
reportWriter.Save(fileName, WriterFormat.PDF);

 

Sample

 

Download dynamic report creation sample

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Holland
Feb 01, 2019
Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon