Generate PDF Reports in ASP.NET with C# or VB

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 in C# and helps visualize and export SSIS reports to PDF in ASP.NET C#.

IronPDF can be used to render snapshots of data as "reports" in PDF file format. It also works as a PDF C# parser.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet

    PM > Install-Package IronPdf

  2. Copy the code

    new IronPdf.ChromePdfRenderer().RenderHtmlFileAsPdf("report.html").SaveAs("report.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

Step 1

1. Install IronPDF

Using NuGet: IronPdf on NuGet

Install-Package IronPdf

You 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 an HTML document and then render the HTML as a PDF using IronPDF. This tutorial will show you the steps to create a PDF report in ASP.NET C#.

:path=/static-assets/pdf/content-code-examples/how-to/csharp-pdf-reports-render-html-file.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

renderer.RenderHtmlFileAsPdf("report.html").SaveAs("report.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

renderer.RenderHtmlFileAsPdf("report.html").SaveAs("report.pdf")
$vbLabelText   $csharpLabel

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:

:path=/static-assets/pdf/content-code-examples/how-to/csharp-pdf-reports-render-header-footer.cs
using IronPdf;
using IronSoftware.Drawing;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Add a header to very page easily
renderer.RenderingOptions.FirstPageNumber = 1;
renderer.RenderingOptions.TextHeader.DrawDividerLine = true;
renderer.RenderingOptions.TextHeader.CenterText = "{url}";
renderer.RenderingOptions.TextHeader.Font = FontTypes.Arial;
renderer.RenderingOptions.TextHeader.FontSize = 12;

// Add a footer too
renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
renderer.RenderingOptions.TextFooter.Font = FontTypes.Arial;
renderer.RenderingOptions.TextFooter.FontSize = 10;
renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";

renderer.RenderHtmlFileAsPdf(@"c:\my\exported\report.html").SaveAs("report.pdf");
Imports IronPdf
Imports IronSoftware.Drawing

Private renderer As New ChromePdfRenderer()

' Add a header to very page easily
renderer.RenderingOptions.FirstPageNumber = 1
renderer.RenderingOptions.TextHeader.DrawDividerLine = True
renderer.RenderingOptions.TextHeader.CenterText = "{url}"
renderer.RenderingOptions.TextHeader.Font = FontTypes.Arial
renderer.RenderingOptions.TextHeader.FontSize = 12

' Add a footer too
renderer.RenderingOptions.TextFooter.DrawDividerLine = True
renderer.RenderingOptions.TextFooter.Font = FontTypes.Arial
renderer.RenderingOptions.TextFooter.FontSize = 10
renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}"
renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}"

renderer.RenderHtmlFileAsPdf("c:\my\exported\report.html").SaveAs("report.pdf")
$vbLabelText   $csharpLabel

3.1 Crystal Reports to PDF Programmatically with C#

If you wish to work programmatically to create a PDF from a Crystal Reports (RPT) file, it's also possible and gives you much more control.

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using IronPdf;

public static void ExportRptToPdf(string rptPath, string pdfOutputPath)
{
    ReportDocument rpt = new ReportDocument();
    rpt.Load(rptPath);

    // Set export options
    DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions()
    {
        DiskFileName = @"c:\tmp\html\b.html"
    };

    ExportOptions exportOpts = ExportOptions.CreateExportOptions();
    exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
    exportOpts.ExportFormatType = ExportFormatType.HTML40;
    exportOpts.ExportDestinationOptions = diskOpts;

    // Export report to HTML
    rpt.Export();

    // Convert HTML to PDF
    var Renderer = new ChromePdfRenderer();
    Renderer.RenderingOptions.TextHeader.CenterText = "{url}";
    Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
    Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";

    Renderer.RenderFileAsPdf(diskOpts.DiskFileName).SaveAs(pdfOutputPath);

    Console.WriteLine("Report Written To {0}", Path.GetFullPath(pdfOutputPath));
}
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using IronPdf;

public static void ExportRptToPdf(string rptPath, string pdfOutputPath)
{
    ReportDocument rpt = new ReportDocument();
    rpt.Load(rptPath);

    // Set export options
    DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions()
    {
        DiskFileName = @"c:\tmp\html\b.html"
    };

    ExportOptions exportOpts = ExportOptions.CreateExportOptions();
    exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
    exportOpts.ExportFormatType = ExportFormatType.HTML40;
    exportOpts.ExportDestinationOptions = diskOpts;

    // Export report to HTML
    rpt.Export();

    // Convert HTML to PDF
    var Renderer = new ChromePdfRenderer();
    Renderer.RenderingOptions.TextHeader.CenterText = "{url}";
    Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
    Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";

    Renderer.RenderFileAsPdf(diskOpts.DiskFileName).SaveAs(pdfOutputPath);

    Console.WriteLine("Report Written To {0}", Path.GetFullPath(pdfOutputPath));
}
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO
Imports IronPdf

Public Shared Sub ExportRptToPdf(ByVal rptPath As String, ByVal pdfOutputPath As String)
	Dim rpt As New ReportDocument()
	rpt.Load(rptPath)

	' Set export options
	Dim diskOpts As New DiskFileDestinationOptions() With {.DiskFileName = "c:\tmp\html\b.html"}

	Dim exportOpts As ExportOptions = ExportOptions.CreateExportOptions()
	exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
	exportOpts.ExportFormatType = ExportFormatType.HTML40
	exportOpts.ExportDestinationOptions = diskOpts

	' Export report to HTML
	rpt.Export()

	' Convert HTML to PDF
	Dim Renderer = New ChromePdfRenderer()
	Renderer.RenderingOptions.TextHeader.CenterText = "{url}"
	Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}"
	Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}"

	Renderer.RenderFileAsPdf(diskOpts.DiskFileName).SaveAs(pdfOutputPath)

	Console.WriteLine("Report Written To {0}", Path.GetFullPath(pdfOutputPath))
End Sub
$vbLabelText   $csharpLabel

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 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 in the article Using the XslCompiledTransform Class.

The resultant HTML string or file may then be rendered as a PDF using IronPDF:

using System.IO;
using System.Xml;
using System.Xml.Xsl;
using IronPdf;

public static void ConvertXmlToPdf(string xml, string xslt, string pdfOutputPath)
{
    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);
    }

    ChromePdfRenderer renderer = new ChromePdfRenderer();
    renderer.RenderHtmlAsPdf(results.ToString()).SaveAs(pdfOutputPath);
}
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using IronPdf;

public static void ConvertXmlToPdf(string xml, string xslt, string pdfOutputPath)
{
    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);
    }

    ChromePdfRenderer renderer = new ChromePdfRenderer();
    renderer.RenderHtmlAsPdf(results.ToString()).SaveAs(pdfOutputPath);
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
Imports IronPdf

Public Shared Sub ConvertXmlToPdf(ByVal xml As String, ByVal xslt As String, ByVal pdfOutputPath As String)
	Dim transform As New XslCompiledTransform()
	Using reader As XmlReader = XmlReader.Create(New StringReader(xslt))
		transform.Load(reader)
	End Using

	Dim results As New StringWriter()
	Using reader As XmlReader = XmlReader.Create(New StringReader(xml))
		transform.Transform(reader, Nothing, results)
	End Using

	Dim renderer As New ChromePdfRenderer()
	renderer.RenderHtmlAsPdf(results.ToString()).SaveAs(pdfOutputPath)
End Sub
$vbLabelText   $csharpLabel

Please visit the Convert XML to PDF in C# and VB.NET article to learn more.

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.

Tutorial: How to locate and start Reporting Services tools (SSRS)

These reports may be generated as HTML which may then be customized and converted to PDF format using IronPDF.

Render to HTML (Report Builder)

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.

:path=/static-assets/pdf/content-code-examples/how-to/csharp-pdf-reports-sign-pdf.cs
using IronPdf.Signing;

// Sign our PDF Report using a p12 or pix digital certificate file
new PdfSignature("IronSoftware.pfx", "123456").SignPdfFile("signed.pdf");
Imports IronPdf.Signing

' Sign our PDF Report using a p12 or pix digital certificate file
Call (New PdfSignature("IronSoftware.pfx", "123456")).SignPdfFile("signed.pdf")
$vbLabelText   $csharpLabel

If you do not have a digital signature, you can 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 application.

using IronPdf;

public static void RenderAspxToPdf()
{
    var AspxToPdfOptions = new ChromePdfRenderOptions()
    {
        EnableJavaScript = false, // Disable JavaScript for simplicity
        // ...many more options available
    };

    AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.Attachment, "Report.pdf", AspxToPdfOptions);
}
using IronPdf;

public static void RenderAspxToPdf()
{
    var AspxToPdfOptions = new ChromePdfRenderOptions()
    {
        EnableJavaScript = false, // Disable JavaScript for simplicity
        // ...many more options available
    };

    AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.Attachment, "Report.pdf", AspxToPdfOptions);
}
Imports IronPdf

Public Shared Sub RenderAspxToPdf()
	Dim AspxToPdfOptions = New ChromePdfRenderOptions() With {.EnableJavaScript = False}

	AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.Attachment, "Report.pdf", AspxToPdfOptions)
End Sub
$vbLabelText   $csharpLabel

We hope this article has helped you in learning 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.

Frequently Asked Questions

How can I generate PDF reports in ASP.NET using C#?

You can generate PDF reports in ASP.NET by using IronPDF to convert HTML documents into PDF format. First, create your report as an HTML document and then use IronPDF's rendering capabilities to convert it to a PDF.

What steps should I follow to install IronPDF for PDF generation in C#?

To install IronPDF, you can use the NuGet Package Manager Console with the command Install-Package IronPdf or download the DLL from the official NuGet website and add it to your project manually.

Can I convert Crystal Reports to PDFs in a C# application?

Yes, you can convert Crystal Reports to PDFs by first exporting them as HTML and then using IronPDF to render the HTML files into PDF format.

How do I secure my PDF reports generated in C#?

You can secure your PDF reports by digitally signing them with IronPDF. This ensures that the PDF has not been altered after it has been created and saved.

Is it possible to handle JavaScript when converting HTML to PDF in C#?

IronPDF can process JavaScript content when rendering HTML to PDF. This feature can be enabled or disabled depending on your project's needs.

How can I convert SQL Server reports to PDF using C#?

You can export SQL Server reports to HTML and then use IronPDF to convert these HTML files into PDFs, ensuring the reports maintain their formatting and layout.

What is the benefit of using HTML for PDF report generation?

Using HTML allows for detailed styling and formatting, which IronPDF can accurately convert into PDF format, ensuring the visual integrity of the report is preserved.

Can I convert XML reports to PDFs using IronPDF in C#?

Yes, you can convert XML reports to PDFs by using XSLT to transform the XML into HTML, which can then be rendered as a PDF using IronPDF.

How do I render an ASPX page to PDF in an ASP.NET WebForms application?

In an ASP.NET WebForms application, you can use IronPdf.AspxToPdf in the Form_Load event to render an ASPX page as a PDF.

Where can I find resources for using IronPDF in C#?

You can find tutorials and resources on the IronPDF website, including guides on converting ASPX to PDF and generating reports from different data sources.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.