USING IRONPDF How to view PDF files in ASP.NET using C# and IronPDF Curtis Chau 更新:2025年7月28日 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 大多數人使用專用的桌面應用程式在電腦上開啟 PDF 文件,但軟體工程師也可以使用 IronPDF 透過 C# 程式設計方式建立、檢視、開啟、閱讀和編輯 PDF 內容。 在 ASP.NET 和 C# 中讀取 PDF 檔案時,IronPDF 被證明是一個非常有用的外掛程式。 您可以下載ASP.NET PDF 示範專案。 使用 IronPDF 和 C# 可以快速輕鬆地建立 PDF 文件。 PDF 文件的大部分設計和佈局都可以透過使用現有的 HTML 資源或將這項任務委託給網頁設計人員來完成; 它可以處理將 PDF 生成功能整合到您的應用程式中這項耗時的任務,並自動將準備好的文件轉換為 PDF。 使用 .NET,您可以: 將網頁表單、本地 HTML 頁面和其他網站轉換為 PDF 格式。 允許使用者下載文檔,透過電子郵件與他人分享,或將文檔儲存到雲端。 向客戶開立發票並提供報價; 撰寫報告; 協商合約及其他文件。 使用 ASP.NET、ASP.NET Core、Web Forms、MVC、.NET Framework 和 .NET Core 上的 Web API 以及其他程式語言進行開發。 設定 IronPDF 庫 安裝該庫有兩種方法; 使用 NuGet 套件管理器進行安裝 IronPDF 可以透過 Visual Studio 外掛程式或 NuGet 套件管理器從命令列安裝。開啟控制台,在 Visual Studio 中輸入以下命令: Install-Package IronPdf 直接從網站下載 DLL 文件 或者,您可以直接從網站取得 DLL 檔案。 請記住,在任何使用 IronPDF 的cs類文件的頂部添加以下指令: using IronPdf; using IronPdf; $vbLabelText $csharpLabel 查看IronPDF 詳細功能概述。 IronPDF是一款必備插件。 立即取得並使用 IronPDF NuGet 套件進行試用。 使用 .NET C# 從 HTML 字串建立 PDF 文件 在 C# 中,從 HTML 字串建立 PDF 檔案是一種高效且有益的建立新 PDF 檔案的方法。 ChromePdfRenderer中的RenderHtmlAsPdf函數提供了一種簡單的方法,可以將任何 HTML (HTML5) 字串轉換為 PDF 文檔,這要歸功於 IronPDF DLL 中嵌入的 Google Chromium 引擎版本。 // Create a renderer to convert HTML to PDF var renderer = new ChromePdfRenderer(); // Convert an HTML string to a PDF using var renderedPdf = renderer.RenderHtmlAsPdf("<h1>My First HTML to Pdf</h1>"); // Define the output path for the PDF var outputPath = "My_First_Html.pdf"; // Save the rendered PDF to the specified path renderedPdf.SaveAs(outputPath); // Automatically open the newly created PDF System.Diagnostics.Process.Start(outputPath); // Create a renderer to convert HTML to PDF var renderer = new ChromePdfRenderer(); // Convert an HTML string to a PDF using var renderedPdf = renderer.RenderHtmlAsPdf("<h1>My First HTML to Pdf</h1>"); // Define the output path for the PDF var outputPath = "My_First_Html.pdf"; // Save the rendered PDF to the specified path renderedPdf.SaveAs(outputPath); // Automatically open the newly created PDF System.Diagnostics.Process.Start(outputPath); $vbLabelText $csharpLabel RenderHtmlAsPdf是一個功能強大的工具,它全面支援 CSS、JavaScript 和圖片。 如果這些材質儲存在硬碟上,則可能需要設定RenderHtmlAsPdf的第二個參數。 以下程式碼將產生一個PDF檔案: // Render HTML to PDF with a base path for local assets var renderPdf = renderer.RenderHtmlAsPdf("<img src='image_1.png'/>", @"C:\Newproject"); // Render HTML to PDF with a base path for local assets var renderPdf = renderer.RenderHtmlAsPdf("<img src='image_1.png'/>", @"C:\Newproject"); $vbLabelText $csharpLabel 所有引用的 CSS 樣式表、圖片和 JavaScript 檔案都將相對於BaseUrlPath ,從而可以保持更有序、更符合邏輯的結構。 當然,您也可以使用網路上提供的圖片、樣式表和資源,例如 Web Fonts、Google Fonts,甚至 jQuery。 使用現有 HTML URL 建立 PDF 文檔 可以使用 C# 有效率地將現有 URL 渲染成 PDF; 這也使得團隊能夠將 PDF 設計和後端 PDF 渲染工作分配到各個部門,這是有益的。 以下程式碼示範如何根據 URL 渲染 endeavorcreative.com 頁面: // Create a renderer for converting URLs to PDF var renderer = new ChromePdfRenderer(); // Convert the specified URL to a PDF using var renderedPdf = renderer.RenderUrlAsPdf("https://endeavorcreative.com/setting-up-wordpress-website-from-scratch/"); // Specify the output path for the PDF var outputPath = "Url_pdf.pdf"; // Save the PDF to the specified path renderedPdf.SaveAs(outputPath); // Open the newly created PDF System.Diagnostics.Process.Start(outputPath); // Create a renderer for converting URLs to PDF var renderer = new ChromePdfRenderer(); // Convert the specified URL to a PDF using var renderedPdf = renderer.RenderUrlAsPdf("https://endeavorcreative.com/setting-up-wordpress-website-from-scratch/"); // Specify the output path for the PDF var outputPath = "Url_pdf.pdf"; // Save the PDF to the specified path renderedPdf.SaveAs(outputPath); // Open the newly created PDF System.Diagnostics.Process.Start(outputPath); $vbLabelText $csharpLabel 因此,生成的 PDF 中保留了所有超連結(HTML 連結)甚至 HTML 表單。 從現有 HTML 文件建立 PDF 文檔 本節介紹如何渲染任何本機 HTML 檔案。文件似乎已使用 file:/ 協定打開,所有相關資源(例如 CSS、圖片和 JavaScript 等)都是如此。 // Create a renderer for existing HTML files var renderer = new ChromePdfRenderer(); // Render an HTML file to PDF using var renderedPdf = renderer.RenderHtmlFileAsPdf("Assets/test1.html"); // Specify the output path for the PDF var outputPath = "test1_pdf.pdf"; // Save the PDF to the specified path renderedPdf.SaveAs(outputPath); // Open the newly created PDF System.Diagnostics.Process.Start(outputPath); // Create a renderer for existing HTML files var renderer = new ChromePdfRenderer(); // Render an HTML file to PDF using var renderedPdf = renderer.RenderHtmlFileAsPdf("Assets/test1.html"); // Specify the output path for the PDF var outputPath = "test1_pdf.pdf"; // Save the PDF to the specified path renderedPdf.SaveAs(outputPath); // Open the newly created PDF System.Diagnostics.Process.Start(outputPath); $vbLabelText $csharpLabel 這種策略的優點在於,它允許開發人員在創建 HTML 內容的同時,在瀏覽器中測試其效果。 IronPDF的渲染引擎是基於Chrome瀏覽器建構。 因此,建議使用XML 轉換 PDF 轉換,因為可以使用 XSLT 範本將 XML 內容列印到 PDF。 將 ASP.NET Web 表單轉換為 PDF 文件 只需一行程式碼,即可將 ASP.NET 線上表單轉換為 PDF 格式而不是 HTML 格式。 將這行程式碼放在頁面後台程式碼檔案的Page_Load方法中,使其顯示在頁面上。 ASP.NET Web Forms 應用程式既可以從頭開始創建,也可以從先前的版本開啟。 如果尚未安裝,請安裝 NuGet 套件。 應該使用using關鍵字匯入IronPdf命名空間。 找到要轉換為 PDF 的頁面的後台程式碼。 例如,使用 ASP.NET 的Default.aspx.cs檔案。 RenderThisPageAsPdf是AspxToPdf類別中的一個方法。 using IronPdf; using System; using System.Web.UI; namespace WebApplication7 { public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { // Render the current page as a PDF in the browser AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser); } } } using IronPdf; using System; using System.Web.UI; namespace WebApplication7 { public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { // Render the current page as a PDF in the browser AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser); } } } $vbLabelText $csharpLabel 這需要安裝IronPdf.Extensions.ASPX NuGet 套件。 由於 ASPX 已被 MVC 模型取代,因此它在 .NET Core 中不可用。 應用 HTML 模板 對於企業內部網路和網站開發人員來說,能夠建立範本或"批量產生"PDF文件是一項標準必備技能。 IronPDF 庫並非建立 PDF 文件模板,而是利用現有的、經過充分測試的技術來產生 HTML 模板。 如下所示,當 HTML 範本補充來自查詢字串或資料庫的資料時,就會建立一個動態產生的 PDF 檔案。 例如,考慮一下 C# String 類別及其屬性。 Format 方法適用於基本的"郵件合併"操作。 // Basic HTML String Formatting string formattedString = String.Format("<h1>Hello {0}!</h1>", "World"); // Basic HTML String Formatting string formattedString = String.Format("<h1>Hello {0}!</h1>", "World"); $vbLabelText $csharpLabel 由於 HTML 檔案可能非常龐大,因此通常會使用任意佔位符,例如 [[NAME]],然後用實際資料取代它們。 以下範例將產生三個 PDF 文檔,每個文檔都將為不同的使用者進行自訂。 // Define an HTML template with a placeholder var htmlTemplate = "<p>[[NAME]]</p>"; // Sample data to replace placeholders var names = new[] { "John", "James", "Jenny" }; // Create a new PDF for each name foreach (var name in names) { // Replace placeholder with actual name var htmlInstance = htmlTemplate.Replace("[[NAME]]", name); // Create a renderer and render the HTML as PDF var renderer = new ChromePdfRenderer(); using var pdf = renderer.RenderHtmlAsPdf(htmlInstance); // Save the PDF with the name in the filename pdf.SaveAs($"{name}.pdf"); } // Define an HTML template with a placeholder var htmlTemplate = "<p>[[NAME]]</p>"; // Sample data to replace placeholders var names = new[] { "John", "James", "Jenny" }; // Create a new PDF for each name foreach (var name in names) { // Replace placeholder with actual name var htmlInstance = htmlTemplate.Replace("[[NAME]]", name); // Create a renderer and render the HTML as PDF var renderer = new ChromePdfRenderer(); using var pdf = renderer.RenderHtmlAsPdf(htmlInstance); // Save the PDF with the name in the filename pdf.SaveAs($"{name}.pdf"); } $vbLabelText $csharpLabel ASP.NET MVC 路由:下載此頁面的 PDF 版本 使用 ASP.NET MVC 框架,您可以引導使用者存取 PDF 檔案。 建立新的 ASP.NET MVC 應用程式或將現有 MVC 控制器新增至現有應用程式時,請選擇此選項。 從下拉式功能表中選擇 ASP.NET Web 應用程式 (.NET Framework) > MVC,啟動 Visual Studio 新專案精靈。 或者,您可以開啟一個現有的 MVC 專案。 取代 Controllers 資料夾中 HomeController 檔案中的 Index 方法,或在 Controllers 資料夾中建立新的控制器。 以下是程式碼編寫範例: using IronPdf; using System; using System.Web.Mvc; namespace WebApplication8.Controllers { public class HomeController : Controller { public ActionResult Index() { // Render a URL as PDF and return it in the response using var pdf = HtmlToPdf.StaticRenderUrlAsPdf(new Uri("https://en.wikipedia.org")); return File(pdf.BinaryData, "application/pdf", "Wiki.Pdf"); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } } } using IronPdf; using System; using System.Web.Mvc; namespace WebApplication8.Controllers { public class HomeController : Controller { public ActionResult Index() { // Render a URL as PDF and return it in the response using var pdf = HtmlToPdf.StaticRenderUrlAsPdf(new Uri("https://en.wikipedia.org")); return File(pdf.BinaryData, "application/pdf", "Wiki.Pdf"); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } } } $vbLabelText $csharpLabel 為 PDF 文件新增封面頁 如何在 ASP.NET 中使用 C# 和 IronPDF 查看 PDF 文件,圖 1:為 PDF 文件新增封面頁 IronPDF簡化了PDF文件合併流程。 該技術最常見的應用是在已渲染的 PDF 文件中添加封面或封底。 為此,請準備封面頁,然後使用PdfDocument功能。 若要合併這兩個文檔,請使用Merge PDF Documents Method 。 // Create a renderer and render a PDF from a URL var renderer = new ChromePdfRenderer(); using var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/"); // Merge the cover page with the rendered PDF using var merged = PdfDocument.Merge(new PdfDocument("CoverPage.pdf"), pdf); // Save the merged document merged.SaveAs("Combined.Pdf"); // Create a renderer and render a PDF from a URL var renderer = new ChromePdfRenderer(); using var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/"); // Merge the cover page with the rendered PDF using var merged = PdfDocument.Merge(new PdfDocument("CoverPage.pdf"), pdf); // Save the merged document merged.SaveAs("Combined.Pdf"); $vbLabelText $csharpLabel 為文件添加浮水印 最後,使用 C# 程式碼可以實現為 PDF 文件添加浮水印; 這可以用來在文件的每一頁上添加免責聲明,聲明它是"機密的"或"樣本"。 // Prepare a stamper with HTML content for the watermark HtmlStamper stamper = new HtmlStamper("<h2 style='color:red'>SAMPLE</h2>") { HorizontalOffset = new Length(-3, MeasurementUnit.Inch), VerticalAlignment = VerticalAlignment.Bottom }; // Create a renderer and render a PDF from a URL var renderer = new ChromePdfRenderer(); using var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf"); // Apply the watermark to the PDF pdf.ApplyStamp(stamper); // Save the watermarked PDF pdf.SaveAs(@"C:\PathToWatermarked.pdf"); // Prepare a stamper with HTML content for the watermark HtmlStamper stamper = new HtmlStamper("<h2 style='color:red'>SAMPLE</h2>") { HorizontalOffset = new Length(-3, MeasurementUnit.Inch), VerticalAlignment = VerticalAlignment.Bottom }; // Create a renderer and render a PDF from a URL var renderer = new ChromePdfRenderer(); using var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf"); // Apply the watermark to the PDF pdf.ApplyStamp(stamper); // Save the watermarked PDF pdf.SaveAs(@"C:\PathToWatermarked.pdf"); $vbLabelText $csharpLabel 您可以使用密碼保護您的PDF檔案 當您設定 PDF 文件的密碼屬性時,該文件將被加密,使用者需要提供正確的密碼才能閱讀該文件。 此範例可用於 .NET Core 控制台應用程式。 using IronPdf; namespace ConsoleApp { class Program { static void Main(string[] args) { // Create a renderer and render a PDF from HTML var renderer = new ChromePdfRenderer(); using var pdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>"); // Set password to protect the PDF pdfDocument.Password = "strong!@#pass&^%word"; // Save the secured PDF pdfDocument.SaveAs("secured.pdf"); } } } using IronPdf; namespace ConsoleApp { class Program { static void Main(string[] args) { // Create a renderer and render a PDF from HTML var renderer = new ChromePdfRenderer(); using var pdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>"); // Set password to protect the PDF pdfDocument.Password = "strong!@#pass&^%word"; // Save the secured PDF pdfDocument.SaveAs("secured.pdf"); } } } $vbLabelText $csharpLabel 即使不具備上述優勢,使用 IronPDF 您還可以: 從PDF文件中提取圖像和文本 編輯PDF檔案的HTML內容 增強前景和背景圖像 為PDF檔案新增數位簽名 快速輕鬆地自動填寫您的PDF表單 建立 PDF 文件是一項極具挑戰性的工作; 有些人可能從未接觸過製作出最優秀文件應該採用的基本概念。 因此,IronPDF 非常有用,因為它簡化了 PDF 的創建,從而改善了從 PDF 和 HTML 創建的文件的原始呈現效果。 根據文件和競爭對手分析中提供的資訊:IronPDF 是創建 PDF 時最有效的工具,它使任何人,包括在辦公室或學校工作的人員,都能輕鬆有效地完成任務。 圖 2:如何在 ASP.NET 中使用 C# 和 IronPDF 檢視 PDF 文件 IronPDF 是一個必備的 .NET 函式庫。 立即取得並使用 IronPDF NuGet 套件進行試用。 常見問題解答 如何使用 C# 在 ASP.NET 應用程式中檢視 PDF 檔案? 您可以使用 IronPDF for .NET 在 ASP.NET 應用程式中檢視 PDF 檔案,方法是將 PDF 呈現為圖片或 HTML 元素,以便嵌入網頁中。 在 ASP.NET 中將 HTML 頁面轉換為 PDF 的步驟為何? 要在 ASP.NET 中將 HTML 頁面轉換為 PDF,您可以使用 IronPDF 的 RenderHtmlAsPdf 方法,該方法支援 CSS 和 JavaScript 以進行精確的渲染。 如何用 C# 合併多個 PDF 文件? IronPDF 允許您使用 PdfDocument.Merge 方法合併多個 PDF 文件,該方法可將不同的 PDF 文件合併為單一文件。 是否可以在 ASP.NET 中為 PDF 文件加入水印? 是的,您可以在 ASP.NET 中使用 IronPDF 為 PDF 文件添加水印,方法是使用 HtmlStamper 類來覆蓋自訂的 HTML 內容。 如何使用 C# 在 PDF 檔案上實現密碼保護? 您可以使用 IronPDF 在 PDF 檔案上實現密碼保護,方法是在 PdfDocument 上設定 Password 屬性,以加密檔案。 IronPDF 可以用來將 ASP.NET Web Forms 轉換成 PDF 嗎? 是的,IronPDF 可以使用 RenderThisPageAsPdf 等方法將 ASP.NET Web 表單轉換為 PDF,將整個 Web 表單擷取成 PDF 文件。 IronPDF 在 ASP.NET 中生成 PDF 有哪些優勢? IronPDF 的優點包括使用內建的 Google Chromium 引擎準確呈現 HTML、CSS 和 JavaScript,使其成為在 ASP.NET 中生成 PDF 的靈活工具。 如何在我的 ASP.NET 專案中安裝 IronPDF? 您可以透過 NuGet Package Manager 或直接從 IronPDF 網站下載 DLL 檔案,在您的 ASP.NET 專案中安裝 IronPDF for .NET。 是什麼讓 IronPDF 成為軟體開發人員的寶貴資產? IronPDF 是軟體開發人員的寶貴資產,因為它簡化了複雜的 PDF 生成任務,並能無縫整合到 ASP.NET 應用程式中以進行高效的 PDF 操作。 如何使用 IronPDF 在 C# 中從 URL 創建 PDF? 您可以使用 IronPDF 的 RenderUrlAsPdf 方法在 C# 中從 URL 建立 PDF,此方法會從 URL 抓取內容並轉換成 PDF 文件。 .NET 10 支援:IronPDF 是否與 .NET 10 相容,可在 ASP.NET 中檢視 PDF 檔案? 是 - IronPDF 完全支援 .NET 10,包括使用 ASP.NET 或 ASP.NET Core 的 Web 應用程式。它可以在 .NET 10 專案中無縫運作,無需特殊設定。您仍然可以使用熟悉的方法,例如 RenderUrlAsPdf 或返回 MIME 類型為 application/pdf 的 FileStreamResult 就像在早期的 .NET 版本中一樣。IronPDF 是專為跨平台支援而設計,.NET 10 已明確列在支援的框架中。([ironpdf.com](https://ironpdf.com/?utm_source=openai)) Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 相關文章 更新2026年1月22日 How to Create PDF Documents in .NET with IronPDF: Complete Guide Discover effective methods to create PDF files in C# for developers. Enhance your coding skills and streamline your projects. Read the article now! 閱讀更多 更新2026年1月21日 How to Merge PDF Files in VB.NET: Complete Tutorial Merge PDF VB NET with IronPDF. Learn to combine multiple PDF files into one document using simple VB.NET code. Step-by-step examples included. 閱讀更多 更新2026年1月21日 C# PDFWriter Tutorial: Create PDF Documents in .NET Learn to create PDFs efficiently using C# PDFWriter with this step-by-step guide for developers. Read the article to enhance your skills today! 閱讀更多 Generating PDF Files in C# (2026 Guide)Generating PDFs in C# using IronPDF
更新2026年1月22日 How to Create PDF Documents in .NET with IronPDF: Complete Guide Discover effective methods to create PDF files in C# for developers. Enhance your coding skills and streamline your projects. Read the article now! 閱讀更多
更新2026年1月21日 How to Merge PDF Files in VB.NET: Complete Tutorial Merge PDF VB NET with IronPDF. Learn to combine multiple PDF files into one document using simple VB.NET code. Step-by-step examples included. 閱讀更多
更新2026年1月21日 C# PDFWriter Tutorial: Create PDF Documents in .NET Learn to create PDFs efficiently using C# PDFWriter with this step-by-step guide for developers. Read the article to enhance your skills today! 閱讀更多