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

Photo by Fatos Bytyqi Originally Posted On: https://ironpdf.com/docs/questions/csharp-pdf-reports/

Saturday, March 7th 2020, 3:19 am

By: News On 6


How to Generate PDF Reports in ASP.NET with C# or VB?Photo by Fatos Bytyqi

Originally Posted On: 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 help allow for visaulise and export ssrs reports to pdf in asp.net c#.

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. 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”);

Copy code to clipboardVB 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:

  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”);

Copy code to clipboardVB 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.

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

Copy code to clipboardVB 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:

  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”);

Copy code to clipboardVB 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. 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

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”);

Copy code to clipboardVB 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.

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

Copy code to clipboardVB C#

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.

Information contained on this page is provided by an independent third-party content provider. Frankly and this Site make no warranties or representations in connection therewith. If you are affiliated with this page and would like it removed please contact pressreleases@franklymedia.com

logo

Get The Daily Update!

Be among the first to get breaking news, weather, and general news updates from News on 6 delivered right to your inbox!

More Like This

March 7th, 2020

April 19th, 2024

April 19th, 2024

April 19th, 2024

Top Headlines

April 19th, 2024

April 19th, 2024

April 19th, 2024

April 19th, 2024