在ASP.NET中将ASPX页面转换为PDF
此ASPX转PDF教程将逐步指导您如何在ASP.NET网页应用程序中将ASPX页面保存为PDF文件。
用户永远不应在Google Chrome中打开带有.aspx文件扩展名的ASPX文件。 我们要求工程团队使用.NET代码自动将ASPX转换为PDF! 我们从不需要按CTRL P!有一种基于服务器的方法,可以将ASPX互联网媒体转换并保存为PDF。
应用设置,包括设置文件行为和名称、添加页眉和页脚、更改打印选项、添加分页符、结合异步和多线程等。
概述
Converting ASPX files to PDF in simple steps
- 下载 ASPX 转 PDF C# 库
- 选择 ASPX 页面
- 将 ASPX 文件转换为 PDF
- 应用 ASPX 到 PDF 转换器设置并添加页眉、页脚
- 在 PDF 文件中创建分页符
如何将 ASPX 文件转换为 PDF
ASP.NET常用于开发复杂的网站、在线银行、内网和会计系统的Microsoft Web表单应用程序。 ASP.NET (ASPX) 网站的一个常见功能是生成动态 PDF 文件,如发票、票据或管理报告,以供用户以 PDF 格式下载。
本教程演示如何使用IronPDF软件组件将任何ASP.NET Web表单转换为PDF (ASP.NET to PDF)。 HTML通常作为网页呈现,将用于呈现为PDF供下载或在Web浏览器中查看。 附加的源项目将向您展示如何在ASP.NET中使用C#将网页转换为PDF。
当我们使用IronPDF及其AspxToPdf工具类渲染网页时,实现了将HTML转换为PDF(将ASPX转换为PDF)的功能。
步骤 1
立即在您的项目中开始使用IronPDF,并享受免费试用。
1. 从IronPDF安装ASPX文件转换器免费版。
通过 NuGet 安装
在 Visual Studio 中,右键单击您的项目解决方案资源管理器,然后选择“管理 NuGet 包...”。 从那里开始,搜索IronPDF并安装最新版本... 点击确定以应对任何弹出的对话框。
这适用于任何C# .NET Framework项目,从Framework 4及以上版本,或者.NET Core 2及以上版本。 它在VB.NET项目中同样有效。
Install-Package IronPdf
https://www.nuget.org/packages/IronPdf
通过 DLL 安装
或者,可以从https://ironpdf.com/packages/IronPdf.zip下载IronPDF DLL,并手动安装到项目或GAC中。
请记得在任何使用 IronPDF 的cs类文件的顶部添加这句话:
使用 IronPdf;
教程
2. 将ASP.NET网页转换为PDF
我们从一个普通的ASPX“Web Form”开始,它渲染为HTML。 我们稍后将 ASPX 页面转换为 PDF 文件格式。
在附带的示例源代码中,我们渲染了一个商业发票"Invoice.aspx",这是一个作为ASP.NET页面渲染的简单HTML商业发票。
HTML页面包含CSS3样式表,可能还包括图像和JavaScript。
要将此 ASP.NET 网页渲染为 PDF 而不是 HTML,我们需要打开 C#(或 VB.NET)代码并将其添加到Page_Load事件中:
:path=/static-assets/pdf/content-code-examples/tutorials/aspx-to-pdf-1.cs
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser);
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser)
这就是所需的全部内容; HTML现在渲染为PDF。 超链接、样式表、图像乃至 HTML 表单都得到了保留。 这与用户在浏览器中将 HTML 打印为 PDF 的输出非常相似。 IronPDF 基于支持 Google Chrome 的 Chromium 网络浏览器技术构建。
整个C#代码如下:在Active Server Pages中将ASPX页面转换为PDF。
:path=/static-assets/pdf/content-code-examples/tutorials/aspx-to-pdf-2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IronPdf;
namespace AspxToPdfTutorial
{
public partial class Invoice : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser);
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports IronPdf
Namespace AspxToPdfTutorial
Partial Public Class Invoice
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser)
End Sub
End Class
End Namespace
3. 应用 ASPX 文件到 PDF 转换器设置
在使用 .NET Web Forms 生成的时候,将 ASPX 文件转换为 PDF 有许多可以调整和完善的选项。
这些选项在https://ironpdf.com/object-reference/api/IronPdf.AspxToPdf.html上有完整的在线文档。
3.1 设置PDF文件行为
“InBrowser” 文件行为尝试直接在用户的浏览器中显示 PDF。 这并不总是在每个网络浏览器中都能实现的,但通常是现代、符合标准的浏览器的常见功能。
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser);
附件 文件行为会导致 PDF 被下载。
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment);
3.2. 设置 PDF 文件名
我们还可以通过添加一个额外的参数来设置PDF文档的文件名。 这意味着当用户决定下载或保留文件时,我们可以控制文件的名称。 当我们将 ASPX 页面保存为 PDF 时,将使用此名称命名 PDF 文档。
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf");
3.3 更改PDF打印选项
我们可以通过添加一个IronPdf.ChromePdfRenderer类的实例来控制PDF的输出:
https://ironpdf.com/object-reference/api/IronPdf.ChromePdfRenderer.html
:path=/static-assets/pdf/content-code-examples/tutorials/aspx-to-pdf-3.cs
var AspxToPdfOptions = new IronPdf.ChromePdfRenderOptions()
{
EnableJavaScript = false,
//.. many more options available
};
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf", AspxToPdfOptions);
Dim AspxToPdfOptions = New IronPdf.ChromePdfRenderOptions() With {.EnableJavaScript = False}
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf", AspxToPdfOptions)
可用的PDF渲染选项包括:
- 应用边距到页眉和页脚。 将边距选项应用于HTML头部和尾部。 默认值为 false,这使得 HTML 头部和尾部的边距为 0。 仅支持ChromeRender。
- CreatePdfFormsFromHtml 将 ASPX 表单元素转换为可编辑的 PDF 表单。
- CssMediaType 支持媒体类型为"screen"或"print"的CSS样式和CSS3样式表。
- CustomCssUrl。 允许通过 URL 将自定义 CSS 样式表应用于 HTML。
- 启用JavaScript。 在ASPX页面中启用JavaScript、jQuery甚至Json代码。 可能需要应用渲染延迟。
- FirstPageNumber。 页眉和页脚的第一页页码。 默认值为1。
- FitToPaperWidth。 在可能的情况下,将PDF内容缩小到一张虚拟纸张的宽度。
- 生成唯一的文档标识符。 如果您希望使用二进制文件等式来比较PDF,例如用于单元测试,请将其设置为false。
- 灰度。 输出一个灰度PDF,使用灰色阴影而不是全彩色。
- HtmlHeader。 设置每个PDF页面的头部内容,可以使用内容字符串或者HTML。
- 文本标题. 为每个PDF页面设置页脚内容为文本。 支持“邮件合并”并自动将网址转换为超链接。
- HtmlFooter。 为每个PDF页面设置页脚内容,可以使用内容字符串甚至HTML。
- 文本页脚。 设置每个PDF页面的页眉内容为文本。 支持“邮件合并”并自动将网址转换为超链接。
- InputEncoding。 输入字符编码作为字符串。 UTF-8 是 ASP.NET 的默认编码。
- MarginBottom。 底部PDF纸张边距,以毫米为单位。 设置为零以创建无边框的pdf。
- LeftMargin 左侧PDF纸张边距,单位为毫米。 设置为零以创建无边框的pdf。
- MarginRight。 正确的PDF纸张边距,单位为毫米。 设置为零以创建无边框的pdf。
- MarginTop。 顶级PDF纸张边距,单位为毫米。 设置为零以创建无边框的pdf。
- 纸张方向。 PDF纸张方向。 横向 或 纵向。
- PaperSize。 使用System.Drawing.Printing.PaperKind设置PDF页面的输出纸张大小。 开发人员还可以使用SetCustomPaperSize(int width, int height)方法来创建自定义尺寸。
- PrintHtmlBackgrounds。 打印HTML图像背景。
- RenderDelay。 在 HTML 渲染后等待几毫秒再打印,以便 Javacsript 或 JSON 有时间执行。
- 超时。 渲染超时(秒)。
- 标题。 PDF文档的“标题”元数据。
- ViewPortHeight。 定义一个虚拟屏幕高度,用于在IronPdf中将HTML渲染为PDF。 以像素为单位。
- ViewPortWidth。 在IronPdf中定义用于将HTML渲染为PDF的虚拟屏幕宽度。 以像素为单位。
- 放大。 允许开发者放大或缩小HTML内容的百分比缩放级别。
4. 为 ASPX PDFs 添加页眉和页脚
使用IronPDF,可以在PDF输出中添加页眉和页脚。
最简单的方法是使用SimpleHeaderFooter类,该类支持基本布局,可以轻松添加动态数据,如当前时间和页码。
4.1.ASPX 转换为 PDF 的页眉和页脚示例
:path=/static-assets/pdf/content-code-examples/tutorials/aspx-to-pdf-4.cs
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspxToPdfTutorial
{
public partial class Invoice : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var AspxToPdfOptions = new IronPdf.ChromePdfRenderOptions()
{
TextHeader = new IronPdf.TextHeaderFooter()
{
CenterText = "Invoice",
DrawDividerLine = false,
Font = FontTypes.Arial,
FontSize = 12
},
TextFooter = new IronPdf.TextHeaderFooter()
{
LeftText = "{date} - {time}",
RightText = "Page {page} of {total-pages}",
Font = IronSoftware.Drawing.FontTypes.Arial,
FontSize = 12,
},
};
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf", AspxToPdfOptions);
}
}
}
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace AspxToPdfTutorial
Partial Public Class Invoice
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim AspxToPdfOptions = New IronPdf.ChromePdfRenderOptions() With {
.TextHeader = New IronPdf.TextHeaderFooter() With {
.CenterText = "Invoice",
.DrawDividerLine = False,
.Font = FontTypes.Arial,
.FontSize = 12
},
.TextFooter = New IronPdf.TextHeaderFooter() With {
.LeftText = "{date} - {time}",
.RightText = "Page {page} of {total-pages}",
.Font = IronSoftware.Drawing.FontTypes.Arial,
.FontSize = 12
}
}
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf", AspxToPdfOptions)
End Sub
End Class
End Namespace
另外,我们还可以使用 HtmlHeaderFooter 类生成 HTML 页眉和页脚,该类还支持 CSS、图像和超链接。
:path=/static-assets/pdf/content-code-examples/tutorials/aspx-to-pdf-5.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspxToPdfTutorial
{
public partial class Invoice : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var AspxToPdfOptions = new IronPdf.ChromePdfRenderOptions()
{
MarginTop = 50, // make sufficiant space for an HTML header
HtmlHeader = new IronPdf.HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>"
}
};
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "MyDocument.pdf", AspxToPdfOptions);
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace AspxToPdfTutorial
Partial Public Class Invoice
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim AspxToPdfOptions = New IronPdf.ChromePdfRenderOptions() With {
.MarginTop = 50,
.HtmlHeader = New IronPdf.HtmlHeaderFooter() With {.HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>"}
}
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "MyDocument.pdf", AspxToPdfOptions)
End Sub
End Class
End Namespace
在我们的示例中,我们可以使用占位符将动态文本或HTML“合并”到页眉/页脚中:
-
{page} 用于 PDF 的当前页码
-
{total-pages} 作为 PDF 内的总页数
-
{date} 以服务器系统环境适当的格式表示今天的日期
-
{time} 表示使用24小时制的时间(小时:秒)
-
{html-title} 插入 ASPX 网页表单头标签中的标题
- {pdf-title}用于文档文件名
5. 将 ASPX 文件应用至 PDF 技巧:分页符
相比HTML通常会“流动”成一个长页面,PDF则模拟数字纸张,被分割成固定的页面。 将以下代码添加到您的ASPX页面中,将在.NET生成的PDF中自动创建一个分页符。
:path=/static-assets/pdf/content-code-examples/tutorials/aspx-to-pdf-6.cs
<div style='page-break-after: always;'> </div>
6. 结合异步与多线程以提升性能
IronPDF 是为 .NET Framework 4.0 或 .NET Core 2 及以上版本构建的。 在 Framework 4.5 或更高版本的项目中,可以利用 异步 来提高处理多个文档时的性能。
将Async与多线程CPU和Parallel.ForEach命令结合使用将显著提高批量PDF格式处理的效率。
7. 下载为ASP.NET源代码
本教程的完整ASPX文件到PDF转换器源代码可以作为压缩的Visual Studio Web应用程序项目下载。
下载此教程作为 ASP.NET Visual Studio 项目
免费下载包含了一个C# ASP.NET Web Forms项目的工作代码示例,展示了一个网页如何在应用设置后被渲染为PDF。 我们希望本教程帮助您了解如何将ASPX 文件保存为 PDF。
向前迈进
通常,学习任何编程技术的最佳方式是在您自己的ASP.NET项目中进行实验。 这包括尝试使用IronPDF的ASPX到PDF转换器。
开发人员可能还对 IronPdf.AspxToPdf 类参考感兴趣:
https://ironpdf.com/object-reference/api/IronPdf.AspxToPdf.html
8.观看 ASPX 转 PDF 视频教程
教程快速访问
Download this Tutorial as Source Code
The full ASPX File to PDF Converter Source Code for this tutorial is available as a zipped Visual Studio Web Application project. 免费下载包含适用于 C# ASP.NET Web Forms 项目的工作代码示例,展示了应用设置后,将网页呈现为 PDF 的过程。 下载
在 GitHub 上探索此教程
此 C# ASPX-To-PDF 项目的代码在 GitHub 上以 C# 和 VB.NET 格式作为 ASP.NET 网站项目提供。请随意在 Github 上 fork 我们,以便更好地使用 IronPDF。如有需要,将此分享给那些可能会问“如何将 ASPX 转换为 PDF?”的人。
C# ASPX 转 PDF 网站项目 高级 ASP.NET 页面到 PDF 的 C# 示例,用于创建 PDF ASP.NET PDF 示例:使用 VB.NET 创建 PDF下载 C# PDF 快速入门指南
为了使您在.NET应用程序中开发 PDF 更容易,我们将快速入门指南编译成 PDF 文档。这份 "小抄 "提供了在 C# 和 VB.NET 中快速访问生成和编辑 PDF 的常用函数和示例,有助于节省您在.NET 项目中开始使用 IronPDF 的时间。
下载