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 套餐的初始價格為 $999,沒有後續費用。 這些許可證包含永久許可證、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,允許快速準確地將富文本文件轉換為 PDF 格式。
我可以從 PDF 文件中提取文本和圖像嗎?
是的,IronPDF 允許您輕鬆地從 PDF 文檔中提取文本和圖像,提供搜索和操作 PDF 中的文本和圖像的功能。
有哪些選項可供創建交互式 PDF 表單?
IronPDF 支持創建和編輯交互式 PDF 表單,允許開發者為 PDF 文檔添加表單字段、圖形、書籤和水印。
如何在 .NET 中合併或拆分 PDF 文檔?
IronPDF 提供合併和拆分 PDF 文檔的功能,允許開發者將多個 PDF 合併為一個或將一個 PDF 分割為多個文檔。
IronPDF 在生產環境中提供哪些許可選項?
IronPDF 提供多種生產環境的許可選項,包括不需持續成本的基本套裝。許可提供永久使用權、30 天退款保證,以及一年的產品更新和支持。
IronPDF 可以在 ASP.NET 網路和 Windows 應用程序中使用嗎?
是的,IronPDF 與 ASP.NET 網路應用程序和傳統 Windows 應用程序兼容,為開發者在不同環境中提供了彈性。
IronPDF 支持密碼保護的 PDF 文件嗎?
IronPDF 可以處理密碼保護的 PDF 文件,允許用戶提供所需的憑證來讀取和編輯這些文檔。
.NET 10:IronPDF是否完全支持針對.NET 10的專案?
是的。IronPDF完全相容於.NET 10——囊括新的執行環境和C#語言增強功能——涵蓋桌面、網頁、微服務和雲端/容器環境。在.NET 10專案中開箱即用,無需變通方案或自訂API。



