Top
ArticleCity.comArticle Categories Generate PDF Reports in ASP.NET with C# or VB

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

  1. IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
  2. 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:

  1. IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
  2. // add a header to very page easily
  3. Renderer.PrintOptions.FirstPageNumber = 1;
  4. Renderer.PrintOptions.Header.DrawDividerLine = true;
  5. Renderer.PrintOptions.Header.CenterText = "{url}" ;
  6. Renderer.PrintOptions.Header.FontFamily = "Helvetica,Arial";
  7. Renderer.PrintOptions.Header.FontSize = 12;
  8. // add a footer too
  9. Renderer.PrintOptions.Footer.DrawDividerLine = true;
  10. Renderer.PrintOptions.Footer.FontFamily = "Arial";
  11. Renderer.PrintOptions.Footer.FontSize = 10;
  12. Renderer.PrintOptions.Footer.LeftText = "{date} {time}";
  13. Renderer.PrintOptions.Footer.RightText = "{page} of {total-pages}";
  14. 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.

  1. CrystalDecisions.Shared.DiskFileDestinationOptions diskOpts = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions();
  2. CrystalDecisions.Shared.ExportOptions exportOpts = new CrystalDecisions.Shared.ExportOptions();
  3. CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions csvExpOpts = new CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions();
  4. CrystalDecisions.Shared.HTMLFormatOptions HTMLExpOpts = new CrystalDecisions.Shared.HTMLFormatOptions();
  5. rpt.Load(@"c:myreport.rpt");
  6. //diskOpts.DiskFileName = "c:\ReportName.csv";
  7. diskOpts.DiskFileName = @"c:tmphtmlb.html";
  8. exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
  9. exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40;
  10. exportOpts.ExportDestinationOptions = diskOpts;
  11. HTMLExpOpts = new HTMLFormatOptions();
  12. HTMLExpOpts.HTMLBaseFolderName = @"c:tmphtmlb.html";
  13. HTMLExpOpts.HTMLEnableSeparatedPages = false;
  14. HTMLExpOpts.UsePageRange = false;
  15. HTMLExpOpts.HTMLHasPageNavigator = false;
  16. System.IO.Stream htmlStream;
  17. byte[] htmlByteArray = null;
  18. htmlStream = rpt.ExportThtmlStream(CrystalDecisions.Shared.ExportFormatType.HTML40);
  19. htmlByteArray = new byte[htmlStream.Length];
  20. htmlStream.Read(htmlByteArray, 0, Convert.ToInt32(htmlStream.Length - 1));
  21. System.IO.File.Create(diskOpts.DiskFileName, Convert.ToInt32(htmlStream.Length - 1)).Close();
  22. System.IO.File.OpenWrite(diskOpts.DiskFileName).Write(htmlByteArray, 0, Convert.ToInt32(htmlStream.Length - 1));
  23. System.IO.File.SetAttributes(diskOpts.DiskFileName, System.IO.FileAttributes.Directory);
  24. htmlStream.Close();
  25. IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
  26. // add a header to very page easily
  27. Renderer.PrintOptions.FirstPageNumber = 1;
  28. Renderer.PrintOptions.Header.DrawDividerLine = true;
  29. Renderer.PrintOptions.Header.CenterText = "{url}" ;
  30. Renderer.PrintOptions.Header.FontFamily = "Helvetica,Arial";
  31. Renderer.PrintOptions.Header.FontSize = 12;
  32. // add a footer too
  33. Renderer.PrintOptions.Footer.DrawDividerLine = true;
  34. Renderer.PrintOptions.Footer.FontFamily = "Arial";
  35. Renderer.PrintOptions.Footer.FontSize = 10;
  36. Renderer.PrintOptions.Footer.LeftText = "{date} {time}";
  37. Renderer.PrintOptions.Footer.RightText = "{page} of {total-pages}";
  38. Renderer.RenderFileAsPdf(diskOpts.DiskFileName).SaveAs("Report.pdf");
  39. 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:

  1. XslCompiledTransform transform = new XslCompiledTransform();
  2. using(XmlReader reader = XmlReader.Create(new StringReader(XSLT))) {
  3. transform.Load(reader);
  4. }
  5. StringWriter results = new StringWriter();
  6. using(XmlReader reader = XmlReader.Create(new StringReader(XML))) {
  7. transform.Transform(reader, null, results);
  8. }
  9. IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
  10. // options, headers and footers may be set there
  11. // Render our report as a PDF
  12. 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.

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

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.

  1. //Sign our PDF Report using a p12 or pix digital certificate file
  2. 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.

  1. IronPdf.PdfPrintOptions PdfOptions = new PdfPrintOptions()
  2. {
  3. DPI = 300,
  4. EnableJavaScript = false,
  5. //.. many more options available
  6. };
  7. 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.

No Comments

Sorry, the comment form is closed at this time.