跳至页脚内容
产品比较
使用 IronPDF 作为 PDFSharp 的替代品

PDFSharp HTML 到 PDF 示例和教程比较

在.NET环境中处理PDF文件时,开发人员需要可靠且功能丰富的库来处理诸如文档创建、编辑、加密和HTML-to-PDF转换等常见任务。 IronPDF and PDFsharp是两个流行的库,但它们满足不同的需求。

IronPDF是一个功能全面、高性能的PDF库,擅长于HTML-to-PDF转换、PDF安全性和高级PDF操作。 另一方面,PDFsharp是一个轻量级的开源库,专注于PDF创建和基本编辑。 在这两者之间的选择取决于您的项目的复杂性和要求。

在本指南中,我们将比较IronPDF和PDFsharp在关键特性、可用性、安全性、定价和支持方面的表现,帮助您确定适合您的.NET应用程序的最佳工具。

功能概述

IronPDF:综合的PDF解决方案

IronPDF is a feature-packed .NET library that excels in many areas, including HTML-to-PDF转换、PDF编辑和安全文档处理等多个领域表现卓越。 它可以通过NuGet Package Manager Console轻松添加到您的项目中,并无缝集成到您的C#项目中。

其突出能力包括:

PDFsharp: 轻量级替代品

PDFsharp是一个免费的开源库,主要设计用于基本的PDF创建和操作。 虽然它没有IronPDF那样丰富的功能集,但对于不需要复杂功能的简单项目来说是一个很好的选择。 PDFsharp的一些核心功能包括:

  • PDF创建:使用C#或VB.NET编程生成PDF,创建包含图像和图形的文本文档。
  • PDF修改:简单任务如合并、拆分和编辑现有的PDF。
  • 基本加密:为PDF文档添加基本的密码保护。

关键限制:PDFsharp不原生支持HTML-to-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。: Best for Web Applications & Dynamic Reports

在将HTML报告、网页和动态内容转换为PDF的过程中,IronPDF表现出色,同时保留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。: Best for Web Applications & Dynamic Reports

如您所见,使用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强大的渲染选项将Web内容转换为高质量PDF,保持所有原始CSS和JavaScript内容。

PDFsharp: 最适用于不含Web内容的静态PDF

PDFsharp不原生支持HTML-to-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到PDF示例输出

感谢HtmlRenderer附加组件,我们能够在保持大部分原始样式的同时将大多数HTML内容转换为PDF但当涉及更丰富的CSS内容时,如何表现? 让我们来看看将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。: Best for Secure, Compliance-Driven Applications

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的密码保护是一个简单的解决方案。

然而,PDFsharp的加密能力与IronPDF相比有限,无法用于需要强大安全性的场景。 它可以对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,和应用基本编辑等任务中表现出色。 然而,在涉及复杂操作时,如高级编辑、文档转换或处理表单数据,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 provides comprehensive documentation, including step-by-step guides, API references, and code samples. 提供敬业的支持团队以应对技术问题。 此外,在线知识库提供故障排除解决方案和有用的提示。 对于企业客户,优先支持确保更快的问题解决。

  • 全面文档:详细的指南代码示例
  • 支持团队:可以直接获得技术支持
  • 知识库:包括常见问题和故障排除提示。
  • 优先支持:为企业提供快速问题解决

社群互动:

IronPDF还培养了活跃的开发者社区。 用户可以在社区论坛中交流问题、分享经验和寻找解决方案。 此外,还有教程和视频指南可以帮助用户快速入门。 IronPDF也活跃在GitHub上,社区可以报告错误并建议功能。

  • 社区论坛:作为提出问题和分享解决方案的空间。
  • 教程与指南逐步指导视频资源
  • GitHub协作:用户可以贡献和建议新功能。

PDFsharp开发者支持:

PDFsharp的支持主要来自其GitHub库和像堆栈溢出这样的社区论坛。 开发人员可以报告问题、浏览解决方案或提问。 然而,文档有限且详细指南或官方教程稀缺,开发人员需要依赖社区贡献或摸索。

  • 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工作流程!

[{i:(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 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。