PDF轉換器.NET(開發者教程)
1.0 引言
Adobe 的可攜式文件格式 (PDF) 被廣泛用於文件檢視和交換。 開發人員經常需要創建 PDF 文件以滿足客戶需求,而現代庫簡化了這一過程。 為專案選擇庫時,需要考慮建置、讀取和轉換功能等特性。
2.0 IronPDF 功能
IronPDF 是一個功能強大的庫,用於建立、讀取和編輯 PDF 文檔,並能夠使用 Chrome 引擎將 HTML 轉換為 PDF。它支援多種 Web 元件,可用於 ASP.NET Web 應用程式和傳統的 Windows 應用程式。 該程式庫允許使用 HTML5、JavaScript、CSS 和圖像創建視覺上吸引人的 PDF,並包含一個強大的 HTML 到 PDF 轉換器。
- IronPDF 庫支援多種 PDF 生成輸入,包括圖像檔案轉 PDF 、 HTML5 轉 PDF 、 ASPX 轉 PDF以及Razor/MVC 視圖轉換。
- 該庫具有創建互動式 PDF 文件、編輯和發送互動式表單、分割 PDF 文件、提取文字和圖像、文字搜尋以及將 PDF 頁面柵格化為圖像的工具。
- 本程式庫允許使用 URL 作為 PDF 文件的基礎,並支援使用者代理程式、代理伺服器、cookie、HTTP 標頭和表單變量,以便在 HTML 登入表單後面進行身份驗證。
- IronPDF 允許使用者透過提供使用者名稱和密碼來存取受密碼保護的 PDF 檔案。
- 該程式庫支援讀取和編輯現有的 PDF 文件。
- 使用者可以使用 IronPDF 為 PDF 文件添加文字、圖形、整理書籤、浮水印、自訂頁首和頁腳,以及從 PDF 文件中提取圖像。
- IronPDF 允許在新文件或現有 PDF 文件中分割和合併頁面,並且可以從文字產生 PDF 對象,而無需 Adobe Acrobat Reader。
- 支援 CSS 媒體檔案和CSS 檔案到 PDF 的轉換。
- 使用者可以使用 IronPDF新增新的可填寫 PDF 表單或完成現有的表單。
- 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")RenderUrlAsPdf方法可用於快速將 URL 轉換為 PDF 文件。 只需提供 URL 和所需的儲存位置,IronPDF 就會根據以上資訊產生 PDF 檔案。 這樣只需幾行程式碼即可輕鬆將網頁轉換為 PDF 文件。
! PDF Converter .NET(開發者教學),圖 1:從 Google URL 產生的 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")此程式碼片段示範如何使用RenderHtmlAsPdf函數將 HTML 文字轉換為 PDF。 將 HTML 轉換為字串的函數可以接受所需的任意長度的 HTML 程式碼。 然後,可以使用SaveAs功能快速輕鬆地儲存文檔,從而完成該過程。
! PDF Converter .NET(開發者教學),圖 2:從 HTML 字串輸出的 PDF 文件 從 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")HTML程式碼如下:
<p style="color:red">Hello world</p><p style="color:red">Hello world</p>! PDF Converter .NET(開發者教學),圖 3:從 HTML 檔案輸出的 PDF 文件 從 HTML 文件輸出的 PDF 文件
6.0 從富文本文件建立 PDF 文件
IronPDF API 庫還允許從富文本檔案 (RTF) 快速建立 PDF。 以下範例程式碼可用於將任意數量的 RTF 檔案轉換為單一 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")! PDF Converter .NET(開發者教學),圖 4:從 RTF 檔案輸出的 PDF 文件 從 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")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")! PDF Converter .NET(開發者教學),圖 5:Markdown 文件的輸出 PDF 文件 從 Markdown 文件輸出的 PDF 文件
如上圖所示,原始文件在左側,轉換後的 PDF 文件在右側。 下面提供的程式碼可以將 MD 字串轉換為 PDF。 有關使用 IronPDF 進行 HTML 轉換的更多信息,請訪問此HTML 轉 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");// 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")有關如何使用 IronPDF API 庫的更多信息,請參閱開發者文件IronPDF 文檔資源。
8.0 結論
IronPDF 庫提供免費的開發許可,根據開發人員的需求,還可以購買多種許可用於生產環境。 Lite 套餐的初始價格為$799 ,沒有後續費用。 這些許可證包含永久許可證、30 天退款保證、一年的產品支援和升級,以及 SaaS 和 OEM 的重新分發可能性。 這些都是一次性購買,可用於開發、建造和製作。 IronPDF 還提供額外的限時免費許可證和用於防止重新分發的免費許可證。 有關 IronPDF 定價和許可資訊的完整概述,請造訪IronPDF 許可資訊頁面。
常見問題解答
如何在 .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 檔案,可快速且精確地將 Rich Text Files 轉換為 PDF 格式。
我可以從 PDF 檔案中萃取文字和影像嗎?
是的,IronPDF 可讓您輕鬆地從 PDF 文件中提取文字和影像,提供搜尋和操作 PDF 內文字和影像的功能。
建立互動式 PDF 表單有哪些選項?
IronPdf 支援互動式 PDF 表單的建立與編輯,讓開發人員可以在 PDF 文件中加入表單欄位、圖形、書籤和水印。
如何在 .NET 中合併或分割 PDF 文件?
IronPDF 提供合併和分割 PDF 文件的功能,讓開發人員可以將多個 PDF 合併為一個,或將一個 PDF 分割為多個文件。
IronPDF 為生產用途提供哪些授權選項?
IronPDF 為生產用途提供了多種授權選項,其中包括無持續成本的 Lite 套件。授權提供永久使用權、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、微服務和雲/容器環境。IronPDF 開箱即可與 .NET 10 專案搭配使用,無須變通或自訂 API。






