using IronPDF 的 AspxToPdf.RenderThisPageAsPdf() 方法將 ASPX 頁面轉換為 PDF,可將任何 ASP.NET 網頁表單即時轉換為可下載的 PDF 文件,同時保留樣式、連結和表單。 無論是生成發票、報告還是文件,此方法都能使用基於 Chrome 的渲染技術確保像素完美的渲染。
本教程將逐步引導您完成 ASPX 到 PDF 的轉換過程。 在ASP.NET網頁應用程式中將ASPX頁面儲存為PDF,完全控制頁眉、頁腳和頁面格式。
Microsoft Web Form Applications for ASP.NET 通常用於開發複雜的網站、網上銀行、內聯網和會計系統。 ASPX 網站的一個常見功能是生成如發票、票或管理報告之類的動態 PDF 文件,供使用者下載。
本教程介紹如何使用 IronPDF 將任何 ASP.NET 網頁表單轉換為 PDF (ASP.NET 到 PDF)。 通常以網頁形式渲染的 HTML 將渲染為 PDF,以供下載或在網頁瀏覽器中查看。 附帶的源專案展示了如何在 ASP.NET 中使用 C# 將網頁轉換為 PDF。
當使用 IronPDF 和其 AspxToPdf Class (/object-reference/api/IronPdf.AspxToPdf.html) 渲染網頁時,我們實現了 HTML 到 PDF 的轉換。 此過程保持所有 CSS 樣式、JavaScript 功能,甚至網路字型在生成的 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)
如何將 ASP.NET 網頁轉換為 PDF?
從一個正常的 ASPX "網頁表單" 開始,它會被渲染為 HTML。 之後將 ASPX 頁面轉換為 PDF 文件格式。
在附加的範例源程式碼中,我們渲染了一個商務發票 "Invoice.aspx",一個簡單的 HTML 商務發票渲染為 ASP.NET 頁面。 對於涉及身份驗證和 Cookie的更複雜情況,IronPDF 提供全面的解決方案。
HTML 頁面包含 CSS3 樣式表,還可能包括圖像和 JavaScript。 IronPDF 支援JavaScript 執行 在渲染之前,確保動態內容準確捕獲。
要將此 ASP.NET 網頁渲染為 PDF 而非 HTML,請開啟 C#(或 VB.NET) 程式碼,並新增到*Page_Load* 事件:
這就是所需的全部; HTML 現已呈現為 PDF。 超連結、樣式表、圖像甚至 HTML 表單都被保留。 這類似於使用者在瀏覽器中將 HTML 列印為 PDF 的輸出。 IronPDF 是基於驅動 Google Chrome 的 Chromium 網頁瀏覽器技術構建的。
完整的 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 的轉換器設定?
存在許多選項可以調整和完善從 ASPX 文件生成 PDF,使用 .NET Web Forms。 這些設定控制從紙張大小到邊距配置的所有內容。
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 中建立分頁。 對於進階的分頁控制,請參閱我們的分頁指南。
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.