在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
在 .NET 中管理 PDF 文档时,开发人员有多种库可供选择,每个库都提供独特的功能和能力。在这些库中, IronPDF 和PDFsharp作为突出的选项,迎合了不同的需求和偏好。
在本文中,我们将仔细看看这些库所提供的各种功能,以及它们各自的支持、定价、文档和兼容性。
一个强大的 .NET PDF 库,IronPDF 使开发人员能够轻松创建、编辑和提取 PDF 内容。用户可以使用 IronPDF 将各种格式(如 HTML、ASPX、MVC 和图像)转换为 PDF 文档。 IronPDF 具有多种功能,如文本编辑、图像处理和元数据更改、水印添加和PDF文件加密等。它以易于使用的界面和令人印象深刻的功能集而闻名,使开发人员更容易处理PDF文档。
PDFsharp是一个基于.NET的开源库,专注于PDF文档的动态生成。该产品完全用C#实现,支持绘制文本、图形和图像。它还允许进行如合并或拆分现有PDF文件的修改操作。PDFsharp适合那些寻求用于简单PDF文件操作的基本库,而不是包含许多额外功能的工具。
IronPDF 和 PDFsharp 都在 .NET 环境下运行。PDFsharp 从 6.1 版本开始支持 .NET Framework 4.7.2 或更高版本、.NET Standard 2.0 和 .NET 6 或更高版本,并且可以在 .NET 兼容的平台上运行,如 Linux 和 macOS,以及 Windows。
IronPDF 支持与各种 .NET 版本的最新平台兼容性,允许开发人员在 Windows、Linux、Mac、Azure、Docker 和 AWS 等应用环境中工作,从而确保与许多操作系统和开发环境的通用兼容性。
.NET 版本:
(C#, VB.NET, F#)
.NET Core (8、7、6、5 和 3.1+)
.NET 版本:
.NET Framework (4.7.2+)
IronPDF 提供多种处理PDF文件的功能,如生成、修改和从PDF中提取内容,将HTML、ASPX、MVC和图像转换为PDF格式,编辑文本、图像和元数据;还可以在PDF页面上应用水印或对PDF文件进行加密。
另一方面,PDFsharp的重点在于创建和操作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");
Imports IronPdf
' Disable local disk access or cross-origin requests
Installation.EnableWebSecurity = True
' Instantiate Renderer
Dim renderer = New ChromePdfRenderer()
' Create a PDF from a HTML string using C#
Dim 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.
Dim myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", "C:\site\assets\")
myAdvancedPdf.SaveAs("html-with-assets.pdf")
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);
}
Imports System.IO
Imports PdfSharp.Pdf
Imports TheArtOfDev.HtmlRenderer.PdfSharp
Shared Sub Main(ByVal args() As String)
Dim html As String = "<h1>Hello World</h1>"
Dim pdf As PdfDocument = PdfGenerator.GeneratePdf(html, PageSize.A4)
Const filename As String = "HtmlToPdfExample.pdf"
pdf.Save(filename)
End Sub
IronPDF 提供了一种便捷而简单的方式,将HTML代码、HTML片段或HTML模板转换为PDF格式,同时允许用户拥有大量控制权。IronPDF很好地支持现代网页标准,能够创建与源网页一致的PDF文件。
另一方面,PDFsharp 目前不支持HTML到PDF的转换,也没有解析HTML文件的能力;相反,可以与PDFsharp一起使用HtmlRenderer.PdfSharp库来完成此任务。
有时,用户可能会想要 编辑 某些部分的 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");
Imports IronPdf
Private pdf As PdfDocument = PdfDocument.FromFile("novel.pdf")
' Redact the 'are' phrase from all pages
pdf.RedactTextOnAllPages("are")
pdf.SaveAs("redacted.pdf")
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
在处理 PDF 文件中的内容编辑时,IronPDF 提供了一种直接从 PDF 文档中编辑文本的简便方法。用户可以轻松指定需要编辑的文本遍布所有页面,其直观简洁的 API 使任务得以快速高效地完成。
相比之下,PDFsharp 目前不支持内置编辑功能。然而,用户可以利用其图形绘制功能手动覆盖内容进行编辑,尽管这种方法需要更多的手动干预和自定义。
如果您需要 签署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")
PDFsharp:
PDFsharp 没有内置支持数字签名PDF文件的功能,而是依赖于外部库如iTextSharp来完成此任务。
以编程方式对PDF文档进行数字签名可以大大简化工作流程。IronPDF 具有内置的数字签名支持,使整个过程变得简单明了,只需要几行代码即可完成。然而,PDFsharp 没有内置的数字签名功能,因此需要使用iTextSharp等第三方库,这会使实现变得更加复杂。
在处理私密文件、版权保护、品牌推广或任何涉及敏感记录的任务时;具备应用和个性化 水印 在 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");
Imports IronPdf
' Stamps a Watermark onto a new or existing PDF
Private renderer = New ChromePdfRenderer()
Private 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")
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")
为PDF文档添加水印对于涉及隐私、版权保护和品牌推广的任务至关重要。IronPDF 提供了一个简单的API,允许用户使用HTML/CSS高效地应用自定义水印,提供了实现的灵活性和易用性,同时也给用户足够的控制权。另一方面,PDFsharp 需要更多手动处理,使用图形和文本绘制方法将水印覆盖到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");
Imports IronPdf
' Instantiate Renderer
Private renderer As New DocxToPdfRenderer()
' Render from DOCX file
Private pdf As PdfDocument = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx")
' Save the PDF
pdf.SaveAs("pdfFromDocx.pdf")
PDFsharp:
PDFsharp目前不支持将DOCX直接转换为PDF文件格式。如果用户希望进行此任务,他们需要使用诸如DocX之类的外部库。
在DOCX到PDF转换方面,IronPDF提供了一个简单明了的方法,通过其DocxToPdfRenderer直接支持DOCX到PDF的转换。这使得渲染和保存生成的PDF变得无缝。
PDFsharp本身不支持DOCX到PDF的转换;用户必须依赖于像DocX这样的外部库来实现该功能。
有时,如在应用水印时,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");
Imports IronPdf
Imports IronPdf.Editing
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>")
' Create text stamper
Private textStamper As New TextStamper() With {
.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
Dim imageStamper As New ImageStamper(New Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg")) With {.VerticalAlignment = VerticalAlignment.Top}
' Stamp the image stamper
pdf.ApplyStamp(imageStamper, 0)
pdf.SaveAs("stampImage.pdf")
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")
IronPDF 使用户能够轻松地向PDF文档添加文本和图像,同时保持过程简便易行。它赋予用户对过程各方面的完全控制,特别适合熟悉HTML/CSS的用户。
另一方面,PDFsharp 需要更多的手动处理,其XGraphics对象将文本和图像根据具体的坐标和图形参数直接盖在每一页上。
无论是在办公室、学校还是图书馆, 加密和解密 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");
Imports IronPdf
Imports System
' Open an Encrypted File, alternatively create a new PDF from Html
Private 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")
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")
IronPDF 是一个强大的工具,具有强加密功能,可以让您轻松加密 PDF 并管理安全设置,如只读访问、复制粘贴限制和打印权限。您还可以为加密文件添加密码保护并自定义元数据;总之,IronPDF 是一个全面的安全 PDF 管理解决方案。
PDFsharp 没有任何原生的加密支持,但它可以使用例如 PdfSharp.Pdf.IO 这样的第三方库来执行这一功能。您可以设置不同级别的加密,创建用户或所有者密码,并定义打印权利或内容提取等其他文档操作的权限。
IronPDF 有不同等级和额外功能的许可证供购买。开发者还可以购买 Iron Suite 这将让您以两个产品的价格获取所有IronSoftware的产品。如果您还没准备好购买许可证,IronPDF提供一个 免费试用 有效期为30天。
PDFsharp 是一个开源库,可以免费使用。然而,这也意味着其功能比竞争对手如 IronPDF 提供的要少且不够先进。
IronPDF 提供了广泛的文档和支持选项,确保开发人员可以轻松地集成和利用其功能。
有关IronPDF文档和支持的更多详细信息,请访问 IronPDF文档 和 IronSoftware YouTube频道.
综上所述,在.NET环境中选择IronPDF还是PDFsharp进行PDF文档管理时,开发人员应考虑其具体需求和项目要求。IronPDF凭借其丰富的功能集而表现出色,其中包括HTML到PDF的转换、加密功能以及对各种.NET平台和开发环境的强大支持。它提供了全面的文档、多个支持渠道和灵活的许可选项,尽管需要付费,但可以通过NuGet包管理器轻松安装到您的项目中。
另一方面,PDFsharp作为一个开源库,可以免费使用,但缺乏高级功能,如对HTML到PDF转换和PDF加密的原生支持。它可能需要额外的库,并且缺乏商业支持选项,因此主要适用于基本的PDF操作任务。