Articles in this section
Category / Section

How to Create RDL and RDLC Reports in .NET Core Controls?

7 mins read

The Syncfusion reporting component allows you to create and export RDL and RDLC reports programmatically using ASP.NET Core 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 page header 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 page footer 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)
{
    try
    { 
        ReportSerializer serializer = new ReportSerializer();
 
        MemoryStream memoryStream = new MemoryStream();
            // save the changed correction using the savereportdefinition method in serializer 
            serializer.SaveReportDefinition(memoryStream, report);
    }
    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);

 

Conclusion

I hope you enjoyed learning about how to create RDL and RDLC reports in ASP.NET Core controls.

You can refer to our ASP.NET Core feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core Example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied