跳過到頁腳內容
使用IRONPDF

如何製作C# PDF轉換器

能夠從各種文件類型建立 PDF 文檔,是製作原始文件易於閱讀的匯總版本的絕佳方式。例如,您可能已經將作品集以網頁形式展示,但希望將其轉換為易於展示的 PDF 文件,以便與潛在雇主分享。 或者,您可能有一些圖表,其中包含大量重要資料,您想將其作為報告的一部分顯示出來。使用 PDF 轉換器,您可以將報告和圖表轉換為 PDF,並將它們合併到一個易於共享的文件中。

今天,我們將學習如何使用 C# 將文件轉換為 PDF 格式。 我們還將了解IronPDF ,這是一個功能強大的 .NET PDF 庫,以及如何使用它進行 PDF 轉換。 無論處理的是 HTML、圖像還是其他文檔,IronPDF 都提供了一種簡單且有效率的方式來以程式設計方式建立和操作 PDF 文件。

為什麼選擇 IronPDF 進行 PDF 轉換?

IronPDF 是一個行業標準庫,可簡化 PDF 的建立和轉換。 它支援將多種格式轉換為 PDF,例如 HTML、圖像,甚至其他 PDF 文件。 主要功能包括

*高保真 PDF 渲染:由於 IronPDF 對現代 Web 標準有著強大的支持,因此能夠持續地從 HTML 和網頁等 Web 內容產生高品質的 PDF。

  • PDF 產生:從圖片、HTML、DOCX 和 Markdown 等文件格式產生 PDF 文件。 *安全功能:*使用 IronPDF,處理 PDF 安全性(例如 PDF 加密和數位簽章)變得輕而易舉。 易於整合:** IronPDF 可與 ASP.NET 和桌面應用程式無縫整合。

安裝 IronPDF。

在使用 IronPDF 將文件轉換為 PDF 文件之前,我們必須先安裝它並將其設定到我們的專案中。 幸好,由於 IronPDF 的實現非常簡便,這項工作可以很快完成。若要安裝 IronPDF 庫,您可以在 NuGet 套件管理器控制台中執行以下命令:

Install-Package IronPdf

或者,您可以使用"管理解決方案的 NuGet 套件"來安裝它:

如何用 C# 寫 PDF 轉換器:圖 1

C# PDF轉換

現在,我們可以看看 IronPDF 如何處理不同文件類型的 PDF 轉換。正如我們之前提到的,IronPDF 是一個功能強大的 PDF 庫,能夠處理多種文件類型的 PDF 轉換。這使得它在需要轉換的場景中非常靈活,而不僅僅是像許多其他 PDF 庫那樣只專注於將 HTML 轉換為 PDF。 使用 IronPDF,您可以在一個地方完成所有轉換,而無需安裝額外的庫。

將 HTML 字串轉換為 PDF

PDF 轉換最常見的場景之一是將 HTML 內容轉換為 PDF 文檔,而 IronPDF 使這一過程無縫銜接。 在這個例子中,我們將轉換一個 HTML 字串,但對於 HTML 文件、HTML 頁面或 HTML 文件,轉換過程類似。 首先,我們將建立一個ChromePdfRenderer實例,將我們的 HTML 內容渲染為 PDF。 然後,使用RenderHtmlAsPdf() ,我們可以將 HTML 內容轉換為 PDF,最後儲存 PDF。

using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // HTML content to convert
        string html = "<h1>This Document was Converted using IronPDF</h1><p>This document was converted from HTML content</p>";

        // Instantiate the ChromePdfRenderer class
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Render the HTML content into a PDF document
        PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

        // Save the rendered PDF document to a file
        pdf.SaveAs("html-to-pdf.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // HTML content to convert
        string html = "<h1>This Document was Converted using IronPDF</h1><p>This document was converted from HTML content</p>";

        // Instantiate the ChromePdfRenderer class
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Render the HTML content into a PDF document
        PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

        // Save the rendered PDF document to a file
        pdf.SaveAs("html-to-pdf.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' HTML content to convert
		Dim html As String = "<h1>This Document was Converted using IronPDF</h1><p>This document was converted from HTML content</p>"

		' Instantiate the ChromePdfRenderer class
		Dim renderer As New ChromePdfRenderer()

		' Render the HTML content into a PDF document
		Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

		' Save the rendered PDF document to a file
		pdf.SaveAs("html-to-pdf.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

輸出

如何製作 C# PDF 轉換器:圖 2

URL 至 PDF

另一種常見的 PDF 轉換方法是將 URL 轉換為 PDF。 這樣,我們就可以將整個網頁擷取為像素級精確的 PDF 檔案。 使用 URLhttps://www.nuget.org/packages/IronPdf/我們將利用ChromePdfRenderer類別和 IronPDF 對現代 Web 標準的支援,只需幾行程式碼即可產生高品質的 PDF。

using System;
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Instantiate the ChromePdfRenderer
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Render the URL content into a PDF
        PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/");

        // Save the PDF to a file
        pdf.SaveAs("url-to-pdf.pdf");
    }
}
using System;
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Instantiate the ChromePdfRenderer
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Render the URL content into a PDF
        PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/");

        // Save the PDF to a file
        pdf.SaveAs("url-to-pdf.pdf");
    }
}
Imports System
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Instantiate the ChromePdfRenderer
		Dim renderer As New ChromePdfRenderer()

		' Render the URL content into a PDF
		Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/")

		' Save the PDF to a file
		pdf.SaveAs("url-to-pdf.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

輸出

如何製作 C# PDF 轉換器:圖 3

將圖像轉換為 PDF

IronPDF 也支援將 PNG、JPG 和 GIF 等影像檔案類型轉換為 PDF。 這是將多張圖片合併到一個文件中的好方法。 在我們的範例中,我們將選取三張不同的影像,每張影像的格式都不同,並將它們轉換為新的 PDF 文件,然後再將它們合併成一個 PDF 文件,以示範如何使用此功能將多張影像轉換為同一個 PDF 文件。 ImageToPdfConverter類別用於執行影像轉換。

using System.Collections.Generic;
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Define image file paths
        string jpgFile = "image-jpg.jpg";
        string pngFile = "image-png.png";
        string gifFile = "image-gif.gif";

        // Convert each image to a separate PDF document
        PdfDocument pdfA = ImageToPdfConverter.ImageToPdf(gifFile);
        PdfDocument pdfB = ImageToPdfConverter.ImageToPdf(pngFile);
        PdfDocument pdfC = ImageToPdfConverter.ImageToPdf(jpgFile);

        // Combine all the PDFs into a single document
        List<PdfDocument> pdfFiles = new List<PdfDocument> { pdfA, pdfB, pdfC };
        PdfDocument mergedPdf = PdfDocument.Merge(pdfFiles);

        // Save the merged PDF document to a file
        mergedPdf.SaveAs("images-to-pdf.pdf");
    }
}
using System.Collections.Generic;
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Define image file paths
        string jpgFile = "image-jpg.jpg";
        string pngFile = "image-png.png";
        string gifFile = "image-gif.gif";

        // Convert each image to a separate PDF document
        PdfDocument pdfA = ImageToPdfConverter.ImageToPdf(gifFile);
        PdfDocument pdfB = ImageToPdfConverter.ImageToPdf(pngFile);
        PdfDocument pdfC = ImageToPdfConverter.ImageToPdf(jpgFile);

        // Combine all the PDFs into a single document
        List<PdfDocument> pdfFiles = new List<PdfDocument> { pdfA, pdfB, pdfC };
        PdfDocument mergedPdf = PdfDocument.Merge(pdfFiles);

        // Save the merged PDF document to a file
        mergedPdf.SaveAs("images-to-pdf.pdf");
    }
}
Imports System.Collections.Generic
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Define image file paths
		Dim jpgFile As String = "image-jpg.jpg"
		Dim pngFile As String = "image-png.png"
		Dim gifFile As String = "image-gif.gif"

		' Convert each image to a separate PDF document
		Dim pdfA As PdfDocument = ImageToPdfConverter.ImageToPdf(gifFile)
		Dim pdfB As PdfDocument = ImageToPdfConverter.ImageToPdf(pngFile)
		Dim pdfC As PdfDocument = ImageToPdfConverter.ImageToPdf(jpgFile)

		' Combine all the PDFs into a single document
		Dim pdfFiles As New List(Of PdfDocument) From {pdfA, pdfB, pdfC}
		Dim mergedPdf As PdfDocument = PdfDocument.Merge(pdfFiles)

		' Save the merged PDF document to a file
		mergedPdf.SaveAs("images-to-pdf.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

輸出

如何製作 C# PDF 轉換器:圖 4

將 DOCX 檔案轉換為 PDF

IronPDF 只需幾行程式碼即可無縫處理 DOCX 到 PDF 的轉換。 首先,我們將建立一個新的DocxToPdfRenderer類別實例,然後使用該實例透過RenderDocxAsPdf()轉換 DOCX 文件,最後儲存 PDF。

using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Instantiate the DocxToPdfRenderer 
        DocxToPdfRenderer renderer = new DocxToPdfRenderer();

        // Convert the DOCX file to a PDF
        PdfDocument pdf = renderer.RenderDocxAsPdf("Meeting notes.docx");

        // Save the converted PDF document
        pdf.SaveAs("docx-to-pdf.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Instantiate the DocxToPdfRenderer 
        DocxToPdfRenderer renderer = new DocxToPdfRenderer();

        // Convert the DOCX file to a PDF
        PdfDocument pdf = renderer.RenderDocxAsPdf("Meeting notes.docx");

        // Save the converted PDF document
        pdf.SaveAs("docx-to-pdf.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Instantiate the DocxToPdfRenderer 
		Dim renderer As New DocxToPdfRenderer()

		' Convert the DOCX file to a PDF
		Dim pdf As PdfDocument = renderer.RenderDocxAsPdf("Meeting notes.docx")

		' Save the converted PDF document
		pdf.SaveAs("docx-to-pdf.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

如何製作 C# PDF 轉換器:圖 5

結論

在當今的數位環境中,將文件轉換為 PDF 等通用格式對於確保一致性、便攜性和專業展示至關重要。 IronPDF 將強大的 C# PDF 轉換器功能整合到您的應用程式中,不僅高效,而且非常靈活。

在本教程中,我們示範了將 HTML 檔案、網頁、圖像、URL 甚至 DOCX 檔案轉換為精美專業的 PDF 是多麼容易。 但這只是冰山一角。除了基本的轉換功能外, IronPDF還提供一系列其他功能,例如:

將多個PDF文件合併成一個文件。 *新增密碼保護和數位簽章等安全功能。

  • 從現有 PDF 中提取文字和圖像。 *產生具有即時資料的動態 PDF ,使其成為產生發票、報告和憑證的理想選擇。

準備好將您的文件處理提升到一個新的水平了嗎? 立即開始體驗 IronPDF 的免費試用版,釋放 C# 專案中 PDF 操作的全部潛力。

若要進一步探索進階功能、最佳化和實際範例,請造訪 IronPDF 官方文檔,並加入開發者社區,共同改變現代應用程式中處理 PDF 的方式。

常見問題解答

如何在 C# 中將 HTML 內容轉換為 PDF?

您可以使用 IronPDF 庫的 ChromePdfRenderer 類將 HTML 內容轉換為 PDF。RenderHtmlAsPdf() 方法允許您無縫地將 HTML 字串渲染為 PDF 文件。

在 C# 中將 URL 轉換為 PDF 的最佳方法是什麼?

IronPDF 提供了一個簡單的方法來使用 ChromePdfRenderer 類將 URL 轉換為 PDF。此功能可捕獲整個網頁並生成像素精確的 PDF。

如何使用 C# 將圖像轉換為 PDF?

使用 IronPDF,您可以使用 ImageToPdfConverter 類將 PNG、JPG 和 GIF 等圖像文件轉換為 PDF。這允許輕鬆合併多個圖像為一個 PDF 文檔。

我可以在 C# 中將 DOCX 文件轉換為 PDF 嗎?

是的,您可以使用 IronPDF 的 DocxToPdfRenderer 類將 DOCX 文件轉換為 PDF。RenderDocxAsPdf() 方法有助於創建並保存 DOCX 內容為 PDF。

IronPDF 的 PDF 轉換關鍵功能有哪些?

IronPDF 提供了高保真渲染、ASP.NET 集成、密碼保護、數字簽名以及合併和提取 PDF 內容的功能。

有沒有辦法使用 C# 合併多個 PDF?

IronPDF 允許您將多個 PDF 合併為一個文檔。這可以通過庫提供的 PDF 合併功能來完成。

如何向我的 PDF 文件添加安全保障?

IronPDF 提供例如密碼保護和數字簽名的安全功能,確保您的 PDF 文件的保密性和真偽性。

有哪些資源可用於進一步了解 IronPDF?

您可以在 IronPDF 官方網站上找到豐富的資源和文檔,其中包括教程、示例和詳細的 API 參考指南。

IronPDF 是否與 .NET 10 完全相容?

是的 — IronPDF旨在支持.NET 10(以及.NET 9、8、7、6、Core、Standard和Framework)。您可以通過NuGet安裝,使用其完整的HTML、URL、圖像、DOCX轉換、編輯、簽名和部署功能,無需特殊解決方案。
• 官方兼容性列表中包含.NET 10作為支持平台之一。

我可以在.NET 10中用IronPDF的async/await進行非阻塞PDF生成嗎?

當然可以。IronPDF完全支持.NET 10中的async/await,允許非阻塞操作,例如渲染PDF(如RenderHtmlAsPdfAsync())、異步保存文件,並流暢地集成到響應式或基於服務的應用程序中。這確保了在現代.NET 10開發環境中的更佳性能和可擴展性。

Curtis Chau
技術作家

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

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

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我