Generate PDF Reports in ASP.NET with C# or VB
Published: 2022-01-20 06:46:58

---
canonical_url: https://ironpdf.com/docs/questions/csharp-pdf-reports/
---

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#](https://ironpdf.com/use-case/pdf-reader-csharp/) 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](https://ironpdf.com/use-case/csharp-pdf-parser/).

---

**Step 1**

# 1. Install IronPDF

Using NUGET: [https://www.nuget.org/packages/IronPdf](https://www.nuget.org/packages/IronPdf)

`PM > Install-Package IronPdf`

You can also [download](https://ironpdf.com/packages/IronPdf.Package.For.net.csharp.pdf.reports.zip) 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#

**C#:**
```

IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
Renderer.RenderFileAsPdf("report.html")SaveAs("report.pdf");
```

**VB:**
```
Dim Renderer As New IronPdf.ChromePdfRenderer()
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:

**C#:**
```

IronPdf.ChromePdfRenderer Renderer = new IronPdf.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.FontFamily = "Helvetica,Arial";
Renderer.RenderingOptions.TextHeader.FontSize = 12;
// add a footer too
Renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
Renderer.RenderingOptions.TextFooter.FontFamily = "Arial";
Renderer.RenderingOptions.TextFooter.FontSize = 10;
Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
Renderer.RenderFileAsPdf(@"c:\my\exported\report.html").SaveAs("report.pdf");
```

**VB:**
```
Dim Renderer As New IronPdf.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.FontFamily = "Helvetica,Arial"
Renderer.RenderingOptions.TextHeader.FontSize = 12
' add a footer too
Renderer.RenderingOptions.TextFooter.DrawDividerLine = True
Renderer.RenderingOptions.TextFooter.FontFamily = "Arial"
Renderer.RenderingOptions.TextFooter.FontSize = 10
Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}"
Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}"
Renderer.RenderFileAsPdf("c:\my\exported\report.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 give you much more control.

**C#:**
```

  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 very page easily
Renderer.RenderingOptions.FirstPageNumber = 1;
Renderer.RenderingOptions.TextHeader.DrawDividerLine = true;
Renderer.RenderingOptions.TextHeader.CenterText = "{url}" ;
Renderer.RenderingOptions.TextHeader.FontFamily = "Helvetica,Arial";
Renderer.RenderingOptions.TextHeader.FontSize = 12;
// add a footer too
Renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
Renderer.RenderingOptions.TextFooter.FontFamily = "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");
Cosole.WriteLine("Report Weitten To {0}", Path.GetFullPath(diskOpts.DiskFileName));
```

**VB:**
```
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 very page easily
Renderer.RenderingOptions.FirstPageNumber = 1
Renderer.RenderingOptions.TextHeader.DrawDividerLine = True
Renderer.RenderingOptions.TextHeader.CenterText = "{url}"
Renderer.RenderingOptions.TextHeader.FontFamily = "Helvetica,Arial"
Renderer.RenderingOptions.TextHeader.FontSize = 12
' add a footer too
Renderer.RenderingOptions.TextFooter.DrawDividerLine = True
Renderer.RenderingOptions.TextFooter.FontFamily = "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")
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](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:

**C#:**
```
    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.RenderHtmlAsPdf(results.ToString()).SaveAs("Report.pdf");
```

**VB:**
```
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.RenderHtmlAsPdf(results.ToString()).SaveAs("Report.pdf")
```

_Note:_ You can also use [Byte Arrays with HTML Strings](https://ironpdf.com/docs/questions/asp-net-mvc-pdf-binary/)

# 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.

[https://docs.microsoft.com/en-us/sql/reporting-services/tools/tutorial-how-to-locate-and-start-reporting-services-tools-ssrs?view=sql-server-2017](https://docs.microsoft.com/en-us/sql/reporting-services/tools/tutorial-how-to-locate-and-start-reporting-services-tools-ssrs?view=sql-server-2017)

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

[https://docs.microsoft.com/en-us/sql/reporting-services/report-builder/rendering-to-html-report-builder-and-ssrs?view=sql-server-2017](https://docs.microsoft.com/en-us/sql/reporting-services/report-builder/rendering-to-html-report-builder-and-ssrs?view=sql-server-2017)

# 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.

**C#:**
```

//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");
```

**VB:**
```
'Sign our PDF Report using a p12 or pix digital certificate file
Call (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.

**C#:**
```

   IronPdf.PdfPrintOptions PdfOptions = new PdfPrintOptions()
    {
        DPI = 300,
        EnableJavaScript = false,
        //.. many more options available
    };
   IronPdf.AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.Attachment,"MyPdfFile.pdf", PdfOptions);
```

**VB:**
```
Dim PdfOptions As IronPdf.PdfPrintOptions = New PdfPrintOptions() With {
	.DPI = 300,
	.EnableJavaScript = False
}
   IronPdf.AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.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](https://ironpdf.com/tutorials/aspx-to-pdf/) to learn more.