在ASP.NET中使用C#或VB生成PDF报告
从 SQL 等结构化数据生成管理或数据库报告是一个常见的 .NET 开发任务。 IronPDF可以用作PDF阅读器C#,并帮助在ASP.NET C#中将ssrs报告可视化并导出为PDF。
IronPDF可用于将数据快照呈现为PDF文件格式的“报告”。 它还可以作为PDF C#解析器使用。
如何生成 C# PDF 报告
- 下载并安装 C# PDF 报告库
- 将报告生成 HTML 文档,然后将 HTML 文档渲染为 PDF 文件
- 使用以下方法导出为 HTML文件 -> 导出并选择 HTML 4.0
- 在 XML 报告样式中,可对 XML 进行解析,然后使用数据生成 HTML
- 确保 PDF 报告未被修改或篡改,可对其进行数字签名
步骤 1
1. 安装IronPDF
使用 NuGet:https://www.nuget.org/packages/IronPdf
Install-Package IronPdf
您还可以下载手动运行 IronPDF DLL。
教程
2. 创建PDF报告的方法论
首先将报告生成为HTML文档,然后使用IronPDF将HTML渲染为PDF。 本教程将向您展示如何在ASP.NET C#中创建PDF报告。
:path=/static-assets/pdf/content-code-examples/how-to/csharp-pdf-reports-render-html-file.cs
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderHtmlFileAsPdf("report.html").SaveAs("report.pdf");
Imports IronPdf
Private renderer As New ChromePdfRenderer()
renderer.RenderHtmlFileAsPdf("report.html").SaveAs("report.pdf")
3. 使用 .NET 将 Crystal Reports 转换为 PDF
在Crystal Reports应用程序中,您可以使用以下方法导出到HTML:
文件 -> 导出 并选择 HTML 4.0
生成的报告可以使用方法部分上述的C#示例代码导出为PDF。
这里有一个例子:
:path=/static-assets/pdf/content-code-examples/how-to/csharp-pdf-reports-render-header-footer.cs
using IronPdf;
using IronSoftware.Drawing;
ChromePdfRenderer renderer = new 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.Font = FontTypes.Arial;
renderer.RenderingOptions.TextHeader.FontSize = 12;
// Add a footer too
renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
renderer.RenderingOptions.TextFooter.Font = FontTypes.Arial;
renderer.RenderingOptions.TextFooter.FontSize = 10;
renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
renderer.RenderHtmlFileAsPdf(@"c:\my\exported\report.html").SaveAs("report.pdf");
Imports IronPdf
Imports IronSoftware.Drawing
Private renderer As New 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.Font = FontTypes.Arial
renderer.RenderingOptions.TextHeader.FontSize = 12
' Add a footer too
renderer.RenderingOptions.TextFooter.DrawDividerLine = True
renderer.RenderingOptions.TextFooter.Font = FontTypes.Arial
renderer.RenderingOptions.TextFooter.FontSize = 10
renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}"
renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}"
renderer.RenderHtmlFileAsPdf("c:\my\exported\report.html").SaveAs("report.pdf")
3.1 使用C#将Crystal Reports转换为PDF程序
如果您希望以编程方式从Crystal Reports创建PDF(RPT)这也是可以实现的,而且能让您控制得更多。
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.Font = IronSoftware.Drawing.FontTypes.Arial;
renderer.RenderingOptions.TextHeader.FontSize = 12;
// Add a footer too
renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
renderer.RenderingOptions.TextFooter.Font = IronSoftware.Drawing.FontTypes.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));
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.Font = IronSoftware.Drawing.FontTypes.Arial;
renderer.RenderingOptions.TextHeader.FontSize = 12;
// Add a footer too
renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
renderer.RenderingOptions.TextFooter.Font = IronSoftware.Drawing.FontTypes.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));
IRON VB CONVERTER ERROR developers@ironsoftware.com
4. XML 报告
尽管像JSON这样更易于编码的格式已经很普遍,但导出报告数据为XML格式仍然很常见。
要对XML报告进行样式设计,可以解析XML,然后用数据生成HTML。
使用 XSLT 将 XML 直接转换为 HTML 的更优雅的解决方案是使用 XslCompiledTransform 类,如下文所述:'使用 XslCompiledTransform 类.'
生成的HTML字符串或文件可以使用IronPDF渲染为PDF。
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.RenderHtmlFileAsPdf(results.ToString()).SaveAs("Report.pdf");
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.RenderHtmlFileAsPdf(results.ToString()).SaveAs("Report.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
请访问用 C# 和 VB.NET 将 XML 转换为 PDF'一文了解更多信息。
5. 微软SQL Server报告
Microsoft的SQL Server和免费的SQL Server Express包含报告工具。 在ASP.NET中将SSRS报告导出为PDF文件是IronPDF的一个有用途径。
这些报告可以生成为HTML,然后可以使用IronPDF进行自定义并转换为PDF格式。
6. 报告安全性
为了确保PDF报告未被修改或篡改,可以对其进行数字签名。 这最容易在PDF报告文件渲染并保存到磁盘后实现。
:path=/static-assets/pdf/content-code-examples/how-to/csharp-pdf-reports-sign-pdf.cs
using IronPdf.Signing;
// Sign our PDF Report using a p12 or pix digital certificate file
new PdfSignature("IronSoftware.pfx", "123456").SignPdfFile("signed.pdf");
Imports IronPdf.Signing
' Sign our PDF Report using a p12 or pix digital certificate file
Call (New PdfSignature("IronSoftware.pfx", "123456")).SignPdfFile("signed.pdf")
如果没有数字签名,可以在 macOS 和 Windows 上使用免费的 Adobe Acrobat Reader 创建新的数字签名文件。
7. 使用ASP.NET Webforms将ASPX转换为PDF
在ASP.NET中提供html内容的最简单方法是在ASP.NET WebForms的Form_Load事件中使用IronPdf.AspxToPdf类。
var AspxToPdfOptions = new IronPdf.ChromePdfRenderOptions()
{
EnableJavaScript = false,
//.. many more options available
};
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Report.pdf", AspxToPdfOptions);
var AspxToPdfOptions = new IronPdf.ChromePdfRenderOptions()
{
EnableJavaScript = false,
//.. many more options available
};
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Report.pdf", AspxToPdfOptions);
IRON VB CONVERTER ERROR developers@ironsoftware.com
我们希望这篇文章能帮助您学习如何在ASP.NET C#或VB.NET中生成PDF报告。 您也可以浏览我们完整的ASP.NET ASPX 转 PDF 教程了解更多信息。