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#.
- IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
- 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:
- 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: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.
- 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:myreport.rpt”);
- //diskOpts.DiskFileName = “c:\ReportName.csv”;
- diskOpts.DiskFileName = @”c:tmphtmlb.html”;
- exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
- exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40;
- exportOpts.ExportDestinationOptions = diskOpts;
- HTMLExpOpts = new HTMLFormatOptions();
- HTMLExpOpts.HTMLBaseFolderName = @”c:tmphtmlb.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));
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:
- 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”);
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.
- //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”);
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.
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);
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