FastReport to PDF Demo.
Minimal C# code to load a FastReport report template, populate a dataset, then export to PDF file.
Project page: https://github.com/arvinboggs/FastReportPdf
Warning: FastReport is a commercial product with price starting at $499. They also offer a free open source edition but the PDF export feature is actually an image export. That is, the text of the rendered PDF can neither be selected nor copied. For the purpose of creating a proof-of-concept project, I opted to use their trial edition instead of their open source edition.
- Open Visual Studio 2022 and create a console application.
- Optional. Set the target OS to Windows. This will minimize the file dependencies of FastReport nuget.
- Go to Package Manager Console and run the following:
Install-Package FastReport.Net.Demo
- On the startup method, write this section of code:
1 2 3 4
var pDataSet = CreateDataSet(); // optional. not needed for report rendering but needed for report designing var pFilename = System.IO.Path.Combine(Util.AppDirectory, "dataset.xml"); pDataSet.WriteXml(pFilename, XmlWriteMode.WriteSchema);
- The
CreateDataSet
function can anything that output aDataSet
, ideally from a database query.
- The
- Download FastReport Designer Community Edition
- Extract the zip and run
Designer.exe
. - Create a new blank report.
- Add a new data source.
- Click “New Connection”.
- Choose “XML Database” as the connection type.
- On “Data file or URL” field, browse for “dataset.xml” (created by the code in step 2).
- Click Next.
- Select the table.
- Click Finish.
- Save the report template as “report.frx”.
- Go back to Visual Studio and write these section:
1 2 3 4 5
pFilename = System.IO.Path.Combine(Util.AppDirectory, "report.frx"); var pReport = new Report(); pReport.Load(pFilename); pReport.RegisterData(pDataSet); pReport.Prepare();
- At this point, we are ready to produce a report file that an end user can use.
1 2 3 4 5
pFilename = System.IO.Path.Combine(Util.AppDirectory, "rendered_report.pdf"); FastReport.Export.Pdf.PDFExport pExport = new(); pExport.Producer = "My Application"; // optional pExport.Title = "Products Report"; // optional pReport.Export(pExport, pFilename);
- Done.