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.
Create an API to get the data
Design a FastReport using this data
Download this FastReport as a PDF
Step 1: Create an API to get the data
Create a simple .NET web API project.
I created a controller and two models.
I am using this data to keep things simpler. We can run this API using Swagger UI.
{
"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
}
]
}
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.
Then add API response models and a Data.json file.
Data.json has API response in JSON format.
The above code will be auto-generated. We need to add the FastReport code to Form1_Load.
These two packages are required. Let's first install them.
Now, Let’s break down the code.
Report report = new();
- Initialize a new instance of the report class using the default setting.
var responseModel = JsonConvert.DeserializeObject<ResponseDataModel>(File.ReadAllText("data.json"));
- 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");
- 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();
It runs the report designer. Now let’s run the code.
It will open the FastReportDesigner
We can see a blank report with a header and a footer. We can add the required bands from the Configure section.
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.
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.
When I click twice on the text object, the edit text box opens as a Pop-up.
We can type the desired text here.
First, we need to bind the data to the Report, and then the Report can use it.
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.
Now we need to add the collection data below the column header. For that add a new data band to the existing data band.
First, associate the collection data with this new data band.
Insert a text box in the new data band and add the collection parameter.
Save and Run.
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.
I will write a controller GenerateReportPdf in the project FastReportPdf.Controllers.
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:-
Any comments or suggestions would be greatly appreciated.
Top comments (0)