How to Generate PDF Reports in C# and VB?

Generating management or database reports from structured data such as SQL is a common .Net development task.

IronPDF can use used to render snapshots of data as "reports" in the PDF File Format.

Methodology

The basic methodology is to first generate the report as a HTML document - and then render the HTML as a PDF using IronPDF.


IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();

Renderer.RenderFileAsPdf("report.html")SaveAs("report.pdf");
Dim Renderer As IronPdf.HtmlToPdf = New IronPdf.HtmlToPdf
Renderer.RenderFileAsPdf("report.html")
SaveAs("report.pdf")
VB   C#

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:\my\exported\report.html").SaveAs("report.pdf");
Dim Renderer As IronPdf.HtmlToPdf = 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:\my\exported\report.html").SaveAs("report.pdf")
VB   C#

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:\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.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));
Dim diskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions
Dim exportOpts As CrystalDecisions.Shared.ExportOptions = New CrystalDecisions.Shared.ExportOptions
Dim csvExpOpts As CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions = New CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions
Dim HTMLExpOpts As CrystalDecisions.Shared.HTMLFormatOptions = 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 IronPdf.HtmlToPdf = 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))
VB   C#

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");
Dim transform As XslCompiledTransform = New XslCompiledTransform
Imports (
Dim reader As XmlReader = XmlReader.Create(New StringReader(XSLT))
UnknownDim results As StringWriter = New StringWriter
Imports (
Dim reader As XmlReader = XmlReader.Create(New StringReader(XML))
,Nothing
,results
UnknownUnknownDim Renderer As IronPdf.HtmlToPdf = New IronPdf.HtmlToPdf
' options, headers and footers may be set there
' Render our report as a PDF
Renderer.RenderHtmlAsPdf(results.ToString).SaveAs("Report.pdf")
VB   C#

Note: You can also use Byte Arrays with HTML Strings.

Microsoft SQL Server Reports

Microsoft's SQL Server and the free SQL server Express contain reporting tools.

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

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

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.

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);
Dim PdfOptions As IronPdf.PdfPrintOptions = New PdfPrintOptions
IronPdf.AspxToPdf.RenderThisPageAsPDF(AspxToPdf.FileBehaviour.Attachment, "MyPdfFile.pdf", PdfOptions)
VB   C#

You can also take a look through our full ASP.Net ASPX to PDF Tutorial to learn more.