產品比較

HTML 到 PDF 轉換器的 C# 方案選擇

發佈 2024年9月29日
分享:

在現代數位世界中,將文件從一種格式轉換為另一種格式是一項常見的必要性。 其中一個常見的需求是將 HTML 文件轉換為 PDF,特別是在處理從網路內容生成報告、發票或任何可列印文件時。

GroupDocs 提供了一個強大的 .NET 文件轉換 API,專為在 C#、ASP.NET、VB.NET 和 .NET Core 應用程式中無縫轉換 HTML 到 PDF 格式而設計。 它可確保在整個轉換過程中精確的格式和佈局保持,同時將編碼工作量降到最低。

另一方面, IronPDF 是一個專門為在C#應用程式中處理PDF生成和操作任務而設計的.NET庫。 它簡化了將 HTML 內容轉換為 PDF 文件的過程,提供豐富的自訂選項和可靠的性能。 在本文中,我們將探索這兩個庫並進行比較。

如何使用 GroupDocs 和 IronPDF 將 HTML 轉換為 PDF

  1. 建立一個新專案將 HTML 轉換為 PDF。

  2. 將 GroupDocs 安裝到新創建的項目中

  3. 使用GroupDocs.Conversion庫將HTML轉換為PDF文件。

  4. 將 IronPDF 安裝到專案中。

  5. 使用 IronPDF 庫將 HTML 轉換為 PDF 文件。

GroupDocs.Conversion .NET 庫概述

GroupDocs替代方案,用於C#中的HTML轉PDF轉換器:圖1

GroupDocs.Conversion 提供了一個線上工具,讓使用者可以輕鬆地將 HTML 文件轉換成 PDF。 此工具可從任何具有網路連線的設備訪問,使其具有高度的多功能性且使用者友好。 該轉換器支持多種平台,包括Windows、macOS、Linux、Android和iOS。

它提供了一個強大的 API,用於 HTML 到 PDF 的轉換,設計上既簡單又靈活,足以應對各種情況。 現在讓我們來看看 GroupDocs.Conversion .NET NuGet 庫的一些重要功能。

主要功能

廣泛的格式支援

  1. 文件: 在 PDF、DOCX、PPTX、XLSX 等多種格式之間轉換。

  2. 圖片: 支援 BMP、JPEG、PNG、GIF 及 TIFF 格式。

  3. CAD 和 GIS: 將 CAD 文件如 DWG、DXF 及 GIS 格式如 SHP 和 KML 進行轉換。

  4. 音頻和視頻: 轉換音頻文件 (MP3,WAV) 和视频文件 (MP4, AVI).

  5. 其他: 包括對電子書的支援 (EPUB, MOBI),網頁格式 (HTML, MHTML)和檔案 (ZIP, TAR).

    可自訂的轉換選項

  6. 部分轉換: 轉換整個文件或特定頁面範圍。

  7. 浮水印: 在轉換文檔時添加浮水印。

  8. 外觀自定義: 自定義轉換後文件的外觀,如設置圖像的解析度或指定文件的輸出格式。

    靈活的加載選項

  9. 本地和遠端加載: 從本地磁碟、資料流、URL 或雲端儲存服務(如 Amazon S3 和 Azure Blob)加載文檔。

  10. 受密碼保護的文件: 載入和轉換受密碼保護的文件。

    跨平台支持

  11. 操作系統: 與 Windows、Linux 和 macOS 兼容。

  12. 開發環境: 支援在 C#、F# 和 VB.NET 中的開發。

    自給自足的程式庫

  13. 無第三方依賴: 該庫不依賴於任何第三方軟體,例如 Microsoft Word 或 OpenOffice,這使其成為一個獨立的解決方案。

IronPDF .NET 介紹

GroupDocs 的 HTML 到 PDF 轉換器在 C# 中的替代方案:圖 2

IronPDF 是一個專門為在C#應用程式中處理PDF生成和操作任務而設計的.NET庫。 它簡化了將 HTML 內容轉換為 PDF 格式的過程,提供豐富的自訂選項和可靠的性能。 無論您是在開發網頁應用程式、桌面軟體還是後端服務,IronPDF 提供了一整套工具,以無縫整合 PDF 生成功能。

主要功能

HTML 轉換為 PDF

  1. HTML 檔案: 將 HTML 檔案,包括 CSS、JavaScript 和影像,轉換為 PDF。

  2. HTML 字串: 將 HTML 字串直接呈現為 PDF 文件。

  3. URLs: 透過提供 URL 將網頁轉換為 PDF。

    PDF 生成與編輯

  4. 建立 PDF:從頭開始或從現有文件生成 PDF。

  5. 編輯 PDF:新增、移除或修改 PDF 中的文字、圖片及其他元素。

  6. 合併與拆分: 將多個 PDF 合併為一個,或將單個 PDF 拆分為多個文件。

    進階格式設定

  7. 頁首和頁尾: 添加頁首和頁尾,包括頁碼和日期。

  8. 水印: 為您的 PDF 文件添加水印。

  9. 頁面設定: 自訂頁面大小、方向、邊距等。

    安全性與合規性

  10. 密碼保護: 使用密碼保護PDF並設置權限。

  11. 數位簽章: 使用證書對 PDF 進行數位簽名。

  12. 合規性: 支援 PDF/A 和 PDF/UA 標準,以滿足長期存檔和無障礙訪問的需求。

    跨平台支持

  13. 操作系統:兼容 Windows、Linux 和 macOS。

  14. 開發環境:適用於.NET Framework、.NET Core、.NET 5、6、7和8。

GroupDocs.Conversion 和 IronPDF 的範例程式碼

先決條件

  1. Visual Studio:確保您已安裝 Visual Studio 以創建和管理您的 C# 專案。

  2. .NET 已安裝: 確保機器上已安裝最新的 .NET 版本 8

步驟 1:建立一個新專案以將 HTML 轉換為 PDF

首先,讓我們在 Visual Studio 中創建一個新項目。 為此,打開 Visual Studio 並點擊創建新項目。 當顯示以下窗口時,選擇專案類型為控制台應用程式。

GroupDocs 的 HTML 轉 PDF 轉換器 C# 替代方案:圖 3

選擇以下的專案名稱和位置。

HTML轉PDF轉換器的GroupDocs替代方案(C#):圖4

選擇所需的 .NET 版本。

GroupDocs替代方案HTML轉PDF轉換器在C#:圖5

當所有選項都選擇完畢後,專案將會成功創建。

步驟 2:將 GroupDocs 安裝到已建立的專案中。

現在使用 Visual Studio 套件管理器或直接從命令列安裝 GroupDocs.Conversion .NET NuGet 套件。

GroupDocs 的 C# HTML 到 PDF 轉換器替代方案:圖 6

從 CMD 行:

C# 中將 HTML 轉換為 PDF 的 GroupDocs 替代方案:圖 7

成功安裝 GroupDocs.Conversion NuGet 後,我們就可以開始使用該庫了。

步驟3:使用GroupDocs.Conversion函式庫將HTML文件轉換為PDF文件。

以下程式碼片段顯示如何使用 GroupDocs.Conversion 庫將 HTML 轉換為 PDF。

using System;
using System.IO;
using GroupDocs.Conversion.Options.Convert;
// Html To PDF conversion app sample code
namespace GroupDocVsIronPDF
{
    public class Program
    {
        public static void ConvertHtmlToPdf
        {
            string outputFolder = "C:\\code\\articles\\july26"; // output with html pages converted to PDF
            string outputFile = Path.Combine(outputFolder, "convertedUsingGroupDoc.pdf"); // converted pdf file
            var input = "C:\\code\\articles\\july26\\groupDocConversion.html" // input html file
            using (var converter = new GroupDocs.Conversion.Converter(input))
            {
                var options = new PdfConvertOptions();
                converter.Convert(outputFile, options);
                Console.WriteLine("\nConversion to pdf completed successfully. \nCheck output in {0}", outputFolder);
            }
        }
    }
}
using System;
using System.IO;
using GroupDocs.Conversion.Options.Convert;
// Html To PDF conversion app sample code
namespace GroupDocVsIronPDF
{
    public class Program
    {
        public static void ConvertHtmlToPdf
        {
            string outputFolder = "C:\\code\\articles\\july26"; // output with html pages converted to PDF
            string outputFile = Path.Combine(outputFolder, "convertedUsingGroupDoc.pdf"); // converted pdf file
            var input = "C:\\code\\articles\\july26\\groupDocConversion.html" // input html file
            using (var converter = new GroupDocs.Conversion.Converter(input))
            {
                var options = new PdfConvertOptions();
                converter.Convert(outputFile, options);
                Console.WriteLine("\nConversion to pdf completed successfully. \nCheck output in {0}", outputFolder);
            }
        }
    }
}
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports GroupDocs.Conversion.Options.Convert
' Html To PDF conversion app sample code
Namespace GroupDocVsIronPDF
	Public Class Program
		Public Shared ReadOnly Property ConvertHtmlToPdf() As void
			Dim outputFolder As String = "C:\code\articles\july26" ' output with html pages converted to PDF
			Dim outputFile As String = Path.Combine(outputFolder, "convertedUsingGroupDoc.pdf") ' converted pdf file
'INSTANT VB TODO TASK: The following line contains an assignment within expression that was not extracted by Instant VB:
'ORIGINAL LINE: var input = "C:\code\articles\july26\groupDocConversion.html" using(var converter = new GroupDocs.Conversion.Converter(input))
			Dim input = "C:\code\articles\july26\groupDocConversion.html" using(var converter = New GroupDocs.Conversion.Converter(input))
			If True Then
				Dim options = New PdfConvertOptions()
				converter.Convert(outputFile, options)
				Console.WriteLine(vbLf & "Conversion to pdf completed successfully. " & vbLf & "Check output in {0}", outputFolder)
			End If
		End Property
	End Class
End Namespace
VB   C#

程式碼解釋

此程序演示如何將 HTML 文件轉換 (群組文件轉換.html) 為PDF文件 (convertedUsingGroupDoc.pdf) 使用 GroupDocs.Conversion 庫。 以下是代碼的說明:

  1. 命名空间和引用: 程序开始时导入必要的命名空间。 System 和 System.IO 是標準的 .NET 命名空間,負責基本輸入輸出操作。 GroupDocs.Conversion.Options.Convert 用於匯入 GroupDocs.Conversion API 的轉換選項。 可能已經導入 Microsoft.VisualBasic 命名空間以使用某些工具,例如 Path.Combine,它是一種跨平台的路徑拼接方法。

  2. Program 類別: Program 類別作為 GroupDocVsIronPDF 命名空間的一部分被宣告。

    1. ConvertHtmlToPdf 方法: 這是一個靜態方法 (csharp public static void ConvertHtmlToPdf) 負責執行 HTML 至 PDF 的轉換。 它不需要任何參數。

    2. 輸出資料夾和檔案路徑:

      outputFolder:指定將轉換的 PDF 文件保存的目錄。

      outputFile:將outputFolder與所需的輸出檔案名稱結合 (convertedUsingGroupDoc.pdf),形成輸出 PDF 文件的完整路徑。

      Input: 指定輸入 HTML 文件的路徑。

  3. 使用 Converter 的 Using 語句: 初始化 GroupDocs.Conversion.Converter 並用於將 HTML 文件轉換為 PDF 格式,然後保存為 convertedUsingGroupDoc.pdf。

輸入 HTML 文件內容

讓我們以以下的 HTML 文件為例,你也可以下載任何可用的免費 HTML 文件。

GroupDocs 選擇 HTML to PDF 轉換器在 C#:圖 8

<!DOCTYPE html>
<html>
<body>
<h1 style="color:black;">Demonstrate GroupDocs .Net Package to Convert HTML to PDF</h1>
<p style="color:gray;">GroupDocs provides a robust API for HTML to PDF conversion, designed to be simple yet flexible enough to handle various scenarios. To begin using GroupDocs HTML to PDF converter in your C# applications.</p>
<h2 style="color:black;">Install</h2>
<code style="color:blue;"> dotnet add package GroupDocs.Conversion --version 24.6.0</code>
<h2 style="color:black;">Import Namespaces</h2>
<code class="language-csharp" data-lang="csharp">
<p style="color:blue;">using GroupDocs.Conversion.Options.Convert;
 </p>
<p style="color:blue;">
using System.IO;
 </p>
<p style="color:blue;"> using GroupDocs.Conversion.Options.Convert;</p>
</code>
<h2 style="color:black;">Load the Source HTML File</h2>
<code style="color:blue;"> using (var converter = new GroupDocs.Conversion.Converter("path to html file"))
</code>
<h2 style="color:black;">Perform the Conversion</h2>
<code style="color:blue;"> 
<p>var options = new PdfConvertOptions();</p>
<p>converter.Convert(outputFile, options);</p>
</code>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1 style="color:black;">Demonstrate GroupDocs .Net Package to Convert HTML to PDF</h1>
<p style="color:gray;">GroupDocs provides a robust API for HTML to PDF conversion, designed to be simple yet flexible enough to handle various scenarios. To begin using GroupDocs HTML to PDF converter in your C# applications.</p>
<h2 style="color:black;">Install</h2>
<code style="color:blue;"> dotnet add package GroupDocs.Conversion --version 24.6.0</code>
<h2 style="color:black;">Import Namespaces</h2>
<code class="language-csharp" data-lang="csharp">
<p style="color:blue;">using GroupDocs.Conversion.Options.Convert;
 </p>
<p style="color:blue;">
using System.IO;
 </p>
<p style="color:blue;"> using GroupDocs.Conversion.Options.Convert;</p>
</code>
<h2 style="color:black;">Load the Source HTML File</h2>
<code style="color:blue;"> using (var converter = new GroupDocs.Conversion.Converter("path to html file"))
</code>
<h2 style="color:black;">Perform the Conversion</h2>
<code style="color:blue;"> 
<p>var options = new PdfConvertOptions();</p>
<p>converter.Convert(outputFile, options);</p>
</code>
</body>
</html>
version 24.6.0 -= 1
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: <!DOCTYPE html> <html> <body> <h1 style="color:black;"> Demonstrate GroupDocs.Net Package to Convert HTML to PDF</h1> <p style="color:gray;"> GroupDocs provides a robust API for HTML to PDF conversion, designed to be simple yet flexible enough to handle various scenarios.@To begin using GroupDocs HTML to PDF converter in your C# applications.</p> <h2 style="color:black;"> Install</h2> <code style="color:blue;"> dotnet add package GroupDocs.Conversion --version 24.6.0</code> <h2 style="color:black;"> Import Namespaces</h2> <code class="language-csharp" data-lang="csharp"> <p style="color:blue;"> using GroupDocs.Conversion.Options.Convert;
"csharp"> <p style="color:blue;"> using GroupDocs.Conversion.Options.Convert
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: <!DOCTYPE html> <html> <body> <h1 style="color:black;"> Demonstrate GroupDocs.Net Package to Convert HTML to PDF</h1> <p style="color:gray;"> GroupDocs provides a robust API for HTML to PDF conversion, designed to be simple yet flexible enough to handle various scenarios.@To begin using GroupDocs HTML to PDF converter in your C# applications.</p> <h2 style="color:black;"> Install</h2> <code style="color:blue;"> dotnet add package GroupDocs.Conversion version 24.6.0</code> <h2 style="color:black;"> Import Namespaces</h2> <code class="language-csharp" data-lang="csharp"> <p style
"language-csharp" data-lang="csharp"> <p style
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: <!DOCTYPE html> <html> <body> <h1 style="color:black;"> Demonstrate GroupDocs.Net Package to Convert HTML to PDF</h1> <p style="color:gray;"> GroupDocs provides a robust API for HTML to PDF conversion, designed to be simple yet flexible enough to handle various scenarios.@To begin using GroupDocs HTML to PDF converter in your C# applications.</p> <h2 style="color:black;"> Install</h2> <code style="color:blue;"> dotnet add package GroupDocs.Conversion version 24.6.0</code> <h2 style="color:black;"> Import Namespaces</h2> <code class="language-csharp" data-lang
"color:black;"> Import Namespaces</h2> <code class="language-csharp" data-lang
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: <!DOCTYPE html> <html> <body> <h1 style="color:black;"> Demonstrate GroupDocs.Net Package to Convert HTML to PDF</h1> <p style="color:gray;"> GroupDocs provides a robust API for HTML to PDF conversion, designed to be simple yet flexible enough to handle various scenarios.@To begin using GroupDocs HTML to PDF converter in your C# applications.</p> <h2 style="color:black;"> Install</h2> <code style="color:blue;"> dotnet add package GroupDocs.Conversion version 24.6.0</code> <h2 style="color:black;"> Import Namespaces</h2> <code class
"color:blue;"> dotnet add package GroupDocs.Conversion version 24.6.0</code> <h2 style="color:black;"> Import Namespaces</h2> <code class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: <!DOCTYPE html> <html> <body> <h1 style="color:black;"> Demonstrate GroupDocs.Net Package to Convert HTML to PDF</h1> <p style="color:gray;"> GroupDocs provides a robust API for HTML to PDF conversion, designed to be simple yet flexible enough to handle various scenarios.@To begin using GroupDocs HTML to PDF converter in your C# applications.</p> <h2 style="color:black;"> Install</h2> <code style="color:blue;"> dotnet add package GroupDocs.Conversion version 24.6.0</code> <h2 style
"color:black;"> Install</h2> <code style="color:blue;"> dotnet add package GroupDocs.Conversion version 24.6.0</code> <h2 style
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: <!DOCTYPE html> <html> <body> <h1 style="color:black;"> Demonstrate GroupDocs.Net Package to Convert HTML to PDF</h1> <p style="color:gray;"> GroupDocs provides a robust API for HTML to PDF conversion, designed to be simple yet flexible enough to handle various scenarios.@To begin using GroupDocs HTML to PDF converter in your C# applications.</p> <h2 style="color:black;"> Install</h2> <code style
"color:gray;"> GroupDocs provides a robust API for HTML [to] PDF conversion, designed [to] be simple yet flexible enough [to] handle various scenarios.To begin using GroupDocs HTML [to] PDF converter in your C# applications.</p> <h2 style="color:black;"> Install</h2> <code style
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: <!DOCTYPE html> <html> <body> <h1 style="color:black;"> Demonstrate GroupDocs.Net Package to Convert HTML to PDF</h1> <p style="color:gray;"> GroupDocs provides a robust API for HTML to PDF conversion, designed to be simple yet flexible enough to handle various scenarios.@To begin using GroupDocs HTML to PDF converter in your C# applications.</p> <h2 style
"color:black;"> Demonstrate GroupDocs.Net Package [to] Convert HTML [to] PDF</h1> <p style="color:gray;"> GroupDocs provides a robust API for HTML [to] PDF conversion, designed [to] be simple yet flexible enough [to] handle various scenarios.To begin using GroupDocs HTML [to] PDF converter in your C# applications.</p> <h2 style
<(Not DOCTYPE) html> (Of html) (Of body) <h1 style="color:black;"> Demonstrate GroupDocs.Net Package [to] Convert HTML [to] PDF</h1> <p style
 </p> <p style="color:blue;"> using System.IO
 </p> <p style="color:blue;"> using GroupDocs.Conversion.Options.Convert
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: </p> </code> <h2 style="color:black;"> Load the Source HTML File</h2> <code style="color:blue;"> using(var converter = new GroupDocs.Conversion.Converter("path to html file")) </code> <h2 style="color:black;"> Perform the Conversion</h2> <code style="color:blue;"> <p> var options = new PdfConvertOptions();
 "color:blue;"> (Of p) var options = New PdfConvertOptions()
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: </p> </code> <h2 style="color:black;"> Load the Source HTML File</h2> <code style="color:blue;"> using(var converter = new GroupDocs.Conversion.Converter("path to html file")) </code> <h2 style="color:black;"> Perform the Conversion</h2> <code style="color:blue;"> <p> var options
 "color:black;"> Perform the Conversion</h2> <code style="color:blue;"> (Of p) var options
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: </p> </code> <h2 style="color:black;"> Load the Source HTML File</h2> <code style="color:blue;"> using(var converter = new GroupDocs.Conversion.Converter("path to html file")) </code> <h2 style="color:black;"> Perform the Conversion</h2> <code style
 "color:blue;"> using(var converter = New GroupDocs.Conversion.Converter("path to html file")) </code> <h2 style="color:black;"> Perform the Conversion</h2> <code style
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: </p> </code> <h2 style="color:black;"> Load the Source HTML File</h2> <code style="color:blue;"> using(var converter = new GroupDocs.Conversion.Converter("path to html file")) </code> <h2 style
 "color:black;"> Load the Source HTML File</h2> <code style="color:blue;"> using(var converter = New GroupDocs.Conversion.Converter("path to html file")) </code> <h2 style
 </p> </code> <h2 style="color:black;"> Load the Source HTML File</h2> <code style
 </p> (Of p) converter.Convert(outputFile, options)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
' </p> </code> </body> </html>
VB   C#

輸出 PDF

GroupDocs 的 C# HTML 轉 PDF 轉換器替代方案:圖 9

步驟 4:將 IronPDF 安裝到已建立的專案中。

現在安裝這個 IronPDF NuGet 套件來自 IronsSoftware 使用 Visual Studio 或從 NuGet 套件管理器。

GroupDocs 的 HTML 到 PDF 轉換器在 C# 中的替代方案:圖 10

從命令提示字元:

GroupDocs C# HTML 轉 PDF 轉換器的替代方案:圖 11

一旦成功安裝 NuGet 套件,專案便準備好添加代碼以使用 IronPDF 測試 HTML 轉換為 PDF 的過程。

第 5 步:使用 IronPDF 庫將 HTML 轉換為 PDF 文件。

以下程式碼顯示如何使用 IronPDF .NET 庫將 HTML 轉換為 PDF。

namespace GroupDocVsIronPDF
{
    internal class IronPDFDemo
    {
        public static void Execute()
        {
            IronPdf.License.LicenseKey = "your key";
            // Create Renderer
            var renderer = new ChromePdfRenderer();
            // Create a PDF from an existing HTML Data files
            var pdf = renderer.RenderHtmlFileAsPdf("C:\\code\\articles\\july26\\groupDocConversion.html");
            // Save As PDF 
            pdf.SaveAs("convertedUsingIronPDF.pdf");
        }
    }
}
namespace GroupDocVsIronPDF
{
    internal class IronPDFDemo
    {
        public static void Execute()
        {
            IronPdf.License.LicenseKey = "your key";
            // Create Renderer
            var renderer = new ChromePdfRenderer();
            // Create a PDF from an existing HTML Data files
            var pdf = renderer.RenderHtmlFileAsPdf("C:\\code\\articles\\july26\\groupDocConversion.html");
            // Save As PDF 
            pdf.SaveAs("convertedUsingIronPDF.pdf");
        }
    }
}
Namespace GroupDocVsIronPDF
	Friend Class IronPDFDemo
		Public Shared Sub Execute()
			IronPdf.License.LicenseKey = "your key"
			' Create Renderer
			Dim renderer = New ChromePdfRenderer()
			' Create a PDF from an existing HTML Data files
			Dim pdf = renderer.RenderHtmlFileAsPdf("C:\code\articles\july26\groupDocConversion.html")
			' Save As PDF 
			pdf.SaveAs("convertedUsingIronPDF.pdf")
		End Sub
	End Class
End Namespace
VB   C#

程式碼解釋

此 C# 程式在 GroupDocVsIronPDF 命名空間中展示如何使用 IronPDF 將 HTML 檔案進行轉換。 (群組文件轉換.html) 為PDF文件 (convertedUsingIronPDF.pdf). 以下是代碼的說明:

  1. 命名空間和類別: IronPDFDemo 類別在 GroupDocVsIronPDF 命名空間中被宣告為內部類別。

  2. Execute 方法: public static void Execute()此方法作為使用 IronPDF 執行 PDF 轉換的進入點。 它不需要任何參數。

  3. 設定授權金鑰: IronPdf.License.LicenseKey = "your key";:設定IronPDF的授權金鑰。 此步驟對於以授權模式使用 IronPDF 至關重要,確保符合許可條款。

  4. 創建渲染器: var renderer = new ChromePdfRenderer()初始化 ChromePdfRenderer 的新實例。 此渲染器能夠在底層使用Google Chrome的渲染引擎將HTML內容轉換為PDF。

  5. HTML 轉 PDF 轉換: renderer.RenderHtmlFileAsPdf("C:\code\articles\july26\groupDocConversion.html")將指定的 HTML 文件轉換 (群組文件轉換.html) 轉換成 PDF 文件 (PDF) 使用 ChromePdfRenderer 提供的 RenderHtmlFileAsPdf 方法。

  6. 保存 PDF: pdf.SaveAs("convertedUsingIronPDF.pdf");: 儲存轉換後的 PDF (PDF) 作為檔案名稱 convertedUsingIronPDF.pdf。 除非指定完整路徑,否則文件會存儲在當前工作目錄中。

輸入 HTML 文件

GroupDocs 的 C# HTML 轉 PDF 轉換器替代方案:圖 12

<!DOCTYPE html>
<html>
<body>
<h1 style="color:black;">Demonstrate Iron .Net Package to Convert HTML to PDF</h1>
<p style="color:gray;"> IronPDF simplifies HTML to PDF conversion in C# applications with its robust set of features and intuitive API. Whether you need to generate reports, convert web pages, or create printable documents from HTML content, IronPDF provides the tools necessary to achieve high-quality PDF outputs efficiently. By integrating IronPDF into your projects, you can enhance document generation workflows and deliver professional-grade PDF documents tailored to your application's requirements.</p>
<h2 style="color:black;">Install</h2>
<code style="color:blue;">dotnet add package IronPdf --version 2024.7.8</code>
<h2 style="color:black;">Import Namespaces</h2>
<code class="language-csharp" data-lang="csharp">
<p style="color:blue;">    using IronPdf;
 </p>
</code>
<h2 style="color:black;">Create ChromePdfRenderer</h2>
<code style="color:blue;"> var renderer = new ChromePdfRenderer();
</code>
<h2 style="color:black;">Create PDF from HTML file</h2>
<code style="color:blue;"> var pdf = renderer.RenderHtmlFileAsPdf("myHtmlFile.html");
</code>
<h2 style="color:black;">SAve PDF File</h2>
<code style="color:blue;"> pdf.SaveAs("output.pdf");
</code>
</code>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1 style="color:black;">Demonstrate Iron .Net Package to Convert HTML to PDF</h1>
<p style="color:gray;"> IronPDF simplifies HTML to PDF conversion in C# applications with its robust set of features and intuitive API. Whether you need to generate reports, convert web pages, or create printable documents from HTML content, IronPDF provides the tools necessary to achieve high-quality PDF outputs efficiently. By integrating IronPDF into your projects, you can enhance document generation workflows and deliver professional-grade PDF documents tailored to your application's requirements.</p>
<h2 style="color:black;">Install</h2>
<code style="color:blue;">dotnet add package IronPdf --version 2024.7.8</code>
<h2 style="color:black;">Import Namespaces</h2>
<code class="language-csharp" data-lang="csharp">
<p style="color:blue;">    using IronPdf;
 </p>
</code>
<h2 style="color:black;">Create ChromePdfRenderer</h2>
<code style="color:blue;"> var renderer = new ChromePdfRenderer();
</code>
<h2 style="color:black;">Create PDF from HTML file</h2>
<code style="color:blue;"> var pdf = renderer.RenderHtmlFileAsPdf("myHtmlFile.html");
</code>
<h2 style="color:black;">SAve PDF File</h2>
<code style="color:blue;"> pdf.SaveAs("output.pdf");
</code>
</code>
</body>
</html>
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'<!DOCTYPE html> <html> <body> <h1 style="color:black;"> Demonstrate Iron.Net Package @to Convert HTML @to PDF</h1> <p style="color:gray;"> IronPDF simplifies HTML @to PDF conversion in C# applications @with its robust @set @of features @and intuitive API.Whether you need @to generate reports, convert web pages, @or create printable documents from HTML content, IronPDF provides the tools necessary @to achieve high-quality PDF outputs efficiently.By integrating IronPDF into your projects, you can enhance document generation workflows @and deliver professional-grade PDF documents tailored @to your application's requirements.</p> <h2 style="color:black;"> Install</h2> <code style="color:blue;"> dotnet add package IronPdf --version 2024.7.8</code> <h2 style="color:black;"> Import Namespaces</h2> <code class="language-csharp" data-lang="csharp"> <p style="color:blue;"> using IronPdf; </p> </code> <h2 style="color:black;"> Create ChromePdfRenderer</h2> <code style="color:blue;"> var renderer = New ChromePdfRenderer(); </code> <h2 style="color:black;"> Create PDF from HTML file</h2> <code style="color:blue;"> var pdf = renderer.RenderHtmlFileAsPdf("myHtmlFile.html"); </code> <h2 style="color:black;"> SAve PDF File</h2> <code style="color:blue;"> pdf.SaveAs("output.pdf"); </code> </code> </body> </html>
VB   C#

輸出 PDF

C# 中 HTML 到 PDF 轉換器的 GroupDocs 替代方案:圖 13

IronPDF 授權

IronPDF 以便讓用戶在購買之前查看其豐富的功能。

在使用前,請將授權金鑰放置於腳本的開頭。 IronPDF 套件:

using IronPdf;
# Apply your license key
License.LicenseKey = "key"
using IronPdf;
# Apply your license key
License.LicenseKey = "key"
Imports IronPdf
#Apply your license key
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'License.LicenseKey = "key"
VB   C#

GroupDocs.Conversion

優點:

  1. 多種格式支援: 支援多種文件格式的轉換,包括 Word、Excel、PowerPoint、PDF、HTML 等。

  2. 轉換質量: 一般來說提供良好的轉換質量,並有多種自訂選項。

  3. 文件操作也包含在內: 提供除轉換之外的其他功能,例如文件操作和元數據處理。

  4. API 彈性: 提供適用於 .NET 和 Java 的 API,這對於跨平台或混合技術項目可能是有利的。

    缺點

  5. 成本: 特別是對於商業用途或處理大量文件時,可能相對昂貴。

  6. 學習曲線: 根據使用情境的複雜性,整合和配置 GroupDocs.Conversion 可能需要較陡峭的學習曲線。

  7. 效能: 在某些情況下,效能可能不如其他庫來得優化,特別是對於非常大的文件或複雜的轉換。

IronPDF

優點:

  1. PDF 生成: 非常適合在 .NET 應用程式中直接從 HTML、URL 或其他格式生成 PDF。

  2. 易用性: 通常易於整合和使用,特別是針對基本的 PDF 生成任務。

  3. 文件和支援: 提供良好的文件和支援,對於剛開始的開發者來說非常有幫助。

  4. 成本: 根據您的使用案例,與 GroupDocs.Conversion 相比,它可能在成本上更具效益,尤其是在 PDF 生成任務方面。

    缺點

  5. 有限的格式支持: 主要專注於 PDF 生成; 與 GroupDocs.Conversion 不同,它專門設計僅適用於 PDF 格式。

  6. 自訂化: 雖然適用於基本的 PDF 生成需求,但與 GroupDocs.Conversion 相比,版面配置和進階功能的自訂化選項可能有限。

    擁有高品質的客戶支持,並由技術精湛的開發人員支持,以及 IronSuite,因此這些缺點被抵銷,因此 IronPDF 在這兩者中脫穎而出,成為提供現代、簡單且先進的 PDF 轉換功能的更佳庫。

結論

GroupDocs C# 中的 HTML 到 PDF 轉換器為在應用程式內將 HTML 內容轉換為 PDF 文件提供了一個簡單而強大的解決方案。 通過遵循本文提供的步驟和範例,您可以將HTML轉換為PDF的功能快速整合到您的專案中,無論是用於生成報告、存檔網頁內容或任何其他文件處理需求。 GroupDocs 的庫提供靈活性和廣泛的自訂選項,是開發人員尋求通過從 HTML 內容生成 PDF 來增強其應用程序的可靠選擇。

相比之下, IronPDF 透過其強大的功能集和直觀的 API,簡化了 C# 應用程式中的 HTML 到 PDF 轉換。 無論您需要生成報告、轉換網頁,還是從 HTML 內容創建可打印文檔,IronPDF 提供必要的工具,以高效產生高品質的 PDF 輸出。 通過將 IronPDF 集成到您的項目中,您可以強化文件生成工作流程,並交付根據應用程式需求量身定制的專業級 PDF 文件。

提供卓越的客戶支持,並由技術嫻熟的開發人員支持,以及 IronSuite, IronPDF 在這兩者中脫穎而出,成為提供現代、簡單且先進的 PDF 轉換功能的更佳庫。

< 上一頁
C#中的PDFA庫(.NET開發人員的比較清單)
下一個 >
CraftMyPDF 和 IronPDF 的比較

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 10,993,239 查看許可證 >