跳過到頁腳內容
使用IRONPDF

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 轉換器。

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");
$vbLabelText   $csharpLabel

可使用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");
$vbLabelText   $csharpLabel

此程式碼片段示範如何使用 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");
$vbLabelText   $csharpLabel

HTML程式碼如下:

<p style="color:red">Hello world</p>
<p style="color:red">Hello world</p>
HTML

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");
$vbLabelText   $csharpLabel

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");
$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");
$vbLabelText   $csharpLabel

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");
$vbLabelText   $csharpLabel

有關如何使用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,允許快速準確地將富文本文件轉換為 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。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me