Introduction to FastReport with C#

unbalanced-tree

Ravi Vijay

Posted on July 25, 2024

Introduction to FastReport with C#

Wolf

Wolf Fact: Adult gray and red wolves have 42 highly specialized teeth, while adult humans have 32.


What exactly is FastReport? Why do we need it? How to use it? What kinds of documents can it generate? How do you design and download a PDF using it?

I am going to answer all these questions here.

FastReport:

It is an open-source band-oriented (report title, page header, column header, data, etc.)report generator for .NET frameworks. FastReport is written in C#.

Features:

Objects: It supports

  • 13 kinds of Bands( Header, footer, title, sub-report)

  • texts, pictures, shapes ( line, triangle, rectangle, table, checkbox)

  • multiple page design, front, and back covers

  • inheritance

Data Source:

  • FastReport can work with data from XML, CSV, JSON, MS SQL, MySql, etc.
  • The report can contain data sources ( tables, queries, DB connections).

It has many more features. You can review all of them here.

If you need to generate a PNG, JPEG, GIF, or HTML report, FastReport is your solution. PDF export is available as a plugin.

Let's first download FastReport from here.

I am using the trial version for now. FastReport also has a paid version.


I will create two projects. One project is to design the report, and another is to download it as a PDF. You can use one project for both. ( Using a separate project is a good idea when we only want to design the report once and use it with API.)

The complete code can be found here.

I will do this in three steps.

  1. Create an API to get the data

  2. Design a FastReport using this data

  3. Download this FastReport as a PDF

Step 1: Create an API to get the data

Create a simple .NET web API project.

ASP.NET Core Web API

ASP.NET Core Web API

Project FastReportPdf File Structure

Project FastReportPdf File Structure


I created a controller and two models.

OneMoreDataModel

OneMoreDataModel

ResponseDataModel

ResponseDataModel

HomeController

HomeController

I am using this data to keep things simpler. We can run this API using Swagger UI.

Swagger

Swagger

{
  "header": "FasterReprt Header",
  "footer": "FastReport Footer",
  "content": "FastReprt Column",
  "collection": [
    {
      "number": 1
    },
    {
      "number": 2
    },
    {
      "number": 3
    },
    {
      "number": 4
    },
    {
      "number": 5
    },
    {
      "number": 6
    },
    {
      "number": 7
    },
    {
      "number": 8
    },
    {
      "number": 9
    },
    {
      "number": 10
    },
    {
      "number": 11
    },
    {
      "number": 12
    },
    {
      "number": 13
    },
    {
      "number": 14
    },
    {
      "number": 15
    },
    {
      "number": 16
    },
    {
      "number": 17
    },
    {
      "number": 18
    },
    {
      "number": 19
    },
    {
      "number": 20
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

That is how the API response will appear. I will use this data for FastReport Design.

Step 2: Design a FastReport using this data

Now, let’s write code for the designer. First, create a simple Windows Forms App.

Windows Forms App

Windows Forms App

Then add API response models and a Data.json file.

Project FastReportDesigner File Structure

Project FastReportDesigner File Structure

Data.json

Data.json

Data.json has API response in JSON format.

Form1.cs

Form1.cs

The above code will be auto-generated. We need to add the FastReport code to Form1_Load.

NuGet Packages

NuGet Packages

These two packages are required. Let's first install them.

Form1_Load

Form1_Load

Now, Let’s break down the code.

Report report = new();
Enter fullscreen mode Exit fullscreen mode
  • Initialize a new instance of the report class using the default setting.
var responseModel = JsonConvert.DeserializeObject<ResponseDataModel>(File.ReadAllText("data.json"));
Enter fullscreen mode Exit fullscreen mode
  • It reads all the data from the data.json file and deserializes data based on response models.
var data = new List<ResponseDataModel> { responseModel };
report.RegisterData(data, "ResponseData");
Enter fullscreen mode Exit fullscreen mode
  • The method RegisterData links the data to the report object. Only this linked data can be used in the report. The method RegisterData takes two parameters as input. First, data in IEnumerable format. Second, data name.
report.Design();
Enter fullscreen mode Exit fullscreen mode

It runs the report designer. Now let’s run the code.

It will open the FastReportDesigner

FastReport Designer

FastReport Designer

We can see a blank report with a header and a footer. We can add the required bands from the Configure section.

Configure Section

Configure Section

Insert

Insert

We can insert text, pictures, shapes, or tables from this section to report. Just click on the desired object. It will be on the cursor point. We can insert it into the report.

For example, I selected a text

For example, I selected a text

I inserted the text object in the report title section. We can select it and change its properties like height, width, font, padding, or highlight from the properties section.

Properties

Properties

When I click twice on the text object, the edit text box opens as a Pop-up.

Edit Text

Edit Text

We can type the desired text here.

Save and Run Report

Save and Run Report

First, we need to bind the data to the Report, and then the Report can use it.

Choose Report Data

Choose Report Data

Choose the data

Choose the data

Now, insert the text boxes in the Page Header, Page Footer, and Data Section. Attach header, footer, and content data in respective. Save and Run.

Report Header

Report Header

Header-footer-report

Now we need to add the collection data below the column header. For that add a new data band to the existing data band.

Add new data band

Add new data band

Nested data band

Nested data band

First, associate the collection data with this new data band.

Click on Edit

Click on Edit

Select collection and OK

Select collection and OK

Insert a text box in the new data band and add the collection parameter.

Drag and Drop

Drag and Drop

Save and Run.

Final Report

Final Report

You may add lines, shapes, and images for beautification.

Step 3: Download this FastReport as a PDF

Now let’s download the FastReport as a PDF file.

Required NuGet Package

Required NuGet Package

I will write a controller GenerateReportPdf in the project FastReportPdf.Controllers.

Controller Method

Controller Method

In this,

  • I read the FastReport file.

  • Fetch the data for the Report( The exact format we used with the designer project).

  • Prepare the Report by registering and binding the data.

  • Export the Report to a stream and write it to a file.

That’s how we will download the Report as a PDF.

The output file will look like this:-

Reportpdf.pdf

Reportpdf.pdf

Any comments or suggestions would be greatly appreciated.

💖 💪 🙅 🚩
unbalanced-tree
Ravi Vijay

Posted on July 25, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related