使用IRONPDF PDF轉換器.NET(開發者教程) Curtis Chau 更新日期:6月 22, 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 1.0 介紹 Adobe 的可攜式文檔格式 (PDF) 被廣泛用於文檔查看和交換。 開發人員通常需要創建 PDF 以滿足客戶需求,而現代庫已簡化了此過程。 選擇項目庫時,考慮構建、讀取和轉換功能很重要。 2.0 IronPDF 特點 IronPDF 是一個多功能庫,用於創建、閱讀和編輯 PDF 文檔,具有使用 Chrome 引擎將 HTML 轉換為 PDF 的功能。它支持各種 web 元件,並可用於 ASP.NET Web 應用程序和傳統 Windows 應用程序。 該庫允許使用 HTML5、JavaScript、CSS 和圖像創建視覺上吸引人的 PDF,並包括強大的 HTML-to-PDF 轉換器。 The IronPDF library supports a variety of inputs for PDF production, including image files to PDF conversion, HTML5 to PDF conversion, ASPX to PDF conversion, and Razor/MVC View conversion. The library has tools for creating interactive PDF files, editing and sending interactive forms, dividing PDF files, extracting text and images, text searching, and rasterizing PDF pages into images. 該庫允許使用 URL 作為 PDF 文檔的基礎並支持用戶代理、代理、cookie、HTTP 標頭和表單變量以通過 HTML 登錄表單進行身份驗證。 IronPDF 通過提供用戶名和密碼來存取受密碼保護的 PDF 文件。 該庫支持閱讀和編輯現有的 PDF 文檔。 Users can add text, graphics, organize bookmarks, watermarks, customize headers and footers to PDF documents with IronPDF, and extract images from PDF files. IronPDF 允許在新或現有的 PDF 文檔中分割和合併頁面,並且無需 Adobe Acrobat Reader 就能從文本中生成 PDF 對象。 CSS 媒體文件和CSS 文件轉 PDF被支持。 用戶可以添加新的填充 PDF 表單或用 IronPDF 完成現有表單。 IronPDF 還可以將 PDF 頁面轉換為各種圖像格式,如 JPEG、PNG 等。 3.0 從 URL 創建 PDF 文檔 使用 IronPDF 的內置 Chrome 瀏覽器和 API 庫從網頁生成 PDF 文件很簡單。 只需提供 URL 並使用 IronPDF API 庫將其轉換為 PDF 文件。 文檔轉換可以僅通過幾行代碼快速完成: // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified URL to a PDF var pdf = renderer.RenderUrlAsPdf("https://www.google.co.in/"); // Save the PDF to the specified path pdf.SaveAs("result.pdf"); // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified URL to a PDF var pdf = renderer.RenderUrlAsPdf("https://www.google.co.in/"); // Save the PDF to the specified path pdf.SaveAs("result.pdf"); ' Create a new instance of the ChromePdfRenderer class Dim renderer = New IronPdf.ChromePdfRenderer() ' Convert the specified URL to a PDF Dim pdf = renderer.RenderUrlAsPdf("https://www.google.co.in/") ' Save the PDF to the specified path pdf.SaveAs("result.pdf") $vbLabelText $csharpLabel RenderUrlAsPdf 方法可用於快速將 URL 轉換為 PDF 文檔。 只需提供 URL 和所需的保存位置,IronPDF 就會根據上述信息生成 PDF 文件。 這使得只需幾行代碼就能輕鬆將網頁轉換為 PDF 文檔。 從 Google URL 生成的 PDF 文件 4.0 從 HTML 字串創建 PDF 使用 IronPDF API 庫可以快速將 HTML 字串轉換成 PDF 文件。 以下代碼片段可以將 HTML 字串轉換成文檔,並可將任何 HTML 標籤轉換為 PDF 文件。 // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified HTML string to a PDF var pdf = renderer.RenderHtmlAsPdf("<p>Hello world!!</p>"); // Save the PDF to the specified path pdf.SaveAs("result.pdf"); // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified HTML string to a PDF var pdf = renderer.RenderHtmlAsPdf("<p>Hello world!!</p>"); // Save the PDF to the specified path pdf.SaveAs("result.pdf"); ' Create a new instance of the ChromePdfRenderer class Dim renderer = New IronPdf.ChromePdfRenderer() ' Convert the specified HTML string to a PDF Dim pdf = renderer.RenderHtmlAsPdf("<p>Hello world!!</p>") ' Save the PDF to the specified path pdf.SaveAs("result.pdf") $vbLabelText $csharpLabel 此代碼片段說明瞭如何使用 RenderHtmlAsPdf 函數將 HTML 文本轉換為 PDF。 將 HTML 轉換為字符串的函數允許接受大量的 HTML 代碼。 然後使用 SaveAs 功能快速簡單地完成過程以保存文檔。 從 HTML 字串生成的 PDF 文件 5.0 從 HTML 文件創建 PDF IronPDF API 庫允許快速將 HTML 文件轉換為 PDF 文件。 任何 HTML 標籤都可以使用以下示例代碼轉換成 PDF 文件。 // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified HTML file to a PDF var pdf = renderer.RenderHtmlFileAsPdf("test.html"); // Save the PDF to the specified path pdf.SaveAs("result.pdf"); // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified HTML file to a PDF var pdf = renderer.RenderHtmlFileAsPdf("test.html"); // Save the PDF to the specified path pdf.SaveAs("result.pdf"); ' Create a new instance of the ChromePdfRenderer class Dim renderer = New IronPdf.ChromePdfRenderer() ' Convert the specified HTML file to a PDF Dim pdf = renderer.RenderHtmlFileAsPdf("test.html") ' Save the PDF to the specified path pdf.SaveAs("result.pdf") $vbLabelText $csharpLabel HTML 文本如下: <p style="color:red">Hello world</p> <p style="color:red">Hello world</p> HTML 從 HTML 文件生成的 PDF 文件 6.0 從富文本文件創建 PDF IronPDF API 庫還允許從富文本文件 (RTF) 快速創建 PDF。 轉換 RTFs 為 PDFs 的示例代碼可用於將任意數目 RTFs 轉換為單個 PDF 文件。以下提供了代碼。 // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified RTF file to a PDF var pdf = renderer.RenderRtfFileAsPdf("test.rtf"); // Save the PDF to the specified path pdf.SaveAs("result.pdf"); // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified RTF file to a PDF var pdf = renderer.RenderRtfFileAsPdf("test.rtf"); // Save the PDF to the specified path pdf.SaveAs("result.pdf"); ' Create a new instance of the ChromePdfRenderer class Dim renderer = New IronPdf.ChromePdfRenderer() ' Convert the specified RTF file to a PDF Dim pdf = renderer.RenderRtfFileAsPdf("test.rtf") ' Save the PDF to the specified path pdf.SaveAs("result.pdf") $vbLabelText $csharpLabel 從 RTF 文件生成的 PDF 文件 在上圖中,左側顯示了源文檔,而右側顯示了轉換後的 PDF 文件。 此外,我們可以使用以下提供的代碼利用 RTF 字串創建 PDF。 // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified RTF string to a PDF var pdf = renderer.RenderRtfStringAsPdf("{\\rtf1...}"); // Save the PDF to the specified path pdf.SaveAs("result.pdf"); // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified RTF string to a PDF var pdf = renderer.RenderRtfStringAsPdf("{\\rtf1...}"); // Save the PDF to the specified path pdf.SaveAs("result.pdf"); ' Create a new instance of the ChromePdfRenderer class Dim renderer = New IronPdf.ChromePdfRenderer() ' Convert the specified RTF string to a PDF Dim pdf = renderer.RenderRtfStringAsPdf("{\rtf1...}") ' Save the PDF to the specified path pdf.SaveAs("result.pdf") $vbLabelText $csharpLabel 7.0 從 Markdown 文件創建 PDF 得益於 IronPDF API 模塊,從 Markdown 文件快速生成 PDF 成為可能。 可以使用以下示例代碼將任意數量的 Markdown 文件轉換為 PDF 文件。 // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified Markdown file to a PDF var pdf = renderer.RenderMarkdownFileAsPdf("Markdown.md"); // Save the PDF to the specified path pdf.SaveAs("Markdown_result.pdf"); // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified Markdown file to a PDF var pdf = renderer.RenderMarkdownFileAsPdf("Markdown.md"); // Save the PDF to the specified path pdf.SaveAs("Markdown_result.pdf"); ' Create a new instance of the ChromePdfRenderer class Dim renderer = New IronPdf.ChromePdfRenderer() ' Convert the specified Markdown file to a PDF Dim pdf = renderer.RenderMarkdownFileAsPdf("Markdown.md") ' Save the PDF to the specified path pdf.SaveAs("Markdown_result.pdf") $vbLabelText $csharpLabel 從 Markdown 文件生成的 PDF 文件 如上圖所示,源文檔在左側,轉換後的 PDF 文件位於右側。 下面提供的代碼允許將 MD 字串轉換為 PDF。 關於使用 IronPDF 進行 HTML 轉換的更多信息,請訪問本HTML to PDF Conversion Tutorial。 // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified Markdown string to a PDF var pdf = renderer.RenderMarkdownStringAsPdf("# Hello world\n\nHello world"); // Save the PDF to the specified path pdf.SaveAs("Markdown_result.pdf"); // Create a new instance of the ChromePdfRenderer class var renderer = new IronPdf.ChromePdfRenderer(); // Convert the specified Markdown string to a PDF var pdf = renderer.RenderMarkdownStringAsPdf("# Hello world\n\nHello world"); // Save the PDF to the specified path pdf.SaveAs("Markdown_result.pdf"); Imports Microsoft.VisualBasic ' Create a new instance of the ChromePdfRenderer class Dim renderer = New IronPdf.ChromePdfRenderer() ' Convert the specified Markdown string to a PDF Dim pdf = renderer.RenderMarkdownStringAsPdf("# Hello world" & vbLf & vbLf & "Hello world") ' Save the PDF to the specified path pdf.SaveAs("Markdown_result.pdf") $vbLabelText $csharpLabel 關於如何使用 IronPDF API 庫的更多信息,請參閱開發人員文檔IronPDF Documentation Resource。 8.0 結論 IronPDF 庫為開發提供免費許可證,並根據開發者的需求,有幾種許可證可供購買以供生產環境中使用。 Lite 套餐的初始價格為 $799,且沒有持續成本。 許可證包含永久許可、30 天退款保證、一年產品支持和升級,以及 SaaS 和 OEM 再分發的可能性。 它們是一次性購買,可以用於開發、測試和生產。 IronPDF 還提供附加的限時免費許可和再分發保護的免費許可。 要獲得 IronPDF 的全面定價和許可信息,請訪問IronPDF Licensing Information Page。 常見問題解答 如何在.NET中將HTML轉換為PDF而不遺失格式? 您可以使用 IronPDF 利用 Chrome 引擎將 HTML 文件轉換為 PDF 文件,且不會遺失格式。該程式庫支援 HTML5、JavaScript 和 CSS,確保您的 PDF 檔案保留其原始佈局和設計。 有哪些方法可以將URL轉換為PDF? IronPDF 提供了RenderUrlAsPdf方法,該方法允許使用 ChromePdfRenderer 類別將 URL 直接轉換為 PDF 文件。 有沒有辦法在 C# 中將 Markdown 轉換為 PDF? 是的,IronPDF 可以使用RenderMarkdownFileAsPdf方法將 Markdown 檔案轉換為 PDF。這使得開發人員能夠有效率地將 Markdown 內容轉換為 PDF 格式。 如何使用 .NET 將 RTF 檔案轉換為 PDF 文件? IronPDF 的RenderRtfFileAsPdf方法可以將 RTF 檔案轉換為 PDF,從而實現將富文本檔案快速且準確地轉換為 PDF 格式。 我可以從PDF文件中提取文字和圖像嗎? 是的,IronPDF 可以輕鬆地從 PDF 文件中提取文字和圖像,並提供在 PDF 中搜尋和操作文字和圖像的功能。 建立互動式PDF表單有哪些選項? IronPDF 支援建立和編輯互動式 PDF 表單,允許開發人員在其 PDF 文件中添加表單欄位、圖形、書籤和浮水印。 如何在.NET中合併或分割PDF文件? IronPDF 提供合併和分割 PDF 文件的功能,讓開發人員將多個 PDF 合併為一個,或將單一 PDF 分割為多個文件。 IronPDF 為生產環境提供哪些授權選項? IronPDF 提供多種生產用途的許可選項,包括無需持續付費的精簡版套餐。許可證提供永久使用權、30 天退款保證以及一年的產品更新和支援。 IronPDF 能否用於 ASP.NET Web 和 Windows 應用程式? 是的,IronPDF 既相容於 ASP.NET Web 應用程序,也相容於傳統的 Windows 應用程序,為在不同環境下工作的開發人員提供了靈活性。 IronPDF是否支援受密碼保護的PDF文件? IronPDF 可以處理受密碼保護的 PDF 文件,讓使用者提供必要的憑證來讀取和編輯這些文件。 .NET 10:IronPDF 是否完全支援 .NET 10 的專案? 是的。 IronPDF 完全相容於 .NET 10,包括新增的執行階段和 C# 語言增強功能,支援桌面、Web、微服務和雲端/容器環境。它無需任何變通方案或自訂 API,即可在 .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。 閱讀更多 ASP PDF查看器(開發者教程)如何在C#中裁剪PDF文件
發表日期 11月 13, 2025 如何在 C# 中合併兩個 PDF 位元組數組 使用 IronPDF 在 C# 中合併兩個 PDF 位元組數組。學習如何透過簡單的程式碼範例,將來自位元組數組、記憶體流和資料庫的多個 PDF 文件合併在一起。 閱讀更多
發表日期 11月 13, 2025 如何在 ASP.NET MVC 中創建 PDF 檢視器 為 ASP.NET MVC 應用程式構建一個強大的 PDF 檢視器。顯示 PDF 文件,將視圖轉換為 PDF,使用 IronPDF 添加互動功能。 閱讀更多