使用IRONPDF 在ASP.NET Core中使用IronPDF創建PDF生成器 Curtis Chau 更新日期:7月 28, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 如果你是 .NET 開發人員,你可能經常面臨從網頁生成 PDF 文件的任務。 所幸在 ASP.NET Core 中,這一過程可以通過 IronPDF PDF 庫變得輕而易舉。 這讓你只需用一行代碼即可製作 PDF。 讓我們深入了解如何使用 IronPDF 創建 PDF 文件。 本教程涵蓋的主題 本教程將涵蓋以下主題: IronPDF 創建 ASP.NET Core Web 應用程序 安裝 IronPDF 庫 使用 NuGet 套件管理器 使用 NuGet 套件管理器控制台 使用 DLL 文件 使用 ASP.NET Core 網絡應用程序創建 PDF 文檔 使用 ASP.NET WebForms(ASPX)創建 PDF 文檔 從 HTML 文件創建 ASP.NET Core 的 PDF 文檔 總結 IronPDF IronPDF .NET 庫允許開發人員在 .NET Core 和 .NET Framework 中輕鬆使用 C#、F# 和 VB.NET 創建 PDF 文檔。 IronPDF 的渲染是一個與桌面版 Google Chrome 完全一致的像素精確複製品。 它處理 PDF 文檔時不需要 Adobe Acrobat。 IronPDF 可用於從 ASP.NET 網頁、HTML 內容、URL 或模型視圖控制器應用程式內創建 PDF 文件。 IronPDF .NET 庫的一些重要功能: 從 HTML、CSS 和 JavaScript 創建 PDF 文檔 生成來自 URL 或網頁的 PDF 文檔 Load URLs with custom-network login credentials, user-agents, proxies, cookies, HTTP headers, and form variables (allowing login behind HTML login forms) 閱讀和填寫 PDF 表單數據 從 PDF 文件中提取圖像和文本 數字簽名 讓我們開始介紹如何使用 IronPDF 庫創建 PDF 文檔。 創建 ASP.NET Core Web 應用程序 此教程假設你安裝了最新版的 Visual Studio。 打開 Visual Studio 創建一個新的 ASP.NET Core Web 應用程序 Web 應用程序 為項目命名(例如 Pdf_Generation) 最新且最穩定的 .NET Framework 版本是 6.0。選擇此版本的框架。 .NET Framework 安裝 IronPDF 庫 要創建 PDF 文檔,第一步是安裝 IronPDF 庫。 你可以使用以下任何方法安裝它。 1. NuGet 套件管理器 要從 NuGet 套件管理器安裝 IronPDF C# .NET Core 庫: 點擊 工具 > NuGet 套件管理器 > 為解決方案管理 NuGet 套件 打開 NuGet 套件管理器。 NuGet 套件管理器 或者,右鍵點擊專案中的解決方案資源管理器,然後點擊管理 NuGet 套件。 NuGet 套件管理器 - 解決方案資源管理器 搜索 IronPDF。 選擇 IronPDF 並點擊安裝。 庫將開始安裝。 NuGet 套件管理器 - 解決方案資源管理器 2. NuGet 套件管理器控制台 Open the NuGet Package Manager by clicking on Tools > NuGet 套件管理器 > Package Manager Console. 在終端中輸入以下命令。 Install-Package IronPdf NuGet 套件管理器 - 解決方案資源管理器 3. 使用 DLL 文件 將 IronPDF 包含到項目的第三種方法是添加 IronPDF 庫中的 DLL 文件。 你可以從這個IronPDF 套件的直接下載頁面下載 DLL 文件。 下載 DLL 壓縮文件並將其解壓縮到指定的文件夾中。 在 Visual Studio 中開啟項目。 在 解決方案資源管理器 中右鍵點擊 引用 向 IronPDF DLL 文件導航。 在 ASP.NET Core Web 應用程序中創建 PDF 文檔 IronPDF 已準備就緒,現在在 ASP.NET Web 應用程序(ASPX)和 ASP.NET Core Web 應用程序中創建 PDF。 有多種方法創建 PDF 文檔。 讓我們來看看下面的一些代碼示例。 1. 使用 ASP.NET WebForms(ASPX)創建 PDF 本節將展示如何從 ASP.NET WebForms 生成 PDF 文件,它僅支持 .NET Framework 版本 4。因此,這需要從正式的 ASPX NuGet 頁面安裝 IronPdf.Extensions.ASPX。 因為 ASPX 被 MVC 模型取代,它不適用於 .NET Core。 打開希望轉換為 PDF 文檔的 ASPX 網頁的源文件,在本例中,創建一個新的 Default.aspx 頁面。 NuGet 套件管理器 - 解決方案資源管理器 打開 Default.aspx.cs 文件,並在其頂部添加 IronPDF 命名空間。 using IronPdf; using IronPdf; Imports IronPdf $vbLabelText $csharpLabel 接下來,在 Page_Load() 函數中寫下以下代碼行: // This code renders the current ASPX page as a PDF file and displays it in the browser. AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser); // This code renders the current ASPX page as a PDF file and displays it in the browser. AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser); ' This code renders the current ASPX page as a PDF file and displays it in the browser. AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser) $vbLabelText $csharpLabel 只需要一行代碼,就能從 ASP.NET 網頁創建新的 PDF 文檔。 The RenderThisPageAsPdf method is used within the AspxToPdf class to convert the ASPX page into a PDF file. 當你運行項目時,網頁的 PDF 將顯示在瀏覽器中。 這是在伺服器端完成的。 上面代碼僅顯示 PDF 文檔在瀏覽器中。 也可以直接將 PDF 文檔下載到計算機上,方法是在 Page_Load() 函數中添加此代碼行: // This code downloads the generated PDF file of the current ASPX page to the client. AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment); // This code downloads the generated PDF file of the current ASPX page to the client. AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment); ' This code downloads the generated PDF file of the current ASPX page to the client. AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment) $vbLabelText $csharpLabel 此代碼會將 ASPX 網頁的 PDF 文件下載到 .NET 專案目錄下。 输出: ASPX 頁面到 PDF 2. 在 ASP.NET Core 中使用 HTML 文件創建 PDF 本節將展示如何在 ASP.NET Core 中生成 PDF 文件。 IronPDF 能直接將 HTML 文件中的所有內容,包括圖像、CSS、表單等轉換為 PDF 文檔。 添加一個按鈕,點擊時生成 PDF 文件。 將下面的標記添加到任意.cshtml頁面(在這裡使用index.cshtml)。 <div> <form method="post" asp-page="Index" asp-page-handler="GeneratePDF"> <input type="Submit" value="GeneratePDF"/> </form> </div> <div> <form method="post" asp-page="Index" asp-page-handler="GeneratePDF"> <input type="Submit" value="GeneratePDF"/> </form> </div> HTML 在 index.cshtml.cs 文件中創建一個名為 OnPostGeneratePDF 的方法。 此函數將用於將 HTML 渲染為 PDF。 public void OnPostGeneratePDF() { // Placeholder method that will handle PDF generation. } public void OnPostGeneratePDF() { // Placeholder method that will handle PDF generation. } Public Sub OnPostGeneratePDF() ' Placeholder method that will handle PDF generation. End Sub $vbLabelText $csharpLabel 接下來,將新的 HTML 頁面添加到你的 Web 應用程序中。 添加新網頁 在此頁面正文中添加一些文本,例如“從 HTML 頁面生成 PDF 文件”。 最後,將以下代碼添加到 OnPostGeneratePDF 行動方法中。 public void OnPostGeneratePDF() { // Create a new instance of the ChromePdfRenderer class to handle PDF rendering. var renderer = new ChromePdfRenderer(); // Render the HTML file as a PDF by specifying the path and save the resulting PDF file. var pdf = renderer.RenderHtmlFileAsPdf("Pages/htmlpage.html"); // Save the generated PDF document with the specified name. pdf.SaveAs("MyPdf.pdf"); } public void OnPostGeneratePDF() { // Create a new instance of the ChromePdfRenderer class to handle PDF rendering. var renderer = new ChromePdfRenderer(); // Render the HTML file as a PDF by specifying the path and save the resulting PDF file. var pdf = renderer.RenderHtmlFileAsPdf("Pages/htmlpage.html"); // Save the generated PDF document with the specified name. pdf.SaveAs("MyPdf.pdf"); } Public Sub OnPostGeneratePDF() ' Create a new instance of the ChromePdfRenderer class to handle PDF rendering. Dim renderer = New ChromePdfRenderer() ' Render the HTML file as a PDF by specifying the path and save the resulting PDF file. Dim pdf = renderer.RenderHtmlFileAsPdf("Pages/htmlpage.html") ' Save the generated PDF document with the specified name. pdf.SaveAs("MyPdf.pdf") End Sub $vbLabelText $csharpLabel 在上面,RenderHtmlFileAsPdf 函數用於通過指定要轉換的 HTML 文件路徑創建 PDF。 運行項目並點擊“生成 PDF”按鈕。 生成的 PDF 文件將顯示在 ASP.NET Core 專案文件夾中。 輸出: ASP.NET HTML 頁面到 PDF 訪問教學頁面,瞭解如何在 ASP.NET Core 中將 MVC 視圖轉換為 PDF。 摘要 IronPDF .NET Core 是一個完整的處理 PDF 文檔的解決方案。 它提供將不同格式轉換為新 PDF 文檔的能力。 只需幾行代碼即可程序化創建和格式化 PDF 文件。 IronPDF 的主要亮點是 HTML 轉換器,它在後台使用實際符合標準的網頁瀏覽器實例來渲染 HTML 文檔。 HTML 以完全準確的方式渲染,呈矢量格式,適合最高商業打印標準。 輸出是乾淨且高質量的 PDF。 IronPDF 對於需要在其軟件中處理 PDF 文件的開發人員和公司來說是理想的。 商業許可和定價詳情發佈在網站上。 你可以嘗試 IronPDF 庫的免費版本來測試其功能。 免費試用版授權密鑰允許你測試 IronPDF 的全部功能。 此外,特別優惠允許你以兩款產品的價格獲得 Iron Software 的所有五個產品。 有關許可的更多信息可以在這個Iron Software 許可信息頁面上找到。 常見問題解答 如何在 ASP.NET Core 中產生 PDF 文件而不遺失格式? 您可以使用 IronPDF 從 HTML 內容、CSS 和 JavaScript 建立高品質、像素級精準的 PDF 檔案。 IronPDF 以 Google Chrome 為基礎的渲染引擎可確保格式完整保留。 在 ASP.NET Core 中,從 HTML 建立 PDF 的最簡單方法是什麼? 在 ASP.NET Core 中,從 HTML 建立 PDF 的最簡單方法是使用 IronPDF 的RenderHtmlAsPdf方法,該方法可以輕鬆地將 HTML 字串轉換為 PDF 文件。 我可以使用 ASP.NET Core 中的 PDF 函式庫將網頁轉換為 PDF 嗎? 是的,IronPDF 允許您在 ASP.NET Core 中將網頁轉換為 PDF,方法是使用ChromePdfRenderer類別將網頁的 HTML 內容呈現為 PDF 文件。 在 ASP.NET Core 中,有沒有辦法在不使用 NuGet 的情況下安裝 PDF 函式庫? 是的,除了使用 NuGet 套件管理器之外,您還可以直接將 IronPDF DLL 檔案新增至您的專案中,以便在您的 ASP.NET Core 應用程式中安裝該程式庫。 如何確保我的 ASP.NET Core 應用程式支援從 URL 產生 PDF? IronPDF 可以利用其將網頁渲染成 PDF 的功能,從 URL 產生 PDF,從而輕鬆地將任何可存取的 URL 轉換為 PDF 文件。 在 ASP.NET Core 中使用 PDF 函式庫有哪些優點? 在 ASP.NET Core 中使用 IronPDF 具有諸多優勢,包括能夠從各種內容類型生成 PDF、提取文字和圖像、執行數位簽名以及透過可靠的渲染引擎保持格式。 如何在 ASP.NET Core 中產生 PDF 時新增網路憑證? IronPDF 可讓您在 PDF 產生期間新增自訂網路憑證,確保在 ASP.NET Core 應用程式中建立 PDF 時能夠存取受保護的資源。 我可以在不購買的情況下測試庫的 PDF 生成功能嗎? 是的,您可以使用 IronPDF 的免費試用版來測試其全部功能,這樣您就可以在購買前評估其 PDF 生成能力。 是否可以將 PDF 函式庫與 ASP.NET Core 結合使用? 是的,IronPDF 向後相容 ASPX,因此您可以在 ASP.NET WebForms 和 ASP.NET Core 應用程式中使用它來產生 PDF。 IronPDF 與 .NET 10 相容嗎? 是的。 IronPDF 完全相容於 .NET 10,以及 .NET 8 和 .NET 9。它支援所有主流專案類型(網路、桌面、控制台和雲端),無需任何變通或調整。隨著 .NET 新技術的不斷湧現,IronPDF 始終保持面向未來的兼容性。 (請參閱 IronPDF 對 .NET 10 的跨平台支援) Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 發表日期 11月 13, 2025 如何在 C# 中合併兩個 PDF 位元組數組 使用 IronPDF 在 C# 中合併兩個 PDF 位元組數組。學習如何透過簡單的程式碼範例,將來自位元組數組、記憶體流和資料庫的多個 PDF 文件合併在一起。 閱讀更多 發表日期 11月 13, 2025 如何在 ASP.NET MVC 中創建 PDF 檢視器 為 ASP.NET MVC 應用程式構建一個強大的 PDF 檢視器。顯示 PDF 文件,將視圖轉換為 PDF,使用 IronPDF 添加互動功能。 閱讀更多 發表日期 11月 13, 2025 如何建立 .NET HTML 轉 PDF 轉換器 學習如何在.NET中使用IronPDF將HTML轉換為PDF。 閱讀更多 如何使用IronPDF在.NET中創建PDF文件開發者指南:構建Blazor PDF...
發表日期 11月 13, 2025 如何在 C# 中合併兩個 PDF 位元組數組 使用 IronPDF 在 C# 中合併兩個 PDF 位元組數組。學習如何透過簡單的程式碼範例,將來自位元組數組、記憶體流和資料庫的多個 PDF 文件合併在一起。 閱讀更多
發表日期 11月 13, 2025 如何在 ASP.NET MVC 中創建 PDF 檢視器 為 ASP.NET MVC 應用程式構建一個強大的 PDF 檢視器。顯示 PDF 文件,將視圖轉換為 PDF,使用 IronPDF 添加互動功能。 閱讀更多