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 C# and help 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.


Step 1

1. Install IronPDF

Using NuGet: https://www.nuget.org/packages/IronPdf

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 how 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")
VB   C#

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")
VB   C#

3.1 Crystal Reports to PDF Programmatically with C Sharp

If you wish to work programmatically to create a PDF from a Crystal Reports(RPT) file 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:\my\report.rpt");

//diskOpts.DiskFileName = "c:\\ReportName.csv";

diskOpts.DiskFileName = @"c:\tmp\html\b.html";

exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40;
exportOpts.ExportDestinationOptions = diskOpts;

HTMLExpOpts = new HTMLFormatOptions();
HTMLExpOpts.HTMLBaseFolderName = @"c:\tmp\html\b.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.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();

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

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

Renderer.RenderFileAsPdf(diskOpts.DiskFileName).SaveAs("Report.pdf");

Console.WriteLine("Report Written To {0}", Path.GetFullPath(diskOpts.DiskFileName));
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:\my\report.rpt");

//diskOpts.DiskFileName = "c:\\ReportName.csv";

diskOpts.DiskFileName = @"c:\tmp\html\b.html";

exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40;
exportOpts.ExportDestinationOptions = diskOpts;

HTMLExpOpts = new HTMLFormatOptions();
HTMLExpOpts.HTMLBaseFolderName = @"c:\tmp\html\b.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.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();

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

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

Renderer.RenderFileAsPdf(diskOpts.DiskFileName).SaveAs("Report.pdf");

Console.WriteLine("Report Written To {0}", Path.GetFullPath(diskOpts.DiskFileName));
Dim diskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions()

Dim exportOpts As New CrystalDecisions.Shared.ExportOptions()

Dim csvExpOpts As New CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions()

Dim HTMLExpOpts As New CrystalDecisions.Shared.HTMLFormatOptions()

rpt.Load("c:\my\report.rpt")

'diskOpts.DiskFileName = "c:\\ReportName.csv";

diskOpts.DiskFileName = "c:\tmp\html\b.html"

exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile
exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40
exportOpts.ExportDestinationOptions = diskOpts

HTMLExpOpts = New HTMLFormatOptions()
HTMLExpOpts.HTMLBaseFolderName = "c:\tmp\html\b.html"
HTMLExpOpts.HTMLEnableSeparatedPages = False
HTMLExpOpts.UsePageRange = False
HTMLExpOpts.HTMLHasPageNavigator = False

Dim htmlStream As System.IO.Stream

Dim htmlByteArray() As Byte = Nothing

htmlStream = rpt.ExportThtmlStream(CrystalDecisions.Shared.ExportFormatType.HTML40)

htmlByteArray = New Byte(htmlStream.Length - 1){}

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()

Dim Renderer As New IronPdf.ChromePdfRenderer()

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

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

Renderer.RenderFileAsPdf(diskOpts.DiskFileName).SaveAs("Report.pdf")

Console.WriteLine("Report Written To {0}", Path.GetFullPath(diskOpts.DiskFileName))
VB   C#

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 in the article 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.ChromePdfRenderer renderer = new IronPdf.ChromePdfRenderer();

// Options, headers, and footers may be set there
// Render our report as a PDF
renderer.RenderHtmlFileAsPdf(results.ToString()).SaveAs("Report.pdf");
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.ChromePdfRenderer renderer = new IronPdf.ChromePdfRenderer();

// Options, headers, and footers may be set there
// Render our report as a PDF
renderer.RenderHtmlFileAsPdf(results.ToString()).SaveAs("Report.pdf");
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 IronPdf.ChromePdfRenderer()

' Options, headers, and footers may be set there
' Render our report as a PDF
renderer.RenderHtmlFileAsPdf(results.ToString()).SaveAs("Report.pdf")
VB   C#

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")
VB   C#

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.

var AspxToPdfOptions = new IronPdf.ChromePdfRenderOptions()
{
  EnableJavaScript = false,
  //.. many more options available
};

IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Report.pdf", AspxToPdfOptions);
var AspxToPdfOptions = new IronPdf.ChromePdfRenderOptions()
{
  EnableJavaScript = false,
  //.. many more options available
};

IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Report.pdf", AspxToPdfOptions);
Dim AspxToPdfOptions = New IronPdf.ChromePdfRenderOptions() With {.EnableJavaScript = False}

IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Report.pdf", AspxToPdfOptions)
VB   C#

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.