Skip to main content

Create a Report in Code

  • 4 minutes to read

This help topic shows how to use DevExpress Reporting Tools API to create a data-bound report in code, convert it to a PDF file, and show the the resulting file in the PDF Viewer for .NET MAUI.

DevExpress .NET MAUI App with Reporting

Create a .NET MAUI App

Follow instructions in the following section to create an app with our .NET MAUI components: Get Started.

Note that the DevExpress library for .NET MAUI targets only Android and iOS platforms (See also: Supported Platforms).

Add DevExpress Reporting NuGet Packages

Install the following NuGet packages to use reporting functionality in your app:

DevExpress.Reporting.Core
This package implements core functionality for DevExpress Reporting.
DevExpress.Drawing.Skia
This package implements drawing functionality based on the Skia Graphics Library.

Create a Report in Code

Add a CreateTableReport method that creates a sample table report:

using DevExpress.Maui.Pdf;
using DevExpress.XtraReports.UI;
using MauiReportingApp.Data;

public partial class MainPage : ContentPage {
    //...
    public XtraReport CreateTableReport() {
        XtraReport report = new XtraReport() { Name = "Sample" };
        report.DataSource = new CountryDataSource();

        // Create a table header
        ReportHeaderBand headerBand = new ReportHeaderBand();
        headerBand.HeightF = 40;
        headerBand.BackColor = System.Drawing.Color.Beige;
        report.Bands.Add(headerBand);
        XRTable headerTable = new XRTable();
        headerTable.Rows.Add(new XRTableRow());
        XRTableCell cellDetail3 = new XRTableCell() { Text = "Country" };
        XRTableCell cellDetail4 = new XRTableCell() { Text = "Area, km²" };
        headerTable.Rows[0].Cells.AddRange([cellDetail3, cellDetail4]);
        headerTable.WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right;
        headerBand.Controls.Add(headerTable);
        headerTable.BorderColor = System.Drawing.Color.Gray;
        headerTable.Padding = 0;
        headerTable.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;

        // Create a data table and populate it with data
        DetailBand detailBand = new DetailBand();
        detailBand.HeightF = 25;
        report.Bands.Add(detailBand);
        XRTable table = new XRTable();
        detailBand.Controls.Add(table);
        table.Rows.Add(new XRTableRow());
        XRTableCell cellDetail1 = new XRTableCell();
        XRTableCell cellDetail2 = new XRTableCell();
        cellDetail1.ExpressionBindings.Add(new ExpressionBinding("Text", "[Region]"));
        cellDetail2.ExpressionBindings.Add(new ExpressionBinding("Text", "[Area]"));
        table.Rows[0].Cells.AddRange([cellDetail1, cellDetail2]);
        table.BorderColor = System.Drawing.Color.Gray;
        table.Padding = 0;
        table.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;
        table.WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right;

        report.CreateDocument();
        return report;
    }
}
Show data source implementation
namespace MauiReportingApp.Data {
   public class CountryDataSource : List<CountryData> {
       public CountryDataSource() {
           List<CountryData> sales = new List<CountryData>() {
               new CountryData(1, 0, "Norway", 385207),
               new CountryData(2, 0, "Sweden", 528447),
               new CountryData(3, 0, "Denmark", 42951),
               new CountryData(4, 0, "Finland", 338455),
               new CountryData(5, 0, "Iceland", 103000),
               new CountryData(6, 0, "Ireland", 84421),
               new CountryData(7, 0, "United Kingdom", 243610),

               new CountryData(18, 17, "Spain", 505990),
               new CountryData(19, 17, "Portugal", 92212),
               new CountryData(20, 17, "Greece", 131957),
               new CountryData(21, 17, "Italy", 301230),
               new CountryData(22, 17, "Malta", 316),
               new CountryData(23, 17, "San Marino", 61.2),
               new CountryData(25, 17, "Serbia", 88499),

               new CountryData(27, 26, "USA", 9522055),
               new CountryData(28, 26, "Canada", 9984670),

               new CountryData(30, 29, "Argentina", 2780400),
               new CountryData(31, 29, "Brazil", 8514215),

               new CountryData(34, 32, "India", 3287263),
               new CountryData(35, 32, "Japan", 377975),
               new CountryData(36, 32, "China", 9597000)
           };
           this.AddRange(sales);
       }
   }
}
namespace MauiReportingApp.Data {
   public class CountryData {
       public CountryData(int regionId, int parentRegionId, string region, double area) {
           RegionID = regionId;
           ParentRegionID = parentRegionId;
           Region = region;
           Area = area;
       }
       public int RegionID { get; set; }
       public int ParentRegionID { get; set; }
       public string Region { get; set; }
       public double Area { get; set; }
   }
}

Display the Report in PDF Viewer

In this tutorial, the resulting report is converted to a PDF and displayed in the DevExpress PDF viewer for .NET MAUI. To do so, follow the steps below:

  1. Add the PdfViewer component to the page. For more information, refer to the following section: Add a PDF Viewer to the Page.

    <ContentPage ...
                xmlns:dx="http://schemas.devexpress.com/maui">
        <dx:PdfViewer x:Name="pdfViewer"/>
    </ContentPage>
    
  2. Add the following code to the MainPage constructor to create a report, convert it to a PDF file, and open that file in the PDF Viewer:

    public MainPage() {
        InitializeComponent();
    
        XtraReport report = CreateTableReport();
        string resultFile = Path.Combine(FileSystem.Current.AppDataDirectory, report.Name + ".pdf");
        report.ExportToPdf(resultFile);
        pdfViewer.DocumentSource = PdfDocumentSource.FromFile(resultFile);
    }
    
  3. Run the project to see the results:

    DevExpress .NET MAUI App with Reporting