產品比較

PDFSharp HTML 轉換PDF範例與教程比較

介紹

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

IronPDF 是一個功能全面、高性能的 PDF 庫,在 HTML 到 PDF 轉換、PDF 安全性和高級 PDF 操作方面表現出色。 另一方面,PDFsharp 是一個輕量級的開源庫,專注於 PDF 的創建和基本編輯。 選擇它們取決於您專案的複雜性和需求。

在本指南中,我們將比較 IronPDF 和 PDFsharp 在主要功能、可用性、安全性、定價和支援方面的表現,幫助您確定哪一個是您 .NET 應用程式的最佳工具。

功能概述

IronPDF:全面的 PDF 解決方案

IronPDF 是一個功能豐富的 .NET 函式庫,能在包括HTML 到 PDF 轉換、PDF 編輯及安全文件處理等多個領域中表現優異。 您可以使用 NuGet 套件管理器主控台輕鬆地將它新增到您的專案中,輕鬆整合到您的 C# 專案中。

其突出功能包括:

  • HTML 轉 PDF 轉換:將複雜的網頁,包括JavaScriptCSS和 AJAX,轉換為完整渲染的 PDF 文件。
  • PDF 編輯:IronPDF 是操控 PDF 文件的好幫手,您可以通過添加文本、圖像註釋,甚至編輯敏感信息來修改現有的 PDF 文件。
  • PDF 安全性:完全支援數位簽名密碼保護及最高達 256 位元安全性的加密。
  • 表單填寫與擷取:動態填入、擷取並操作PDF 表單

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。 這使它成為需要在多個操作系統上運行的項目的理想選擇,並確保該庫與大多數開發環境具有通用兼容性。 該程式庫能夠無縫整合至現代網頁開發環境,非常適合用於網路應用程式、雲端解決方案和企業系統。

PDFsharp

PDFsharp 主要為 .NET Framework 設計,對 .NET Core 的支援有限。 雖然有 .NET Core 版本,但仍在開發中,某些功能尚未完全針對非 Windows 平台進行優化。 Linux 和 macOS 用戶可能會遇到相容性問題,這使得 PDFsharp 成為跨平台應用程式的一個較不理想的選擇。

易於使用

IronPDF 提供了一個開發者友好的 API,簡化了處理 PDF 文件的複雜性。 無論是生成 PDF 還是編輯現有文件,語法都保持簡潔直觀,讓開發人員僅需幾行代碼即可實現複雜的功能。

PDFsharp 雖然用於基本任務如文字生成和 PDF 操作相對容易,但在進行複雜的文件生成和編輯等任務時,通常需要更多的手動處理。

使用便利示例

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:最適合用於網頁應用程式和動態報告

IronPDF 擅長將 HTML 報告、網頁和動態內容轉換為 PDF,並保留 CSS、JavaScript 和嵌入的圖片。 這使其成為理想選擇用於:

  • 從 HTML 模板生成發票
  • 自動化報告創建 從網路應用程式。
  • 擷取網頁為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:最適合用於網頁應用程式和動態報告

如您所見,使用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:最適合沒有網頁內容的靜態PDF

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

  • 不依賴動態 HTML 渲染的靜態文件。
  • 發票、合約和報告的內容是以程式方式定義的。

    使用 HtmlRenderer 的 PDFsharp 範例:

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-to-PDF 範例輸出

多虧了HtmlRenderer插件,我們能夠將大多數HTML內容轉換為PDF,同時保持大部分原始樣式,但在面對更多CSS密集的內容時,它與IronPDF相比的表現如何呢? 讓我們來看看將網頁內容轉換為 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:最適合沒有網頁內容的靜態PDF

雖然 PDFsharp 無法處理像 IronPDF 一樣密集的 CSS 和 JavaScript 內容,但對於那些尋找輕量級 PDF 庫且 PDF 生成任務不依賴於 CSS 或 JavaScript 內容的人來說,它仍然是一個強大的競爭者。 雖然需要額外的庫來進行 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文件,那麼PDFsharp的密碼保護是一個簡單的解決方案。

    然而,PDFsharp 的加密功能相較於 IronPDF 有所限制,因此在需要強大安全性的情境中並不適用。 它可以使用用戶密碼保護 PDF,並限制某些行為,如列印和複製,但不提供金融或醫療等行業所需的強大加密標準。

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

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

// Create 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 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 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以及應用基本的遮蔽。 然而,當涉及到較為複雜的操作時,例如進階的遮蔽處理、文件轉換,或處理表單數據時,PDFsharp 的能力相較於 IronPDF 更為有限。

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 提供全面的文件,包括分步指南、API 參考資料和程式碼範例。 有專門的支援團隊提供技術諮詢服務。 此外,線上知識庫提供故障排除方案和有用的提示。 對於企業客戶,優先支持可確保更快速地解決問題。

  • 全面文檔:詳細的指南代碼示例
  • 支援團隊:直接存取技術支援
  • 知識庫常見問題和疑難排解技巧。
  • 優先支援:為企業提供快速問題解決

參與社群方面:

IronPDF 還促進了一個活躍的開發者社群。 用戶可以參加社群論壇,在那裡他們可以提出問題、分享經驗和尋找解決方案。 此外,教程和视频指南可以帮助用户快速入门。 IronPDF 也在 GitHub 上,允許社群報告錯誤和建議功能。

  • 社群論壇:一個可以提出問題和分享解決方案的空間。
  • 教程和指南逐步指引视频资源
  • GitHub 協作:用戶可以貢獻並建議新功能。

PDFsharp 開發人員支持:

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

  • GitHub Repository:報告問題並探索解決方案。
  • 社群論壇:在 Stack Overflow 上尋求幫助。
  • 稀疏文件:有限的指南和資源。

社群主導的貢獻:

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

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

授權、文件及支援摘要

每個庫的許可、文件和支援總結

結論

在選擇 IronPDFPDFsharp 之間時,該決定主要取決於您的專案需求。 IronPDF 是一款功能強大且特性豐富的解決方案,具有先進的功能,例如 HTML-to-PDF 轉換、強大的加密功能以及強大的跨平台支持。 它非常適合需要高品質 PDF 操作、安全功能和專業支持的企業、公司和開發人員。

另一方面,PDFsharp 是一個穩固的開源選擇,適合不需要高級功能的簡單項目,具備易於使用及靈活性,提供基本的 PDF 創建和編輯功能。

如果您正在尋找具有廣泛功能的全方位 PDF 解決方案,我們鼓勵您探索 IronPDF。 IronPDF具備商業支持和持續更新,專為滿足您的開發需求而構建,確保您的PDF能夠高效且安全地處理。 立即嘗試 IronPDF,發現如何簡化您的 PDF 工作流程!

Chipego
奇佩戈·卡林达
軟體工程師
Chipego 擁有天生的傾聽技能,這幫助他理解客戶問題,並提供智能解決方案。他在獲得信息技術理學學士學位後,于 2023 年加入 Iron Software 團隊。IronPDF 和 IronOCR 是 Chipego 專注的兩個產品,但隨著他每天找到新的方法來支持客戶,他對所有產品的了解也在不斷增長。他喜歡在 Iron Software 的協作生活,公司內的團隊成員從各自不同的經歷中共同努力,創造出有效的創新解決方案。當 Chipego 離開辦公桌時,他常常享受讀好書或踢足球的樂趣。
< 上一頁
SpirePDF C# HTML轉PDF教學與庫比較
下一個 >
Accusoft Prizmdoc PDF Viewer 教程和比較