將 ASPX 頁面轉換為 PDF 在 ASP.NET 中

This article was translated from English: Does it need improvement?
Translated
View the article in English

本 ASPX 轉 PDF 教學將一步一步指導您如何在 ASP.NET 網頁應用程式中將 ASPX 轉換為 PDF:將 ASPX 頁面儲存為 PDF。

使用者絕不應該在 Google Chrome 中打開帶有 .aspx 檔案擴展名的 ASPX 檔案。 我們要求工程團隊使用.NET程式碼自動將ASPX轉換為PDF! 我們不需要按 CTRL P!有一種基於伺服器的方法可以將 ASPX 網路媒體轉換並儲存為 PDF。

應用設置,包括設置文件行為和名稱、添加頁眉和頁腳、更改打印選項、添加分頁符、結合異步和多線程等。


概述

如何將 ASPX 檔案轉換成 PDF

ASP.NET 的 Microsoft Web Form 應用程式常用於開發複雜的網站、線上銀行、內部網和會計系統。 ASP.NET (ASPX) 網站的一個常見功能是生成動態的 PDF 檔案,如發票、票券或管理報告,以供用戶以 PDF 格式下載。

本教程介紹如何使用IronPDF軟體元件將任何ASP.NET網頁表單轉換為PDF(ASP.NET to PDF)。 HTML 通常呈現為網頁,將用於渲染為 PDF,以便在網頁瀏覽器中下載或查看。 附帶的原始專案將向您展示如何在ASP.NET中使用C#將網頁轉換為PDF。

當使用IronPDF及其AspxToPdf工具類渲染網頁時,我們實現了HTML到PDF(將ASPX轉換為PDF)的轉換。


第一步

立即在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

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 類別檔案的頂部加入此聲明:


using IronPdf;

教學課程

將ASP.NET網頁轉換為PDF

我們從一個正常的ASPX「Web Form」開始,它渲染為HTML。 我們稍後將 ASPX 頁面轉換為 PDF 文件格式。

在附加的示例源代码中,我们渲染了一个商业发票“Invoice.aspx”,一个简单的HTML商业发票作为ASP.NET页面渲染。

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)
$vbLabelText   $csharpLabel

這就是所需的全部內容; 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
$vbLabelText   $csharpLabel

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)
$vbLabelText   $csharpLabel

可用的PDF渲染選項包括:

  • ApplyMarginToHeaderAndFooter。 將邊距選項應用於HTML頁首和頁尾。 預設值為 false,使 HTML 頁首和頁尾的邊界為 0。 僅支持 ChromeRender。
  • CreatePdfFormsFromHtml。 將 ASPX 表單元素轉換為可編輯的 PDF 表單。
  • CssMediaType 啟用 Media="screen" 或 "print" 用於 CSS 樣式和 CSS3 樣式表。
  • CustomCssUrl 允許通過URL將自定義CSS樣式表應用於HTML。
  • EnableJavaScript。 在ASPX頁面中啟用JavaScript、jQuery甚至Json代碼。 可能需要應用渲染延遲。
  • FirstPageNumber頁眉頁腳的起始頁碼。 預設值為1。
  • FitToPaperWidth。 在可能的情況下,將PDF內容縮小至1頁虛擬紙張的寬度。
  • 生成唯一文件識別碼。 如果您希望使用二進位檔案相等性來比較PDF,例如用於單元測試,請設置為false。
  • 灰階。 輸出灰度PDF,使用灰階而非全彩。
  • HtmlHeader 設置每個 PDF 頁面的標題內容,可以使用內容字符串或甚至 HTML。
  • 文字標題. 將每個 PDF 頁面的頁腳內容設為文本。 支援「郵件合併」並自動將網址轉換為超連結。
  • HtmlFooter。 設定每個 PDF 頁面的頁腳內容,可以使用內容字符串或甚至 HTML。
  • 文字頁腳。 為每個 PDF 頁面設置標頭內容為文字。 支援「郵件合併」並自動將網址轉換為超連結。
  • InputEncoding 輸入字符編碼作為字符串。 UTF-8 是 ASP.NET 的預設編碼
  • MarginBottom。 PDF 下邊界紙張邊距(以毫米為單位)。 設為零則為無邊框的pdf。
  • MarginLeft。 左側 PDF 紙張邊距,以毫米為單位。 設為零則為無邊框的pdf。
  • MarginRight。 正確的PDF紙張邊距,單位為毫米。 設為零則為無邊框的pdf。
  • MarginTop。 頂部PDF紙張邊距(毫米)。 設為零則為無邊框的pdf。
  • PaperOrientation。 PDF 文件的方向。 橫向縱向
  • PaperSize 使用 System.Drawing.Printing.PaperKind 設定 PDF 頁面的輸出紙張大小。 或者,開發人員可以使用 SetCustomPaperSize(int width, int height) 方法來創建自定義尺寸。
  • PrintHtmlBackgrounds。 列印 HTML 圖片背景。
  • RenderDelay。 在HTML渲染後等待多少毫秒再進行列印,以便JavaScript或JSON有時間執行。
  • 超時。 渲染超時(秒)。
  • 標題。 PDF文件「標題」的元數據。
  • ViewPortHeight。 定義用於在IronPdf中將HTML渲染為PDF的虛擬屏幕高度。 以像素測量。
  • ViewPortWidth 在IronPdf中定義一個用於將HTML渲染為PDF的虛擬屏幕寬度。 以像素測量。
  • 縮放。 允許開發者放大或縮小 HTML 內容的 % 比例等級。

4. 為 ASPX PDF 添加頁首和頁尾

使用 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
$vbLabelText   $csharpLabel

或者,我們可以使用 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
$vbLabelText   $csharpLabel

在我們的例子中可以看到,我們可以使用占位符將動態文本或 html "合併"到頁首/頁尾中。

  • {page} 代表 PDF 的當前頁碼

  • {total-pages} 作為 PDF 中的總頁數

  • {date} 以適合伺服器系統環境的格式顯示當前日期

  • {time} 以24小時制顯示小時:秒的時間

  • {html-title} 插入 ASPX 網頁表單中 head 標籤的標題

  • {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;'>&nbsp;</div>
HTML

6. 結合非同步和多執行緒以提升性能

IronPDF 是為 .NET Framework 4.0 或 .NET Core 2 及以上版本建立的。 在 Framework 4.5 或以上的專案中,ASYNC 可以用於提高處理多個文件時的性能。

將 Async 與多線程 CPU 及 Paralllel.ForEach 命令結合起來,將顯著提高批量 PDF 格式處理的效能。


7. 下載為 ASP.NET 原始碼

本教程的完整ASPX 文件至 PDF 轉換器源碼可作為壓縮的 Visual Studio 網頁應用程式專案下載。

將此教學下載為 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 教學影片


快速指南

Brand Visual Studio related to 快速指南

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 上分叉我們,以獲得更多使用 IronPDF 的幫助。隨意分享給任何可能會問「如何將 ASPX 轉換為 PDF?」的人。

C# ASPX 到 PDF 網站專案 進階 ASP.NET 頁面到 PDF 的 C# 範例以建立 PDF 在 VB.NET 中用於建立 PDF 的 ASP.NET PDF 範例
Github Icon related to 快速指南
Html To Pdf Icon related to 快速指南

下載 C# PDF 快速入門指南

為了讓您在 .NET 應用程式中開發 PDF 更加輕鬆,我們已將快速入門指南彙編成 PDF 文件。這份「速查表」提供了常用功能和範例,用於在 C# 和 VB.NET 中生成和編輯 PDF,並將幫助您節省使用 IronPDF 在 .NET 專案中入門的時間。

下載

查看 API 參考文件

探索 IronPDF 的 API 參考,概述 IronPDF 所有功能、命名空間、類別、方法欄位和枚舉的詳細資訊。

查看 API 參考文件
Documentation related to 快速指南
軟體產品設計師 @ Team Iron 學習如何將任何 ASP.NET ASPX 頁面透過在 C# 或 VB.NET 中的一行代碼轉換為 PDF 文檔,而不是 HTML。
Iron 團隊的軟體產品設計師

Jacob 是一位專注的軟體工程師,喜歡開發和寫作有關 C# 和 Python 程式語言的內容。

Jacob 目前主要的興趣在於使用 C# 和 ASP.NET MVC 開發大型企業內聯網專案的軟體模組。

他也對 .NET Core 的未來發展感興趣,視其為新平台無關開發技術的基礎,該技術將在一個開放源代碼的框架中提供桌面、伺服器以及網頁應用程式,優先考慮開發者和使用者而非虛擬機器 (VMs)。