跳過到頁腳內容
產品比較
使用 IronPDF 作為 PDFSharp 的替代品

PDFSharp HTML轉PDF範例及教程比較

在 .NET 環境中處理 PDF 文件時,開發人員需要一個可靠且功能豐富的庫來處理常見任務,如文件創建、編輯、加密和HTML 到 PDF轉換。 IronPDF and PDFsharp是兩個流行的庫,但它們滿足不同的需求。

IronPDF 是一個功能全面、高性能的 PDF 庫,在 HTML 到 PDF 轉換、PDF 安全性和高級 PDF 操作方面表現出色。 PDFsharp 則是一個輕量級的開源庫,主要專注於 PDF 創建和基本編輯。 在它們之間選擇取決於您項目的復雜程度和要求。

在本指南中,我們將在關鍵功能、可用性、安全性、定價和支持方面比較 IronPDF 和 PDFsharp,幫助您確定最適合您的 .NET 應用程序的工具。

功能概述

IronPDF:全面的 PDF 解決方案

IronPDF is a feature-packed .NET library that excels in many areas, including HTML 到 PDF 轉換、PDF 編輯和安全的文檔處理。 它可以輕鬆地通過 NuGet 包管理器控制台添加到您的項目中,並與您的 C# 項目無縫集成。

其突出的功能包括:

PDFsharp:輕量級替代方案

PDFsharp 是一個免費的開源庫,主要設計用於基本的 PDF 創建和操作。 雖然它不提供 IronPDF 那樣豐富的功能集,但對於不需要複雜功能的簡單項目來說,它是一個很好的選擇。 PDFsharp 的一些核心功能包括:

  • PDF 創建:使用 C# 或 VB.NET 程式生成 PDF,創建帶有圖像和圖形的文本文檔。
  • PDF 修改:簡單任務如合併、拆分和編輯現有 PDF 文件。
  • 基本加密:為 PDF 文件添加基本的密碼保護。

關鍵限制:PDFsharp 本身不支持 HTML 到 PDF 轉換,需要像 Polybioz.HtmlRenderer.PdfSharp.Core 這樣的第三方工具,增加了複雜性。

兼容性和可用性

跨平台支持

Pdfsharp Read Pdf 1 related to 跨平台支持

IronPDF

IronPDF 設計為跨平台,完全兼容 .NET Core、.NET Framework 和 Azure,支持 Windows、macOS 和 Linux。 這使得它成為需要在多個操作系統中運行的項目的理想選擇,確保該庫在大多數開發環境中具有通用兼容性。 該庫與現代 Web 開發環境無縫集成,使其成為 Web 應用程序、雲解決方案和企業系統的完美選擇。

PDFsharp

PDFsharp 主要設計用於 .NET Framework,對 .NET Core 提供有限的支持。 雖然存在一個 .NET Core 版本,但仍在繼續開發中,某些功能尚未為非 Windows 平台完全優化。 Linux 和 macOS 用戶可能會面臨兼容性問題,這使得 PDFsharp 在跨平台應用程序中不太理想。

易用性

IronPDF 提供一個開發者友好的 API,抽象了操作 PDF 文件的複雜性。 無論是生成 PDF 還是編輯現有文件,語法都簡潔明了,允許開發人員只需幾行代碼就能實現複雜的功能。

PDFsharp 對於文本生成和 PDF 操作等基本任務相對容易使用,但對於複雜的文檔生成和編輯任務,經常需要更多的手動處理。

易用性 Example

IronPDF:將 HTML 轉換為 PDF

輕鬆地將 HTML 片段和內容轉換為 PDF,只需一次方法調用

using IronPdf;

var renderer = new ChromePdfRenderer();
var content = "<h1>IronPDF Rocks!</h1>";
var pdf = renderer.RenderHtmlAsPdf(content);
pdf.SaveAs("output.pdf");
using IronPdf;

var renderer = new ChromePdfRenderer();
var content = "<h1>IronPDF Rocks!</h1>";
var pdf = renderer.RenderHtmlAsPdf(content);
pdf.SaveAs("output.pdf");
Imports IronPdf

Private renderer = New ChromePdfRenderer()
Private content = "<h1>IronPDF Rocks!</h1>"
Private pdf = renderer.RenderHtmlAsPdf(content)
pdf.SaveAs("output.pdf")
$vbLabelText   $csharpLabel
  • 簡潔的代碼\
  • 從現有 HTML 內容輕鬆轉換

PDFsharp:創建帶有文本的 PDF

PDFsharp 需要通過使用圖形對象手動實現文本和內容的繪製:

using PdfSharp.Pdf;
using PdfSharp.Drawing;

var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 20, XFontStyleEx.Bold);
gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black, new XPoint(50, 100));
document.Save("output.pdf");
using PdfSharp.Pdf;
using PdfSharp.Drawing;

var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 20, XFontStyleEx.Bold);
gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black, new XPoint(50, 100));
document.Save("output.pdf");
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing

Private document = New PdfDocument()
Private page = document.AddPage()
Private gfx = XGraphics.FromPdfPage(page)
Private font = New XFont("Arial", 20, XFontStyleEx.Bold)
gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black, New XPoint(50, 100))
document.Save("output.pdf")
$vbLabelText   $csharpLabel

限制:

手動實現 – 您必須手動定位元素。\ 在生成報告、發票或樣式內容時更為複雜

功能比較

HTML 到 PDF 轉換

IronPDF:適合 Web 應用和動態報告

IronPDF 在轉換 HTML 報告、網頁和動態內容為 PDF 並保留 CSS、JavaScript 和嵌入圖像方面表現出色。 這使得它非常適合:

  • 從 HTML 模板生成發票
  • 從 Web 應用程序自動創建報告
  • 將網頁截取為 PDF 以供離線使用

IronPDF 代碼示例 – 將 HTML 文件轉換為 PDF 格式:

using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlFileAsPdf("example.html");
pdf.SaveAs("example.pdf");
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlFileAsPdf("example.html");
pdf.SaveAs("example.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlFileAsPdf("example.html")
pdf.SaveAs("example.pdf")
$vbLabelText   $csharpLabel

輸出

Pdfsharp Read Pdf 2 related to IronPDF:適合 Web 應用和動態報告

如您所見,使用 IronPDF,我們只需三行簡單的代碼就可以將樣式化的 HTML 文件轉換成 PDF 文檔,不會失去原始質量或佈局! 如果您想看看 IronPDF 如何處理更多 CSS 向內容,為什麼不看看將 此 URL 的內容轉換為 PDF 的示例?

using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.JavaScript(5000);
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.apple.com");
pdf.SaveAs("Apple.pdf");
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.JavaScript(5000);
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.apple.com");
pdf.SaveAs("Apple.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()
renderer.RenderingOptions.EnableJavaScript = True
renderer.RenderingOptions.WaitFor.JavaScript(5000)
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print
Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://www.apple.com")
pdf.SaveAs("Apple.pdf")
$vbLabelText   $csharpLabel

輸出

IronPDF 的 URL 到 PDF 輸出

在這裡,我們利用 IronPDF 強大的渲染選項,將網頁內容轉換為高質量 PDF,保留了所有原始 CSS 和 JavaScript 內容。

PDFsharp:適合不含 Web 內容的靜態 PDF

PDFsharp 本身不支持 HTML 到 PDF 轉換。 如果您需要此功能,必須使用 Html Renderer 等第三方庫,它提供將 HTML 內容轉換為 PDF 文檔的靜態渲染代碼。 由於這些限制,PDFsharp 通常最適合於:

  • 不依賴動態 HTML 渲染的靜態文檔
  • 通過程序定義內容的發票、合同和報告

PDFsharp 配合 HtmlRenderer 示例:

using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;

var document = new PdfSharpCore.Pdf.PdfDocument();
string htmlContent = File.ReadAllText("index.html");
PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4);
document.Save("html-to-pdf-pdfsharp.pdf");
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;

var document = new PdfSharpCore.Pdf.PdfDocument();
string htmlContent = File.ReadAllText("index.html");
PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4);
document.Save("html-to-pdf-pdfsharp.pdf");
Imports PdfSharp.Pdf
Imports TheArtOfDev.HtmlRenderer.PdfSharp

Private document = New PdfSharpCore.Pdf.PdfDocument()
Private htmlContent As String = File.ReadAllText("index.html")
PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4)
document.Save("html-to-pdf-pdfsharp.pdf")
$vbLabelText   $csharpLabel

輸出

PDFsharp HTML 到 PDF 示例輸出

由於 HtmlRenderer 插件,我們能夠將大多數 HTML 內容轉換為 PDF,同時保留大部分原始樣式,但當它面對更 CSS 密集的內容時如何與 IronPDF 比較呢? 讓我們看一下將 Web 內容轉換為 PDF 文件的過程:

using PdfSharpCore.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
using System.Net.Http;
using System.Threading.Tasks;
using System.IO;

public class Program
{
    public static async Task Main(string[] args)
    {
        // Define the URL to fetch HTML content
        string url = "https://www.example.com"; // Replace with the URL you want to convert

        // Fetch HTML content from the URL
        string htmlContent = await FetchHtmlFromUrl(url);

        // Generate the PDF from the fetched HTML content
        var document = new PdfSharpCore.Pdf.PdfDocument();
        PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4);

        // Save the generated PDF to a file
        document.Save("url-to-pdf-example.pdf");

        System.Console.WriteLine("PDF created successfully!");
    }

    // Method to fetch HTML content from a URL
    private static async Task<string> FetchHtmlFromUrl(string url)
    {
        using (HttpClient client = new HttpClient())
        {
            // Send GET request to fetch the HTML content of the URL
            HttpResponseMessage response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                // Read the content as a string
                string htmlContent = await response.Content.ReadAsStringAsync();
                return htmlContent;
            }
            else
            {
                throw new Exception($"Failed to fetch content from URL. Status Code: {response.StatusCode}");
            }
        }
    }
}
using PdfSharpCore.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
using System.Net.Http;
using System.Threading.Tasks;
using System.IO;

public class Program
{
    public static async Task Main(string[] args)
    {
        // Define the URL to fetch HTML content
        string url = "https://www.example.com"; // Replace with the URL you want to convert

        // Fetch HTML content from the URL
        string htmlContent = await FetchHtmlFromUrl(url);

        // Generate the PDF from the fetched HTML content
        var document = new PdfSharpCore.Pdf.PdfDocument();
        PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4);

        // Save the generated PDF to a file
        document.Save("url-to-pdf-example.pdf");

        System.Console.WriteLine("PDF created successfully!");
    }

    // Method to fetch HTML content from a URL
    private static async Task<string> FetchHtmlFromUrl(string url)
    {
        using (HttpClient client = new HttpClient())
        {
            // Send GET request to fetch the HTML content of the URL
            HttpResponseMessage response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                // Read the content as a string
                string htmlContent = await response.Content.ReadAsStringAsync();
                return htmlContent;
            }
            else
            {
                throw new Exception($"Failed to fetch content from URL. Status Code: {response.StatusCode}");
            }
        }
    }
}
Imports PdfSharpCore.Pdf
Imports TheArtOfDev.HtmlRenderer.PdfSharp
Imports System.Net.Http
Imports System.Threading.Tasks
Imports System.IO

Public Class Program
	Public Shared Async Function Main(ByVal args() As String) As Task
		' Define the URL to fetch HTML content
		Dim url As String = "https://www.example.com" ' Replace with the URL you want to convert

		' Fetch HTML content from the URL
		Dim htmlContent As String = Await FetchHtmlFromUrl(url)

		' Generate the PDF from the fetched HTML content
		Dim document = New PdfSharpCore.Pdf.PdfDocument()
		PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4)

		' Save the generated PDF to a file
		document.Save("url-to-pdf-example.pdf")

		System.Console.WriteLine("PDF created successfully!")
	End Function

	' Method to fetch HTML content from a URL
	Private Shared Async Function FetchHtmlFromUrl(ByVal url As String) As Task(Of String)
		Using client As New HttpClient()
			' Send GET request to fetch the HTML content of the URL
			Dim response As HttpResponseMessage = Await client.GetAsync(url)

			If response.IsSuccessStatusCode Then
				' Read the content as a string
				Dim htmlContent As String = Await response.Content.ReadAsStringAsync()
				Return htmlContent
			Else
				Throw New Exception($"Failed to fetch content from URL. Status Code: {response.StatusCode}")
			End If
		End Using
	End Function
End Class
$vbLabelText   $csharpLabel

輸出

Pdfsharp Read Pdf 5 related to PDFsharp:適合不含 Web 內容的靜態 PDF

雖然 PDFsharp 無法處理像 IronPDF 那樣的密集 CSS 和 JavaScript 內容,但對於那些尋找一個輕量級 PDF 庫來執行不依賴於 CSS 或 JavaScript 的 PDF 生成任務的人來說,仍然是一個強有力的競爭者。 儘管 HTML 轉換需要額外的庫,PDFsharp 仍能夠在 .NET 生態系統中從頭創建 PDF 文檔。

加密和安全性

IronPDF:適合安全、合規驅動的應用

IronPDF 是需要安全的 PDF 處理的開發人員的首選,特別是在需要遵守嚴格數據保護法規的應用程序中。 它提供了先進的安全功能,旨在保證 PDF 文件的安全性和防篡改性,使其適合於金融、醫療和法律等行業。

IronPDF 的應用:

  • 法律和財務文件:IronPDF 允許您通過高級加密方法來保護 PDF 中的敏感數據,確保只有授權用戶能夠訪問它們。
  • 需要數字簽名的文件:它提供數字簽名功能,確保文檔的真實性,這對於法律協議和合同非常重要。
  • GDPR 和 HIPAA 合規性:當處理個人或敏感信息時,IronPDF 支持符合 GDPRHIPAA 等法規的加密標準,為開發人員處理安全客戶數據提供了安心。

IronPDF 的加密功能包括用戶級和擁有者級密碼,這意味著您可以限制對 PDF 的訪問或限制特定行為,如列印、複製或編輯。 此外,它允許您將數字簽名應用於 PDF,確保數據的完整性和不可否認性,使其非常適合法律文件。 這些功能在文檔安全至關重要的工作流程中尤其有用。

IronPDF 代碼示例 – 使用密碼加密 PDF:

using IronPdf;

var pdf = PdfDocument.FromFile("ConfidentialDocument.pdf");
pdf.SecuritySettings.UserPassword = "user";
pdf.SecuritySettings.OwnerPassword = "owner";
pdf.SecuritySettings.AllowUserEdits = IronPdf.Security.PdfEditSecurity.NoEdit;
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SaveAs("ConfidentialDocumentSecured.pdf");
using IronPdf;

var pdf = PdfDocument.FromFile("ConfidentialDocument.pdf");
pdf.SecuritySettings.UserPassword = "user";
pdf.SecuritySettings.OwnerPassword = "owner";
pdf.SecuritySettings.AllowUserEdits = IronPdf.Security.PdfEditSecurity.NoEdit;
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SaveAs("ConfidentialDocumentSecured.pdf");
Imports IronPdf

Private pdf = PdfDocument.FromFile("ConfidentialDocument.pdf")
pdf.SecuritySettings.UserPassword = "user"
pdf.SecuritySettings.OwnerPassword = "owner"
pdf.SecuritySettings.AllowUserEdits = IronPdf.Security.PdfEditSecurity.NoEdit
pdf.SecuritySettings.AllowUserAnnotations = False
pdf.SaveAs("ConfidentialDocumentSecured.pdf")
$vbLabelText   $csharpLabel

輸出

具有自定義權限和密碼保護的 PDF

在這個示例中,IronPDF 加密了 PDF 並限制了用戶行為,確保文件免受未經授權的訪問或操控。

PDFsharp:適合基本安全需求

PDFsharp 提供基本的密碼保護,但缺乏像數字簽名、水印或複雜加密這樣的高級功能。 如果您的用例僅需要限制對 PDF 的訪問或應用基本密碼,那麼 PDFsharp 是一個不錯的選擇。 然而,如果您的應用需要更高級的安全功能,如安全的文檔簽名或符合合規標準的加密,您可能會發現 PDFsharp 有所不足。

PDFsharp 的應用:

  • 保護內部文件:PDFsharp 適合通過基本密碼保護來防止未經授權的訪問內部報告、發票和合同的 PDF 文檔。
  • 機密文件的基本加密:如果您的應用不需要合規驅動的安全性,但您仍然希望確保未經授權的用戶不能打開敏感的 PDF 文件,PDFsharp 的密碼保護是一個簡單的解決方案。

不過,與 IronPDF 相比,PDFsharp 的加密功能有限,使得它不適用於金融或醫療等行業需要強安全性的場景。 它可以使用用戶密碼保護 PDF,並限制某些操作,如打印和複製,但不提供金融或醫療等行業所需的強加密標準。

PDFsharp 代碼示例 – 基本 PDF 加密:

using PdfSharp.Pdf;
using PdfSharp.Pdf.Security;
using PdfSharp.Drawing;

// Create a new document
var document = new PdfSharp.Pdf.PdfDocument();

// Add a page and draw something on it
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 16, XFontStyleEx.Bold);
gfx.DrawString("Confidential Document", font, XBrushes.Black, new XPoint(50, 50));

// Set password protection
document.SecuritySettings.UserPassword = "password123";
document.SecuritySettings.OwnerPassword = "admin456";

// Save the encrypted PDF
document.Save("pdfsharp-encrypted.pdf");
using PdfSharp.Pdf;
using PdfSharp.Pdf.Security;
using PdfSharp.Drawing;

// Create a new document
var document = new PdfSharp.Pdf.PdfDocument();

// Add a page and draw something on it
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 16, XFontStyleEx.Bold);
gfx.DrawString("Confidential Document", font, XBrushes.Black, new XPoint(50, 50));

// Set password protection
document.SecuritySettings.UserPassword = "password123";
document.SecuritySettings.OwnerPassword = "admin456";

// Save the encrypted PDF
document.Save("pdfsharp-encrypted.pdf");
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.Security
Imports PdfSharp.Drawing

' Create a new document
Private document = New PdfSharp.Pdf.PdfDocument()

' Add a page and draw something on it
Private page = document.AddPage()
Private gfx = XGraphics.FromPdfPage(page)
Private font = New XFont("Arial", 16, XFontStyleEx.Bold)
gfx.DrawString("Confidential Document", font, XBrushes.Black, New XPoint(50, 50))

' Set password protection
document.SecuritySettings.UserPassword = "password123"
document.SecuritySettings.OwnerPassword = "admin456"

' Save the encrypted PDF
document.Save("pdfsharp-encrypted.pdf")
$vbLabelText   $csharpLabel

輸出

使用 PDFsharp 加密的 PDF

在這個代碼中,PDFsharp 只是為 PDF 文檔設置了一個用戶密碼,以限制訪問。

功能性應用

IronPDF:適合全面的 PDF 編輯、編輯和轉換

IronPDF 在需要全面的 PDF 編輯、編輯以及處理多種文件格式(如 DOCX 到 PDF 轉換)的場景中表現出色。 它是開發需要高級 PDF 操作的應用程序,以便輕鬆集成到業務工作流中的理想選擇。

IronPDF 的實際用例:

  • 編輯敏感信息:IronPDF 在法律、醫療和政府等行業中非常有效,這些行業需要將敏感信息(如個人數據、財務詳情或醫療記錄)從 PDF 文件中移除或隱藏。 編輯涉及的不僅僅是覆蓋文本,還要確保已編輯的信息無法恢復。
  • 自動化 DOCX 文件到 PDF 的創建:IronPDF 使得將 Microsoft Word 文件(DOCX)轉換為 PDF 變得簡單。 這在企業軟件或電子商務等環境中特別有用,這些環境中發票、報告或合同經常從 Word 文件生成,並需要轉換為安全的 PDF 以便存檔或分發。
  • 可編輯的 PDF 表單:如果您正在構建允許用戶輸入數據的交互式 PDF 表單(例如,申請、調查、反饋表單),IronPDF 允許您創建和修改可填寫的表單。 此功能通常用於人力資源、教育和客戶支持等行業。

示例用例 – 將 DOCX 轉換為 PDF 並添加水印:在企業環境中,IronPDF 可以用來將 Word 文檔轉換為 PDF,然後添加水印以進行品牌推廣或安全目的。 使用 IronPDF,您可以將水印應用於指定頁面或整個 PDF 頁面。

在這裡,我們將採用一個樣本 DOCX 文件並應用一個水印,標記為草稿,帶有示例公司品牌。

using IronPdf;
using IronPdf.Editing;

DocxToPdfRenderer renderer = new DocxToPdfRenderer();
PdfDocument pdf = renderer.RenderDocxAsPdf("document.docx");
var watermark = "<h1 style='Color: red; font-size: 60px'>DRAFT</h1><img style='width: 200px' src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>";
pdf.ApplyWatermark(watermark, opacity: 75, VerticalAlignment.Top, HorizontalAlignment.Right);
pdf.SaveAs("output.pdf");
using IronPdf;
using IronPdf.Editing;

DocxToPdfRenderer renderer = new DocxToPdfRenderer();
PdfDocument pdf = renderer.RenderDocxAsPdf("document.docx");
var watermark = "<h1 style='Color: red; font-size: 60px'>DRAFT</h1><img style='width: 200px' src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>";
pdf.ApplyWatermark(watermark, opacity: 75, VerticalAlignment.Top, HorizontalAlignment.Right);
pdf.SaveAs("output.pdf");
Imports IronPdf
Imports IronPdf.Editing

Private renderer As New DocxToPdfRenderer()
Private pdf As PdfDocument = renderer.RenderDocxAsPdf("document.docx")
Private watermark = "<h1 style='Color: red; font-size: 60px'>DRAFT</h1><img style='width: 200px' src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>"
pdf.ApplyWatermark(watermark, opacity:= 75, VerticalAlignment.Top, HorizontalAlignment.Right)
pdf.SaveAs("output.pdf")
$vbLabelText   $csharpLabel

輸出

DOCX 到 PDF 渲染和自定義水印的 PDF 輸出

在此示例中,IronPDF首先將一個DOCX 文檔轉換為 PDF,然後添加一個水印以確保文檔被標記為“草稿”。 此功能可供需要在分發敏感信息時確保文檔的安全性或跟踪哪些文檔是草稿的公司使用。

PDFsharp:適合輕量級的 PDF 編輯和簡單的文檔生成

PDFsharp 更適合基本的 PDF 操作,當需要輕量級的編輯和從頭開始創建文檔時。 它在創建簡單的 PDF 記錄上表現出色,能從頭創建 PDF 文檔,修改現有 PDF,並應用基本的編輯。 然而,當涉及到更複雜的操作,例如高級編輯、文檔轉換或處理表單數據時,相比 IronPDF,PDFsharp 的能力更加有限。

PDFsharp 的實際用例:

  • 生成簡單的報告和文檔:PDFsharp 非常適合創建簡單的靜態文檔,例如報告、發票或宣傳冊。 它經常用於小型業務應用中,PDF 生成不需要過於複雜,但需包含基本格式,如文本、圖像和表格。
  • 基本的 PDF 編輯和合併:在需要將多個 PDF 合併為一個文檔或將大型 PDF 拆分為較小部分的情況下,PDFsharp 提供簡單的功能來處理這些任務。 它非常適合於不需要高級 PDF 操作的文檔管理系統。
  • 基本編輯:雖然 PDFsharp 不提供像 IronPDF 那樣的高級編輯功能,但它可以用於基本編輯,例如使用形狀或實心顏色覆蓋文本或圖像。 這對於低安全性文檔或不要求遵守嚴格數據隱私法的應用程序來說可能已經足夠。

示例用例:在商務環境中,PDFsharp 可以用於從數據庫提取數據生成報告,將多個文件合併為一個,或者為內部使用編輯部分文檔。

PDFsharp 代碼示例 – 合併兩個 PDF:

using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

// Open the first PDF
PdfDocument document1 = PdfReader.Open("document1.pdf", PdfDocumentOpenMode.Import);

// Open the second PDF
PdfDocument document2 = PdfReader.Open("document2.pdf", PdfDocumentOpenMode.Import);

// Create a new PDF document for the merged file
PdfDocument outputDocument = new PdfDocument();

// Add pages from the first document
foreach (PdfPage page in document1.Pages)
{
    outputDocument.AddPage(page);
}

// Add pages from the second document
foreach (PdfPage page in document2.Pages)
{
    outputDocument.AddPage(page);
}

// Save the merged PDF
outputDocument.Save("merged-document.pdf");
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

// Open the first PDF
PdfDocument document1 = PdfReader.Open("document1.pdf", PdfDocumentOpenMode.Import);

// Open the second PDF
PdfDocument document2 = PdfReader.Open("document2.pdf", PdfDocumentOpenMode.Import);

// Create a new PDF document for the merged file
PdfDocument outputDocument = new PdfDocument();

// Add pages from the first document
foreach (PdfPage page in document1.Pages)
{
    outputDocument.AddPage(page);
}

// Add pages from the second document
foreach (PdfPage page in document2.Pages)
{
    outputDocument.AddPage(page);
}

// Save the merged PDF
outputDocument.Save("merged-document.pdf");
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO

' Open the first PDF
Private document1 As PdfDocument = PdfReader.Open("document1.pdf", PdfDocumentOpenMode.Import)

' Open the second PDF
Private document2 As PdfDocument = PdfReader.Open("document2.pdf", PdfDocumentOpenMode.Import)

' Create a new PDF document for the merged file
Private outputDocument As New PdfDocument()

' Add pages from the first document
For Each page As PdfPage In document1.Pages
	outputDocument.AddPage(page)
Next page

' Add pages from the second document
For Each page As PdfPage In document2.Pages
	outputDocument.AddPage(page)
Next page

' Save the merged PDF
outputDocument.Save("merged-document.pdf")
$vbLabelText   $csharpLabel

輸出

使用 PDFsharp 合併 PDF 的輸出

此代碼演示了如何使用 PDFsharp兩個 PDF 文檔合併為一個。 這是一個常見的用例,適用於需要將多個報告、發票或其他文檔合併為一個 PDF 的應用程式。

定價和許可

IronPDF 和 PDFsharp 在定價和許可方面提供了不同的方案,滿足各種需求。 IronPDF 提供永久許可模式,而 PDFsharp 是開源的,免費供使用。

IronPDF 的定價與授權:

IronPDF 採用永久許可模式,意味著用戶獲得終身使用權和年度更新。 它提供了基於使用量的分級定價,包括個人、團隊或企業授權。 提供免費試用,允許用戶在購買前測試。 商業授權用戶還可以獲得技術支持和功能更新。

  • 永久許可:終身訪問權,包括年度更新
  • 分級許可:適用於個人團隊企業的許可。
  • 免費試用:用戶可以在購買前進行嘗試。
  • 商業支持:獲得支持和定期更新。

PDFsharp 的許可:

PDFsharp 是免費的,使用MIT 許可證,這意味著它開源並且免費供個人和商業使用。 然而,它不提供高級支持,用戶需要依靠社區論壇獲取幫助。 這適合較小的或開源項目,但對於更為複雜的需求可能需要外部幫助。

  • MIT 許可證:商業使用
  • 無高級支持:**社區論壇**提供幫助。
  • 靈活性:適合小型項目

最終用戶支持和文檔

IronPDF 在開發者支持方面表現突出,提供詳細的文檔和多種渠道的幫助。 與 PDFsharp 不同,IronPDF 提供了結構化資源,幫助開發人員完成整合過程。

IronPDF 開發者支持:

IronPDF provides comprehensive documentation, including step-by-step guides, API references, and code samples. 專門的支持團隊可供技術問題查詢。 此外,還有一個在線知識庫提供故障排除解決方案和有用的提示。 對於企業客戶,優先支持確保更快地解決問題。

  • 全面的文檔:詳細的指南代碼示例
  • 支持團隊:直接聯繫技術支持
  • 知識庫:**常見問題解答**和故障排除提示。
  • 優先支持:企業的快速解決問題

互動的社區面向:

IronPDF 還培養了一個活躍的開發者社區。 用戶可以參加社區論壇,在那裡他們可以提出問題、分享經驗並找到解決方案。 此外,教程和視頻指南幫助用戶快速入門。 IronPDF 也在 GitHub 上有出現,允許社區報告錯誤和建議功能。

  • 社區論壇:提問和分享解決方案的空間。
  • 教程與指南:**步驟指引視頻資源**。
  • GitHub 協作:用戶可以貢獻和建議新功能。

PDFsharp 開發者支持:

PDFsharp 的支持主要來自其 GitHub 存儲庫和 Stack Overflow 等社區論壇。 開發者可以報告問題、瀏覽解決方案或提出問題。 然而,文檔有限,詳細指南或官方教程稀缺,需要開發人員依賴社區貢獻或試錯。

  • GitHub 存儲庫:報告問題並探索解決方案。
  • 社區論壇:找到在 Stack Overflow 上的幫助。
  • 稀缺文檔:有限的指南和資源。

社區驅動的貢獻:

缺乏官方支持意味著開發商可以通過 GitHub 為 PDFsharp 做出貢獻,改進文檔或添加功能。 雖然有用戶貢獻的教程,但缺乏專門的支持團隊意味著故障排除可能需要更長的時間。

  • 開源貢獻:用戶可以改進文檔
  • 協作故障排除:依靠社區尋求幫助。

許可、文檔和支持總結

每個庫的許可、文檔和支持總結

結論

When choosing between IronPDF and PDFsharp, the decision largely depends on your project's requirements. IronPDF 是一個強大且功能豐富的解決方案,具備將 HTML 轉換成 PDF、強大加密和強大跨平台支持等高級功能。 它非常適合需要高質量 PDF 操作、安全功能和專門支持的企業、企業和開發人員。

另一方面,PDFsharp 是一個可靠的開源選擇,適用於不需要先進功能的簡單項目,提供易用性和基本的 PDF 創建與編輯的靈活性。

如果您正在尋找一個功能全面的 PDF 解決方案,我們鼓勵您探索 IronPDF。 憑藉其商業支持和持續更新,IronPDF 可隨著您的開發需求擴展,並確保您的 PDF 得到有效且安全的處理。 立即嘗試 IronPDF,發現它如何簡化您的 PDF 工作流程!

請注意PDFsharp是其相應所有者的註冊商標。 此網站與PDFsharp無關,未經PDFsharp贊助或認可。所有產品名稱、徽標和品牌均為其相應所有者的財產。 比較僅供信息參考,並反映撰寫時公開可用的信息。

常見問題解答

怎樣在 C# 中將 HTML 轉換為 PDF?

您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字符串轉換為 PDF。您還可以使用 RenderHtmlFileAsPdf 將 HTML 文件轉換為 PDF。

IronPDF 和 PDFsharp 之間的主要區別是什麼?

IronPDF 是一個功能全面的函式庫,具有高級功能,如 HTML 到 PDF 的轉換、PDF 編輯和安全功能。PDFsharp 更為基本,集中於 PDF 的創建和編輯,缺乏本地的 HTML 到 PDF 轉換功能。

IronPDF 適合跨平台的應用嗎?

是的,IronPDF 支援 Windows、macOS 和 Linux,並與 .NET Core 和 .NET Framework 相容,使其成為跨平台應用程式的理想選擇。

IronPDF 提供哪些安全功能?

IronPDF 提供高級安全功能,包括數字簽名和 256 位加密,適合需要強大文件保護的應用程式。

PDFsharp 能否處理複雜的網頁轉換為 PDF?

否,PDFsharp 本身不支援 HTML 到 PDF 的轉換。它需要第三方工具,例如 Polybioz.HtmlRenderer.PdfSharp.Core,來呈現 HTML 內容。

IronPDF 如何支援開發者?

IronPDF 提供完整的文件、專門的支援團隊和活躍的社區論壇,提供豐富的資源給開發者。

IronPDF 的許可選項有哪些?

IronPDF 使用永久授權模式,提供分層定價、免費試用、年度更新和商業支援。

PDFsharp 是免費的嗎?

是的,PDFsharp 是免費的,並在 MIT 許可下開放源碼,適用於個人和商業用途。

哪一個函式庫在處理 PDF 加密方面提供更好的支援?

IronPDF 提供高級的加密功能,包括 256 位加密和數字簽名,使其適合安全的 PDF 處理。

在 .NET 中進行複雜的 PDF 操作哪個函式庫最適合?

IronPDF 更適合複雜的 PDF 任務,提供高級功能,如 HTML 到 PDF 的轉換、編輯和文件安全。

Curtis Chau
技術作家

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

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