varAspxToPdfOptions = new IronPdf.ChromePdfRenderOptions(){EnableJavaScript = false, //.. many more options available};IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf", AspxToPdfOptions);
var AspxToPdfOptions = new IronPdf.ChromePdfRenderOptions()
{
EnableJavaScript = false,
//.. many more options available
};
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf", AspxToPdfOptions);
DimAspxToPdfOptions = New IronPdf.ChromePdfRenderOptions() With {.EnableJavaScript = False}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)
如何将 ASP.NET 网页转换为 PDF?
从一个普通的 ASPX "Web表单 "开始,该表单渲染为HTML。 之后将 ASPX 页面转换为 PDF 文件格式。
在所附示例源代码中,我们渲染了一个名为"Invoice.aspx"的商业发票,这是一个简单的 HTML 商业发票,以 ASP.NET 页面的形式呈现。 对于涉及 身份验证和 cookies 的更复杂的情况,IronPDF 可提供全面的解决方案。
HTML 页面包含 CSS3 样式表,还可能包含图片和 JavaScript。 IronPDF 支持 渲染前执行 JavaScript,确保准确捕捉动态内容。
这就是全部所需条件; 现在HTML会渲染成PDF。 超链接、样式表、图片甚至 HTML 表单都要保留。 这类似于用户在浏览器中将 HTML 打印成 PDF 的输出结果。 IronPDF 基于 Chromium 网络浏览器技术构建,该技术也是 Google Chrome 浏览器的基础。
完整的 C# 代码如下所示:
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 voidPage_Load(object sender, EventArgs e) {IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser); } }}
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);
}
}
}
ImportsSystemImportsSystem.Collections.GenericImportsSystem.LinqImportsSystem.WebImportsSystem.Web.UIImportsSystem.Web.UI.WebControlsImportsIronPdfNamespaceAspxToPdfTutorialPartialPublic Class InvoiceInheritsSystem.Web.UI.PageProtected Sub Page_Load(ByVal sender AsObject, ByVal e AsEventArgs)IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.InBrowser) End Sub End ClassEndNamespace
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
如何将 ASPX 文件应用到 PDF 转换器设置?
using .NET Web Forms 从 ASPX 文件生成 PDF 时,有许多调整和完善的选择。 这些设置控制着从纸张大小到边距配置的一切。
varAspxToPdfOptions = new IronPdf.ChromePdfRenderOptions(){EnableJavaScript = false, //.. many more options available};IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf", AspxToPdfOptions);
var AspxToPdfOptions = new IronPdf.ChromePdfRenderOptions()
{
EnableJavaScript = false,
//.. many more options available
};
IronPdf.AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment, "Invoice.pdf", AspxToPdfOptions);
DimAspxToPdfOptions = New IronPdf.ChromePdfRenderOptions() With {.EnableJavaScript = False}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渲染选项包括:
CreatePdfFormsFromHtml:将ASPX表单转换为可编辑的PDF表单。 请参见 创建 PDF 表单。
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 voidPage_Load(object sender, EventArgs e) { varAspxToPdfOptions = 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); } }}
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);
}
}
}
ImportsIronSoftware.DrawingImportsSystemImportsSystem.Collections.GenericImportsSystem.LinqImportsSystem.WebImportsSystem.Web.UIImportsSystem.Web.UI.WebControlsNamespaceAspxToPdfTutorialPartialPublic Class InvoiceInheritsSystem.Web.UI.PageProtected Sub Page_Load(ByVal sender AsObject, ByVal e AsEventArgs) DimAspxToPdfOptions = 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 ClassEndNamespace
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
或者,使用支持CSS、图像和超链接的HtmlHeaderFooter类生成HTML页眉和页脚。
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 voidPage_Load(object sender, EventArgs e) { varAspxToPdfOptions = new IronPdf.ChromePdfRenderOptions() {MarginTop = 50, // make sufficiant space for an HTML headerHtmlHeader = 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); } }}
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);
}
}
}
ImportsSystemImportsSystem.Collections.GenericImportsSystem.LinqImportsSystem.WebImportsSystem.Web.UIImportsSystem.Web.UI.WebControlsNamespaceAspxToPdfTutorialPartialPublic Class InvoiceInheritsSystem.Web.UI.PageProtected Sub Page_Load(ByVal sender AsObject, ByVal e AsEventArgs) DimAspxToPdfOptions = 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 ClassEndNamespace
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}:当前页码。
{total-pages}:总页数。
{url}:渲染PDF的网页URL。
{date}:系统格式的今天日期。
{time}:24小时制时间。
{html-title}:来自ASPX头标签的标题。
{pdf-title}:文档文件名。
如何在 PDF 文件中添加分页符?
HTML 通常是长长的一页,而 PDF 则是模拟数字纸张,分成连贯的几页。 将此代码添加到 ASPX 页面后,生成的 PDF 将自动创建分页符。 有关高级分页控制,请参阅我们的分页指南。
IronPDF 支持先进的企业功能,包括用于长期存档的 PDF/A 合规性,以及将 ASPX 页面转换为 PDF 时用于文档验证的数字签名。
我可以从 ASPX 页面生成哪些类型的文档?
IronPDF 通常用于从 ASPX 页面生成动态 PDF 文件,如发票、票据、管理报告、文档和其他业务文档,用户可以在 Web 浏览器中下载或查看。
Is it possible to handle asynchronous and multithreading PDF generation with IronPDF?
Yes, IronPDF supports asynchronous operations and multithreading, enhancing performance during bulk PDF processing, especially in enterprise deployments.
How does IronPDF support complex ASP.NET scenarios like authentication?
IronPDF offers comprehensive solutions to manage complex scenarios including handling authentication and cookies, allowing for accurate and secure PDF generation.
What is the recommended way to install IronPDF for ASPX-to-PDF conversion?
The recommended installation is via NuGet in Visual Studio, but you can also install using the IronPDF DLL for custom setups. For ASPX workflows, ensure the required NuGets, like IronPdf.Extensions.ASPX, are installed.
How can I download a quick-start guide for using IronPDF?
You can download our C# PDF Quickstart guide, which provides a comprehensive introduction to generating and editing PDFs in your .NET applications using IronPDF.