产品比较

PDFSharp HTML 到 PDF 示例和教程比较

发布 2024年八月11日
分享:

介绍

在.NET中管理PDF文档时,开发人员可以选择多种库,每种库都提供独特的功能和能力。 在这些当中, IronPDF 和PDFsharp作为突出的选择,迎合不同的需求和偏好。

在本文中,我们将更深入地探讨这些库所提供的各种功能,以及它们各自的支持、定价、文档和兼容性。

IronPDF和PDFsharp概述

一个功能强大的.NET PDF库,IronPDF使开发者能够轻松创建、编辑和提取PDF内容。 用户可以使用IronPDF将多种格式,如HTML、ASPX、MVC和图像,转换为PDF文档。 IronPDF 具有多种功能,例如文本编辑、图像操作和元数据更改、添加水印以及加密PDF文件等。 它以易用的界面和令人印象深刻的功能集而闻名,这使得开发者更容易处理PDF文档。

PDFsharp 是一个基于 .NET 的开源库,专注于动态生成 PDF 文档。 此产品完全用C#实现,支持绘制文本、图形和图像。 它还允许对现有PDF文件进行合并或拆分等修改操作。 PDFsharp 适用于寻找一个基础库来操作简单 PDF 文件的人,而不是那些需要许多额外功能的库。

跨平台兼容性

IronPDF 和 PDFsharp 都在 .NET 环境中操作,从版本6.1开始,PDFsharp 支持 .NET Framework 4.7.2 或更高版本、.NET Standard 2.0 和 .NET 6 或更高版本,并且可以在兼容 .NET 的平台上运行,如 Linux、macOS 以及 Windows。

IronPDF 支持与各种 .NET 版本的最新平台兼容性,允许开发人员在 Windows、Linux、Mac、Azure、Docker 和 AWS 等应用环境中工作,从而确保与许多操作系统和开发环境的通用兼容性。

IronPDF:

  • .NET 版本:

    请注意,您提供的内容是空的。请提供要翻译的文本。 (C#, VB.NET, F#)

    • .NET Core (8、7、6、5 和 3.1+)

    • .NET标准 (2.0+)
  • .NET 框架 (4.6.2+)
  • .NET 项目类型: 支持包括 Blazor、MAUI、WPF、控制台应用等多种项目类型。
  • 应用环境: IronPDF支持包括Windows、Linux、Mac、Docker、Azure和AWS在内的应用环境。
  • IDEs(集成开发环境): 适用于Microsoft Visual Studio和JetBrains Rider & ReSharper等IDEs。

PDFsharp

  • .NET 版本:

    • .NET 框架 (4.7.2+)

    • .NET标准 (2.0+)
  • .NET 6+
  • .NET项目类型: 可以与Blazor、MAUI和ASP.NET等项目类型和技术配合使用。

  • 应用环境: PDFsharp 支持在 Windows、macOS 和 Linux 中工作的项目。

功能比较概览:IronPDF 与 PDFsharp

IronPDF 提供了多种处理PDF文件的功能,例如生成、修改和提取PDF内容,将HTML、ASPX、MVC和图片转换成PDF格式,以及编辑文本、图片和元数据; 将水印应用到您的PDF页面或加密PDF文件。

另一方面,PDFsharp的重点是创建和操作PFD文档; 它允许您在页面上绘制文本行或段落,或者插入矩形和圆形等图形。

IronPDF

  • HTML 转 PDF 转换: IronPDF 支持现代网页标准 (CSS3, HTML5, JavaScript),允许高保真度PDF文档创建。
  • PDF加密: 用户可以使用IronPDF的强大加密工具来加密和解密PDF文件,为他们的PDF文件增加额外的安全层。
  • PDF编辑:IronPDF库包括合并、分割、格式化和修改现有PDF文件的功能。
  • PDF文件的数字签名: IronPDF允许用户对其PDF文件进行数字签名。
  • 水印添加:轻松地在PDF文件中添加文本和图片水印; 利用它使用HTML/CSS来完全控制该过程。
  • PDF盖章: 使用IronPDF在您的PDF文档上盖章图片和文字。

PDFsharp:

  • 创建 PDFs: PDFsharp 可以从任何 .NET 语言创建和生成 PDF 文件。
  • PDF操作: 修改、合并和分割PDF文件
  • 在您的PDF文件上绘制图形: PDFsharp具有在PDF文档上绘制形状、线条和图像的能力。
  • 加密: 使用PDFsharp的加密工具对您的PDF文件进行加密。

IronPDF与PDFsharp的功能比较

将 HTML 转换为 PDF

在各种工作场所和环境中,转换 HTML 转 PDF 是一个简单但必要的任务; 以下代码示例中展示了IronPDF与PDFsharp在此过程中的比较。

IronPDF:

using IronPdf;
// Disable local disk access or cross-origin requests
Installation.EnableWebSecurity = true;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Export to a file or Stream
pdf.SaveAs("output.pdf");
// Advanced Example with HTML Assets
// Load external html assets: images, CSS and JavaScript.
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");
using IronPdf;
// Disable local disk access or cross-origin requests
Installation.EnableWebSecurity = true;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Export to a file or Stream
pdf.SaveAs("output.pdf");
// Advanced Example with HTML Assets
// Load external html assets: images, CSS and JavaScript.
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

PDFsharp:

PDFsharp 本身无法处理 HTML 到 PDF 的转换; 相反,它需要使用HtmlRenderer.PdfSharp库来完成这个任务。 此HTML渲染器可以使用静态渲染代码处理HTML到PDF的转换。

using System.IO;
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
static void Main(string[] args)
{
    string html = @"<h1>Hello World</h1>";
    PdfDocument pdf = PdfGenerator.GeneratePdf(html, PageSize.A4);
    const string filename = "HtmlToPdfExample.pdf";
    pdf.Save(filename);
}
using System.IO;
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
static void Main(string[] args)
{
    string html = @"<h1>Hello World</h1>";
    PdfDocument pdf = PdfGenerator.GeneratePdf(html, PageSize.A4);
    const string filename = "HtmlToPdfExample.pdf";
    pdf.Save(filename);
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

IronPDF 提供了一种方便简单的方法,将 HTML 代码、HTML 片段或 HTML 模板转换为 PDF 格式,同时允许用户拥有充分的控制权。 现代网络标准得到了IronPDF的良好支持,它可以创建与源网页完全相符的PDF文件。

另一方面,PDFsharp 不支持 HTML 到 PDF 的转换,也不具备解析 HTML 文件的能能力; 相反,可以将HtmlRenderer.PdfSharp库与PDFsharp一起使用来完成这项任务。

在PDF文件中编辑文本

有时,用户可能想要 编辑 在处理私人或敏感信息时,对PDF文件的某些部分进行操作。

IronPDF:

using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("novel.pdf");
// Redact the 'are' phrase from all pages
pdf.RedactTextOnAllPages("are");
pdf.SaveAs("redacted.pdf");
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("novel.pdf");
// Redact the 'are' phrase from all pages
pdf.RedactTextOnAllPages("are");
pdf.SaveAs("redacted.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

PDFsharp:

PDFsharp 目前不提供任何内置功能用于编辑PDF内容; 然而,由于涂改涉及永久删除或隐藏敏感信息,您可以尝试使用 PDFsharp 的图形绘图工具手动绘制覆盖您希望涂改的内容,以达到类似的效果。 以下是一个示例,说明如何实现这一点:

using System;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
namespace PdfRedactionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load an existing PDF document
            PdfDocument document = PdfReader.Open("input.pdf", PdfDocumentOpenMode.Modify);
            // Get the first page of the document
            PdfPage page = document.Pages[0];
            // Create a graphics object to write on the page
            XGraphics gfx = XGraphics.FromPdfPage(page);
            // Define the area to redact (for example, a rectangle covering text)
            XRect redactRect = new XRect(100, 100, 200, 50); // Adjust coordinates and size as needed
            // Fill the redaction area with a black rectangle
            gfx.DrawRectangle(XBrushes.Black, redactRect);
            // Save the modified document
            document.Save("output.pdf");
            // Optionally, close the document
            document.Close();
        }
    }
}
using System;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
namespace PdfRedactionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load an existing PDF document
            PdfDocument document = PdfReader.Open("input.pdf", PdfDocumentOpenMode.Modify);
            // Get the first page of the document
            PdfPage page = document.Pages[0];
            // Create a graphics object to write on the page
            XGraphics gfx = XGraphics.FromPdfPage(page);
            // Define the area to redact (for example, a rectangle covering text)
            XRect redactRect = new XRect(100, 100, 200, 50); // Adjust coordinates and size as needed
            // Fill the redaction area with a black rectangle
            gfx.DrawRectangle(XBrushes.Black, redactRect);
            // Save the modified document
            document.Save("output.pdf");
            // Optionally, close the document
            document.Close();
        }
    }
}
Imports System
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Namespace PdfRedactionExample
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Load an existing PDF document
			Dim document As PdfDocument = PdfReader.Open("input.pdf", PdfDocumentOpenMode.Modify)
			' Get the first page of the document
			Dim page As PdfPage = document.Pages(0)
			' Create a graphics object to write on the page
			Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
			' Define the area to redact (for example, a rectangle covering text)
			Dim redactRect As New XRect(100, 100, 200, 50) ' Adjust coordinates and size as needed
			' Fill the redaction area with a black rectangle
			gfx.DrawRectangle(XBrushes.Black, redactRect)
			' Save the modified document
			document.Save("output.pdf")
			' Optionally, close the document
			document.Close()
		End Sub
	End Class
End Namespace
VB   C#

在PDF文件中编辑内容时,IronPDF 提供了一种直接从PDF文档中编辑文本的简单方法。 用户可以轻松地指定要在所有页面上编辑的文本,并且其直观简洁的API使得这项任务可以迅速高效地完成。

PDFsharp 目前不支持内置的编辑功能。 然而,用户可以利用其图形绘制功能手动叠加内容以进行编辑,尽管这种方法需要更多的手动干预和定制。

对PDF文件进行数字签名

如果您需要 签署PDF文档 数字化的过程可能非常耗时。 一种节省时间的方法是通过编程来实现。 在下面的代码示例中,我们将比较 IronPDF 和 PDFsharp 之间的文档签名工作方式。

IronPDF:

using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;
// Create X509Certificate2 object with X509KeyStorageFlags set to Exportable
X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);
// Create PdfSignature object
var sig = new PdfSignature(cert);
// Sign PDF document
PdfDocument pdf = PdfDocument.FromFile("document.pdf");
pdf.Sign(sig);
pdf.SaveAs("signed.pdf");
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;
// Create X509Certificate2 object with X509KeyStorageFlags set to Exportable
X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);
// Create PdfSignature object
var sig = new PdfSignature(cert);
// Sign PDF document
PdfDocument pdf = PdfDocument.FromFile("document.pdf");
pdf.Sign(sig);
pdf.SaveAs("signed.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports System.Security.Cryptography.X509Certificates
' Create X509Certificate2 object with X509KeyStorageFlags set to Exportable
Private cert As New X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable)
' Create PdfSignature object
Private sig = New PdfSignature(cert)
' Sign PDF document
Private pdf As PdfDocument = PdfDocument.FromFile("document.pdf")
pdf.Sign(sig)
pdf.SaveAs("signed.pdf")
VB   C#

PDFsharp:

PDFsharp没有内置支持用于电子签名PDF文件的功能,而是依赖于外部库如iTextSharp来完成这个任务。

通过编程方式对 PDF 文档进行数字签名可以极大地简化工作流程。 IronPDF 内置了对数字签名的支持,使得过程简单直接,只需几行代码就可以完成任务。 然而,PDFsharp 没有内置的数字签名功能,因此需要使用第三方库,如 iTextSharp,这进一步复杂化了实现。

应用 PDF 水印

在处理涉及敏感记录的私人文件、版权保护、品牌标识或任何其他任务时; 应用和个性化的能力 水印 处理PDF文件可能非常有用。 在这篇文章中,我们比较了IronPDF和PDFsharp在向PDF文档添加水印时的表现。

IronPDF:

using IronPdf;
// Stamps a Watermark onto a new or existing PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);
pdf.SaveAs(@"C:\Path\To\Watermarked.pdf");
using IronPdf;
// Stamps a Watermark onto a new or existing PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center);
pdf.SaveAs(@"C:\Path\To\Watermarked.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

PDFsharp:

using PdfSharp.Pdf;
using PdfSharp.Drawing;
PdfDocument document = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify);
XGraphics gfx = XGraphics.FromPdfPage(document.Pages[0]);
XFont font = new XFont("Verdana", 40, XFontStyle.BoldItalic);
XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
string watermarkText = "SAMPLE";
XSize textSize = gfx.MeasureString(watermarkText, font);
XPoint center = new XPoint((document.Pages[0].Width - textSize.Width) / 2, (document.Pages[0].Height - textSize.Height) / 2);
gfx.DrawString(watermarkText, font, brush, center, XStringFormats.Center);
// Save the document
document.Save("Watermarked.pdf");
using PdfSharp.Pdf;
using PdfSharp.Drawing;
PdfDocument document = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify);
XGraphics gfx = XGraphics.FromPdfPage(document.Pages[0]);
XFont font = new XFont("Verdana", 40, XFontStyle.BoldItalic);
XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
string watermarkText = "SAMPLE";
XSize textSize = gfx.MeasureString(watermarkText, font);
XPoint center = new XPoint((document.Pages[0].Width - textSize.Width) / 2, (document.Pages[0].Height - textSize.Height) / 2);
gfx.DrawString(watermarkText, font, brush, center, XStringFormats.Center);
// Save the document
document.Save("Watermarked.pdf");
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Private document As PdfDocument = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify)
Private gfx As XGraphics = XGraphics.FromPdfPage(document.Pages(0))
Private font As New XFont("Verdana", 40, XFontStyle.BoldItalic)
Private brush As XBrush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))
Private watermarkText As String = "SAMPLE"
Private textSize As XSize = gfx.MeasureString(watermarkText, font)
Private center As New XPoint((document.Pages(0).Width - textSize.Width) \ 2, (document.Pages(0).Height - textSize.Height) \ 2)
gfx.DrawString(watermarkText, font, brush, center, XStringFormats.Center)
' Save the document
document.Save("Watermarked.pdf")
VB   C#

向PDF文档添加水印对于涉及隐私、版权保护和品牌推广的任务至关重要。 IronPDF 提供了一个简易的 API,允许用户使用 HTML/CSS 高效地应用自定义水印,既提供了灵活性,又便于实施,同时也让用户对过程拥有充分的控制权。 另一方面,PDFsharp 需要更多的手动操作,使用图形和文本绘制方法将水印覆盖到PDF页面上。

DOCX 转 PDF 文件转换

将不同文件类型转换为PDF在创建PDF文件时至关重要。 对于此实例,我们将专注于 DOCX文件类型 并比较IronPDF和PDFsharp如何完成这个任务。

IronPDF:

using IronPdf;
// Instantiate Renderer
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
// Render from DOCX file
PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx");
// Save the PDF
pdf.SaveAs("pdfFromDocx.pdf");
using IronPdf;
// Instantiate Renderer
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
// Render from DOCX file
PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx");
// Save the PDF
pdf.SaveAs("pdfFromDocx.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

PDFsharp:

PDFsharp 目前不支持将 DOCX 直接转换为 PDF 文件格式。 相反,如果用户希望执行此任务,他们将不得不使用诸如DocX之类的外部库。

在 DOCX 到 PDF 的转换方面,IronPDF 提供了一种简单明了的方法,通过其 DocxToPdfRenderer 直接支持 DOCX 到 PDF 的转换。 这样可以无缝地渲染和保存生成的PDF。

PDFsharp 不支持原生的 DOCX 转 PDF 转换; 用户必须依赖像DocX这样的外部库来实现这个功能。

在PDF上加盖图像和文字

有时,例如在添加水印时,PDF页面可能需要被 盖章 带有某物的。 在本节中,我们将探讨IronPDF与PDFsharp在PDF文档上的盖章功能。

IronPDF:

using IronPdf;
using IronPdf.Editing;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");
// Create text stamper
TextStamper textStamper = new TextStamper()
{
    Text = "Text Stamper!",
    FontFamily = "Bungee Spice",
    UseGoogleFont = true,
    FontSize = 30,
    IsBold = true,
    IsItalic = true,
    VerticalAlignment = VerticalAlignment.Top,
};
// Stamp the text stamper
pdf.ApplyStamp(textStamper);
pdf.SaveAs("stampText.pdf");
// Create image stamper
ImageStamper imageStamper = new ImageStamper(new Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg"))
{
    VerticalAlignment = VerticalAlignment.Top,
};
// Stamp the image stamper
pdf.ApplyStamp(imageStamper, 0);
pdf.SaveAs("stampImage.pdf");
using IronPdf;
using IronPdf.Editing;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");
// Create text stamper
TextStamper textStamper = new TextStamper()
{
    Text = "Text Stamper!",
    FontFamily = "Bungee Spice",
    UseGoogleFont = true,
    FontSize = 30,
    IsBold = true,
    IsItalic = true,
    VerticalAlignment = VerticalAlignment.Top,
};
// Stamp the text stamper
pdf.ApplyStamp(textStamper);
pdf.SaveAs("stampText.pdf");
// Create image stamper
ImageStamper imageStamper = new ImageStamper(new Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg"))
{
    VerticalAlignment = VerticalAlignment.Top,
};
// Stamp the image stamper
pdf.ApplyStamp(imageStamper, 0);
pdf.SaveAs("stampImage.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

PDFsharp:

using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Drawing;
// Load an existing PDF document
PdfDocument document = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify);
// Iterate through each page in the document
 foreach (PdfPage page in document.Pages)
 {
    // Get an XGraphics object for drawing on the page
    XGraphics gfx = XGraphics.FromPdfPage(page);
    // Define a font and brush for stamping text
    XFont font = new XFont("Arial", 12);
    XBrush brush = XBrushes.Red;
     // Stamp text onto the page
     gfx.DrawString("Confidential", font, brush, 50, 50);
     // Stamp an image onto the page
     XImage image = XImage.FromFile("stamp.png"); // Replace with your image path
     gfx.DrawImage(image, 100, 100, image.PixelWidth / 2, image.PixelHeight / 2);
}
document.Save("StampedOutput.pdf");
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Drawing;
// Load an existing PDF document
PdfDocument document = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify);
// Iterate through each page in the document
 foreach (PdfPage page in document.Pages)
 {
    // Get an XGraphics object for drawing on the page
    XGraphics gfx = XGraphics.FromPdfPage(page);
    // Define a font and brush for stamping text
    XFont font = new XFont("Arial", 12);
    XBrush brush = XBrushes.Red;
     // Stamp text onto the page
     gfx.DrawString("Confidential", font, brush, 50, 50);
     // Stamp an image onto the page
     XImage image = XImage.FromFile("stamp.png"); // Replace with your image path
     gfx.DrawImage(image, 100, 100, image.PixelWidth / 2, image.PixelHeight / 2);
}
document.Save("StampedOutput.pdf");
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf
Imports System.Drawing
' Load an existing PDF document
Private document As PdfDocument = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify)
' Iterate through each page in the document
 For Each page As PdfPage In document.Pages
	' Get an XGraphics object for drawing on the page
	Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
	' Define a font and brush for stamping text
	Dim font As New XFont("Arial", 12)
	Dim brush As XBrush = XBrushes.Red
	 ' Stamp text onto the page
	 gfx.DrawString("Confidential", font, brush, 50, 50)
	 ' Stamp an image onto the page
	 Dim image As XImage = XImage.FromFile("stamp.png") ' Replace with your image path
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
	 gfx.DrawImage(image, 100, 100, image.PixelWidth / 2, image.PixelHeight / 2)
 Next page
document.Save("StampedOutput.pdf")
VB   C#

IronPDF 使用户能够轻松地向PDF文档添加文本和图像,同时保持过程简单明了。 它让用户完全掌控整个过程的所有方面,特别是对于熟悉HTML/CSS的用户。

另一方面,PDFsharp 需要使用其 XGraphics 对象进行更多的手动处理,其中文本和图像使用特定坐标和图形参数直接盖章到每一页上。

加密 PDF 文件

无论是在办公室、学校还是图书馆, 加密和解密 PDF 文档是许多工作环境的一部分。 因此,拥有一个能够无障碍执行此功能的程序是非常重要的。 本节将比较IronPDF和PDFsharp如何处理PDF加密任务。

IronPDF:

using IronPdf;
using System;
// Open an Encrypted File, alternatively create a new PDF from Html
var pdf = PdfDocument.FromFile("encrypted.pdf", "password");
// Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto";
pdf.MetaData.Keywords = "SEO, Friendly";
pdf.MetaData.ModifiedDate = DateTime.Now;
// Edit file security settings
// The following code makes a PDF read only and will disallow copy & paste and printing
pdf.SecuritySettings.RemovePasswordsAndEncryption();
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key");
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserFormData = false;
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;
// change or set the document encryption password
pdf.Password = "my-password";
pdf.SaveAs("secured.pdf");
using IronPdf;
using System;
// Open an Encrypted File, alternatively create a new PDF from Html
var pdf = PdfDocument.FromFile("encrypted.pdf", "password");
// Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto";
pdf.MetaData.Keywords = "SEO, Friendly";
pdf.MetaData.ModifiedDate = DateTime.Now;
// Edit file security settings
// The following code makes a PDF read only and will disallow copy & paste and printing
pdf.SecuritySettings.RemovePasswordsAndEncryption();
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key");
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserFormData = false;
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;
// change or set the document encryption password
pdf.Password = "my-password";
pdf.SaveAs("secured.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

PDFsharp:

PDFsharp 本身不支持 PDF 加密功能。 然而,用户可以安装 PdfSharp.Pdf.IO 库来帮助完成这项任务。

using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Security;
PdfDocument document = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify);
string userPassword = "userpassword";
string ownerPassword = "ownerpassword";
PdfSecuritySettings securitySettings = document.SecuritySettings;
securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted128Bit;
securitySettings.UserPassword = userPassword;
securitySettings.OwnerPassword = ownerPassword;
securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotationsAndFieldsEdit = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = true;
securitySettings.PermitFullQualityPrint = true;
securitySettings.PermitModifyDocument = true;
securitySettings.PermitPrint = true;
document.Save("EncryptedOutput.pdf");
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Security;
PdfDocument document = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify);
string userPassword = "userpassword";
string ownerPassword = "ownerpassword";
PdfSecuritySettings securitySettings = document.SecuritySettings;
securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted128Bit;
securitySettings.UserPassword = userPassword;
securitySettings.OwnerPassword = ownerPassword;
securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotationsAndFieldsEdit = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = true;
securitySettings.PermitFullQualityPrint = true;
securitySettings.PermitModifyDocument = true;
securitySettings.PermitPrint = true;
document.Save("EncryptedOutput.pdf");
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports PdfSharp.Pdf.Security
Private document As PdfDocument = PdfReader.Open("Input.pdf", PdfDocumentOpenMode.Modify)
Private userPassword As String = "userpassword"
Private ownerPassword As String = "ownerpassword"
Private securitySettings As PdfSecuritySettings = document.SecuritySettings
securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted128Bit
securitySettings.UserPassword = userPassword
securitySettings.OwnerPassword = ownerPassword
securitySettings.PermitAccessibilityExtractContent = False
securitySettings.PermitAnnotationsAndFieldsEdit = False
securitySettings.PermitAssembleDocument = False
securitySettings.PermitExtractContent = False
securitySettings.PermitFormsFill = True
securitySettings.PermitFullQualityPrint = True
securitySettings.PermitModifyDocument = True
securitySettings.PermitPrint = True
document.Save("EncryptedOutput.pdf")
VB   C#

IronPDF 是一个功能强大的工具,具有强大的加密功能,可让您轻松加密PDF文件并管理安全设置,如只读访问权限、复制粘贴限制和打印权限。 您还可以为加密文件添加密码保护并自定义元数据; 简而言之,IronPDF 是一个全面的安全 PDF 管理解决方案。

PDFsharp 没有任何原生支持加密功能,但它可以使用第三方库比如 PdfSharp.Pdf.IO 来执行这个功能。 您可以设置不同级别的加密,创建用户或所有者密码,并定义打印权限或内容提取等文档操作的权限。

代码示例比较摘要

定价与授权:IronPDF与PDFsharp

IronPDF 定价和许可

IronPDF 拥有不同级别和购买许可证的附加功能。 开发者也可以购买 Iron Suite 它使您可以仅以两个产品的价格访问IronSoftware的所有产品。 如果您尚未准备购买许可证,IronPDF提供了一种 免费试用 持续30天。

  • 永久许可证: 根据您团队的规模、项目需求和地点数量提供一系列永久许可证。 每种许可类型都包括电子邮件支持。
  • Lite License:此许可证价格为749美元,支持一个开发者、一个地点和一个项目。
  • Plus License(加强许可): 支持三名开发者、三个地点和三个项目,这是从轻量版许可升级的下一步,费用为$1,499。Plus许可还提供聊天支持和电话支持,除了基本的电子邮件支持。
  • 专业许可证: 此许可证适用于较大的团队,支持10名开发者、10个位置和10个项目,价格为2999美元。它提供与之前等级相同的联系支持渠道,但也提供屏幕共享支持。
  • 免版税重新分发: IronPDF 的许可还提供额外 $1,999 的免版税重新分发覆盖。
  • 持续不断的产品支持: IronPDF为用户提供持续的产品更新、安全功能升级以及他们工程团队的支持,费用为每年999美元或一次性支付1999美元,享受5年的服务。

  • IronSuite:花费 $1,498,您将获得所有Iron Software产品的访问权,包括IronPDF、IronOCR、IronWord、IronXL、IronBarcode、IronQR、IronZIP、IronPrint和IronWebScraper。

PDFsharp 许可证

PDFsharp 是一个开源库,免费使用。 然而,与IronPDF等竞争对手相比,这会带来功能不够先进和强大的代价。

文档和支持:IronPDF 与 PDFsharp

IronPDF 提供了广泛的文档和支持选项,确保开发人员可以轻松地集成和使用其功能。

IronPDF

  • 全面的文档:涵盖所有功能的广泛且用户友好的文档。
  • 24/5 支持:提供活跃的工程师支持。
  • 视频教程:详细的视频指南可在YouTube上找到。
  • 社区论坛:为了提供额外支持而设立的活跃社区。

  • 定期更新:每月产品更新,确保具备最新功能和安全补丁。

PDFsharp

  • 文档: 网站上提供了基本文档。
  • 论坛: 通过论坛实现社区驱动支持。

  • 支持:没有商业支持选项,但有一个用于报告问题的仓库。

    有关IronPDF文档和支持的更多详情,请访问 IronPDF文档IronSoftware YouTube频道.

结论

总之,在.NET环境中选择IronPDF和PDFsharp进行PDF文档管理时,开发者应考虑他们的具体需求和项目要求。 IronPDF以其广泛的功能集而出色,其中包括HTML转PDF转换、加密功能以及对各种.NET平台和开发环境的强大支持。 它提供全面的文档、多个支持渠道和灵活的许可选项,尽管需要付费,但可以通过NuGet包管理器轻松安装到您的项目中。

另一方面,PDFsharp 作为一个开源库,可以免费使用,但缺乏像原生支持 HTML 到 PDF 转换和 PDF 加密这样的高级功能。 它可能需要额外的库,并且缺乏商业支持选项,因此主要适用于基本的PDF处理任务。

< 前一页
SpirePDF C# HTML转PDF教程及库对比
下一步 >
Accusoft Prizmdoc PDF 查看器教程和比较

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 10,731,156 查看许可证 >