跳過到頁腳內容
使用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 能夠穩定地從網絡內容(如 HTML 和網頁)生成高質量的 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 轉換。在需要將不僅僅是 HTML 轉換為 PDF 的情況下,它非常靈活,這是許多其他 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。 使用 URL https://www.nuget.org/packages/IronPdf/,我們將使用 ChromePdfRenderer 類和 IronPDF 對現代網絡標準的支持,只需幾行代碼即可生成高質量的 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,最後保存該 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 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 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 機器人,結合科技與創意的樂趣。