Generate PDF Reports in ASP.NET with C# or VB
Photo by Danial Igdery
Originally Posted On: How to generate PDF reports in C# : Crystal Reports, XML and IIS | Iron Pdf
Generating management or database reports from structured data such as SQL is a common .NET development task. IronPDF can be used as a PDF reader C# and help visualize and export ssrs reports to PDF in ASP.NET csharp.
IronPDF can be used to render snapshots of data as “reports” in the PDF File Format. It also works as a PDF C# parser.
Step 1
1. Install IronPDF
Using NUGET: https://www.nuget.org/packages/IronPdf
PM > Install-Package IronPdf
Youy can also download the IronPDF DLL manually .
How to Tutorial
2. Methodology for Creating a PDF Report
The basic methodology is to first generate the report as a HTML document – and then render the HTML as a PDF using IronPDF. This tutorial will show you how to create a pdf report in asp .net c#.
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
Renderer.RenderFileAsPdf("report.html")SaveAs("report.pdf");
3. Crystal Reports to PDF with .Net
In the Crystal Reports Application you may export to HTML using:
File -> Export and select HTML 4.0
The resulting report can then be exported as a PDF using the above C# example code in the Methodology section.
Here’s an example:
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// add a header to very page easily
Renderer.PrintOptions.FirstPageNumber = 1;
Renderer.PrintOptions.Header.DrawDividerLine = true;
Renderer.PrintOptions.Header.CenterText = "{url}" ;
Renderer.PrintOptions.Header.FontFamily = "Helvetica,Arial";
Renderer.PrintOptions.Header.FontSize = 12;
// add a footer too
Renderer.PrintOptions.Footer.DrawDividerLine = true;
Renderer.PrintOptions.Footer.FontFamily = "Arial";
Renderer.PrintOptions.Footer.FontSize = 10;
Renderer.PrintOptions.Footer.LeftText = "{date} {time}";
Renderer.PrintOptions.Footer.RightText = "{page} of {total-pages}";
Renderer.RenderFileAsPdf(@"c:myexportedreport.html").SaveAs("report.pdf");
3.1 Crystal Reports to PDF Programmatically with C Sharp
If you wish to work programmatically to create a PDF from a Crystal Report file rpt this is also possible and gives you much more control.
CrystalDecisions.Shared.DiskFileDestinationOptions diskOpts = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions();
CrystalDecisions.Shared.ExportOptions exportOpts = new CrystalDecisions.Shared.ExportOptions();
CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions csvExpOpts = new CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions();
CrystalDecisions.Shared.HTMLFormatOptions HTMLExpOpts = new CrystalDecisions.Shared.HTMLFormatOptions();
rpt.Load(@"c:myreport.rpt");
//diskOpts.DiskFileName = "c:\ReportName.csv";
diskOpts.DiskFileName = @"c:tmphtmlb.html";
exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40;
exportOpts.ExportDestinationOptions = diskOpts;
HTMLExpOpts = new HTMLFormatOptions();
HTMLExpOpts.HTMLBaseFolderName = @"c:tmphtmlb.html";
HTMLExpOpts.HTMLEnableSeparatedPages = false;
HTMLExpOpts.UsePageRange = false;
HTMLExpOpts.HTMLHasPageNavigator = false;
System.IO.Stream htmlStream;
byte[] htmlByteArray = null;
htmlStream = rpt.ExportThtmlStream(CrystalDecisions.Shared.ExportFormatType.HTML40);
htmlByteArray = new byte[htmlStream.Length];
htmlStream.Read(htmlByteArray, 0, Convert.ToInt32(htmlStream.Length - 1));
System.IO.File.Create(diskOpts.DiskFileName, Convert.ToInt32(htmlStream.Length - 1)).Close();
System.IO.File.OpenWrite(diskOpts.DiskFileName).Write(htmlByteArray, 0, Convert.ToInt32(htmlStream.Length - 1));
System.IO.File.SetAttributes(diskOpts.DiskFileName, System.IO.FileAttributes.Directory);
htmlStream.Close();
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// add a header to very page easily
Renderer.PrintOptions.FirstPageNumber = 1;
Renderer.PrintOptions.Header.DrawDividerLine = true;
Renderer.PrintOptions.Header.CenterText = "{url}" ;
Renderer.PrintOptions.Header.FontFamily = "Helvetica,Arial";
Renderer.PrintOptions.Header.FontSize = 12;
// add a footer too
Renderer.PrintOptions.Footer.DrawDividerLine = true;
Renderer.PrintOptions.Footer.FontFamily = "Arial";
Renderer.PrintOptions.Footer.FontSize = 10;
Renderer.PrintOptions.Footer.LeftText = "{date} {time}";
Renderer.PrintOptions.Footer.RightText = "{page} of {total-pages}";
Renderer.RenderFileAsPdf(diskOpts.DiskFileName).SaveAs("Report.pdf");
Cosole.WriteLine("Report Weitten To {0}", Path.GetFullPath(diskOpts.DiskFileName));
4. XML Reports
Exporting report data as XML is still common despite the prevalence of easier to code formats such as JSON.
To style an XML report, the XML may be parsed and then HTML generated with the data.
A more elegant solution is to use XSLT to convert XML directly to HTML using the XslCompiledTransform class as documented here:
https://docs.microsoft.com/en-us/dotnet/standard/data/xml/using-the-xslcompiledtransform-class
The resultant HTML string or file may then be rendered as a PDF using IronPDF:
XslCompiledTransform transform = new XslCompiledTransform();
using(XmlReader reader = XmlReader.Create(new StringReader(XSLT))) {
transform.Load(reader);
}
StringWriter results = new StringWriter();
using(XmlReader reader = XmlReader.Create(new StringReader(XML))) {
transform.Transform(reader, null, results);
}
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// options, headers and footers may be set there
// Render our report as a PDF
Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Report.pdf");
Note: You can also use Byte Arrays with HTML Strings.
5. Microsoft SQL Server Reports
Microsoft’s SQL Server and the free SQL server Express contain reporting tools. Exporting SSRS reports to a PDF in ASP .net can be a useful use of IronPDF.
These reports may be generated as HTML which may then be customized and converted to PDF format using IronPDF.
6. Report Security
To ensure a PDF report has not been modified or tampered with, it may be digitally signed.
This is most easily achieved on a PDF Report file after it has been rendered and saved to disk.
//Sign our PDF Report using a p12 or pix digital certificate file
new IronPdf.PdfSignature("my_signature.p12", "my_password").SignPdfFile("my_report_file.pdf");
If you do not have a digital signature – you may create a new digital signature file using the free Adobe Acrobat Reader on MacOS and Windows.
7. ASPX to PDF with ASP.Net Webforms
The easiest way to serve html content in ASP.Net is to use the IronPdf.AspxToPdf class on the Form_Load event of an ASP.Net WebForms.
IronPdf.PdfPrintOptions PdfOptions = new PdfPrintOptions()
{
DPI = 300,
EnableJavaScript = false,
//.. many more options available
};
IronPdf.AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehaviour.Attachment,"MyPdfFile.pdf", PdfOptions);
We hope this help file has helped you to know how to generate a pdf report in asp.net c# or VB.net. You can also take a look through our full ASP.Net ASPX to PDF Tutorial to learn more.