跳至页脚内容
产品比较

探索添加水印到PDF的PDFsharp最佳替代方案

在PDF中添加水印 是文档安全、品牌和版本控制的常见要求。 无论是将文档标记为机密、对官方报告进行品牌化,还是防止未经授权的复制,水印都是一个至关重要的功能。

在C#中,开发人员可以选择多个库,其中IronPDFPDFsharp是两个最受欢迎的选项。 然而,它们的实现方法、易用性、性能和许可结构差异显著。 本文对IronPDF和PDFsharp在现有PDF上添加水印的功能进行了详细比较,提供了关于其功能、实现过程和自定义能力的见解。

通过阅读本文,您将清楚地了解哪种库基于易用性、性能和功能可用性最符合您的项目需求。

理解PDF水印

什么是水印?

水印是文档上的图形或文本叠加,用作标识、威慑或品牌元素。 水印可以是可见的或不可见的,具体取决于其目的。

水印的类型

  • 文字水印——通常是半透明的叠加,带有"机密"或"草稿"等信息。
  • 图像水印——嵌入到文档中的徽标、标志或图形。
  • 透明水印——不会阻碍文档可读性的微弱品牌标识。
  • 加盖水印——确保可见性的更加显著的标记。

常见用例

  • 安全和保护——通过将文档标记为专有来防止未经授权的复制。
  • 品牌化——添加公司徽标或签名,以保持跨文档的品牌一致性。
  • 版本控制——标记草稿、最终版本或文档修订。

IronPDF和PDFsharp概述

IronPDF。

IronPDF 是一个高级、功能丰富的.NET库,旨在简化PDF处理。 它特别适合于寻找易用的PDF操作任务实现的开发人员,包括水印。

关键特性:

  • 简单直观的API,需要最少的代码。
  • 支持文本和图像水印,自定义选项。
  • 提供不透明度控制、定位和旋转以实现精确放置。
  • 兼容.NET 6+、.NET Core和.NET Framework。
  • 提供永久许可模型用于长期使用。
  • 附加功能包括PDF注释HTML到PDF转换数字签名

PDFsharp

PDFsharp是一个开源库,允许开发人员在C#中创建、编辑和操作PDF。 虽然它灵活性很高,但与IronPDF相比,水印需要更多的手动工作。

关键特性:

  • 免费和开源,使其成为预算有限项目的经济选择。
  • 提供对PDF绘图操作的低级控制,包括轮廓图形路径和透明图形路径。
  • 支持文本和图像水印,但转换需要额外的代码。
  • 与.NET Framework和.NET Core(通过PDFSharpCore)兼容。
  • 缺乏内置的高级水印功能,需要开发人员手动实现不透明度和旋转等功能。

使用 IronPDF 添加水印

IronPDF提供了一个简单的API,使开发人员能够通过几行代码高效地应用水印,轻松简化您的PDF水印任务,而无需复杂或手动设置。 IronPDF的水印工具可以使用HTML/CSS字符串作为水印,正如您将在下面看到的那样,使您完全控制水印的外观。

文字水印示例

using IronPdf;

const string filename = "existing.pdf";
// Load the existing PDF file
PdfDocument pdf = PdfDocument.FromFile(filename);

// Create a simple HTML-based watermark
string watermark = "<h1 style='color:red'>Confidential!</h1>";

// Apply the watermark to the PDF
pdf.ApplyWatermark(watermark);

// Save the updated document with the applied watermark
pdf.SaveAs("watermarked.pdf");
using IronPdf;

const string filename = "existing.pdf";
// Load the existing PDF file
PdfDocument pdf = PdfDocument.FromFile(filename);

// Create a simple HTML-based watermark
string watermark = "<h1 style='color:red'>Confidential!</h1>";

// Apply the watermark to the PDF
pdf.ApplyWatermark(watermark);

// Save the updated document with the applied watermark
pdf.SaveAs("watermarked.pdf");
Imports IronPdf

Private Const filename As String = "existing.pdf"
' Load the existing PDF file
Private pdf As PdfDocument = PdfDocument.FromFile(filename)

' Create a simple HTML-based watermark
Private watermark As String = "<h1 style='color:red'>Confidential!</h1>"

' Apply the watermark to the PDF
pdf.ApplyWatermark(watermark)

' Save the updated document with the applied watermark
pdf.SaveAs("watermarked.pdf")
$vbLabelText   $csharpLabel

探索PDFsharp添加水印到PDF的最佳替代方案:图3 - 文字水印输出

在此代码示例中,我们可以看到使用IronPDF将水印应用到现有PDF文件是多么简单。 在这里,我们使用FromFile方法加载现有PDF。 然后,我们创建一个简单的字符串,将其格式化为HTML元素以作为水印,并通过ApplyWatermark将其应用到PDF。 如输出图像所示,这添加了一个简单的文字字符串"Confidential"作为我们PDF上的水印。

图像水印示例

using IronPdf;

// Load the PDF document
PdfDocument pdf = PdfDocument.FromFile("existing.pdf");

// Create an HTML-based watermark containing the image
string watermark = "<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>";

// Apply the watermark to the PDF with rotation and opacity
pdf.ApplyWatermark(watermark, rotation: 45, opacity: 80);

// Save the watermarked document
pdf.SaveAs("watermarked.pdf");
using IronPdf;

// Load the PDF document
PdfDocument pdf = PdfDocument.FromFile("existing.pdf");

// Create an HTML-based watermark containing the image
string watermark = "<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>";

// Apply the watermark to the PDF with rotation and opacity
pdf.ApplyWatermark(watermark, rotation: 45, opacity: 80);

// Save the watermarked document
pdf.SaveAs("watermarked.pdf");
Imports IronPdf

' Load the PDF document
Private pdf As PdfDocument = PdfDocument.FromFile("existing.pdf")

' Create an HTML-based watermark containing the image
Private watermark As String = "<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>"

' Apply the watermark to the PDF with rotation and opacity
pdf.ApplyWatermark(watermark, rotation:= 45, opacity:= 80)

' Save the watermarked document
pdf.SaveAs("watermarked.pdf")
$vbLabelText   $csharpLabel

探索PDFsharp添加水印到PDF的最佳替代方案:图4

将图像添加为水印与添加文本一样简单,因为它们都使用相同的方法。 就像在文字示例中一样,我们创建一个新的水印字符串变量,包含指向图像URL的HTML图像标签并将其应用。 这一次,我们包括了自定义的旋转和不透明度转换。

此方法在指定位置覆盖一个图像水印,允许自定义位置和透明度。

使用PDFsharp添加水印

PDFsharp要求开发人员使用其GDI+绘图API手动渲染文本和图像。 要在现有PDF文件上加水印,请创建用于绘图的XGraphics对象并应用所需的内容。

文字水印示例

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

const string filename = "existing.pdf";
// Open the PDF document in modify mode
var document = PdfReader.Open(filename, PdfDocumentOpenMode.Modify);

foreach (var page in document.Pages)
{
    // Create an XGraphics object for drawing
    var gfx = XGraphics.FromPdfPage(page);

    // Move the origin to the center of the page for rotation purposes
    gfx.TranslateTransform(page.Width / 2, page.Height / 2);

    // Rotate for diagonal watermark placement
    gfx.RotateTransform(Math.Atan(page.Height / page.Width));

    // Define font and brush for drawing the watermark text
    var font = new XFont("Arial", 40);
    var brush = new XSolidBrush(XColor.FromArgb(128, XColors.Red));  // Semi-transparent red

    // Draw the watermark text centered on the page
    gfx.DrawString("WATERMARK", font, brush, new XPoint(0, 0));
}

// Save modified document
document.Save("watermarked.pdf");
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using PdfSharp.Pdf.IO;

const string filename = "existing.pdf";
// Open the PDF document in modify mode
var document = PdfReader.Open(filename, PdfDocumentOpenMode.Modify);

foreach (var page in document.Pages)
{
    // Create an XGraphics object for drawing
    var gfx = XGraphics.FromPdfPage(page);

    // Move the origin to the center of the page for rotation purposes
    gfx.TranslateTransform(page.Width / 2, page.Height / 2);

    // Rotate for diagonal watermark placement
    gfx.RotateTransform(Math.Atan(page.Height / page.Width));

    // Define font and brush for drawing the watermark text
    var font = new XFont("Arial", 40);
    var brush = new XSolidBrush(XColor.FromArgb(128, XColors.Red));  // Semi-transparent red

    // Draw the watermark text centered on the page
    gfx.DrawString("WATERMARK", font, brush, new XPoint(0, 0));
}

// Save modified document
document.Save("watermarked.pdf");
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf.IO

Private Const filename As String = "existing.pdf"
' Open the PDF document in modify mode
Private document = PdfReader.Open(filename, PdfDocumentOpenMode.Modify)

For Each page In document.Pages
	' Create an XGraphics object for drawing
	Dim gfx = XGraphics.FromPdfPage(page)

	' Move the origin to the center of the page for rotation purposes
	gfx.TranslateTransform(page.Width \ 2, page.Height \ 2)

	' Rotate for diagonal watermark placement
	gfx.RotateTransform(Math.Atan(page.Height \ page.Width))

	' Define font and brush for drawing the watermark text
	Dim font = New XFont("Arial", 40)
	Dim brush = New XSolidBrush(XColor.FromArgb(128, XColors.Red)) ' Semi-transparent red

	' Draw the watermark text centered on the page
	gfx.DrawString("WATERMARK", font, brush, New XPoint(0, 0))
Next page

' Save modified document
document.Save("watermarked.pdf")
$vbLabelText   $csharpLabel

这种实现需要在每个页面上手动绘制水印,要求精确的定位和自定义。 虽然它能够以类似于IronPDF示例的输出处理该任务,但PDFsharp需要更多代码和更复杂的方法来处理将文本水印应用到现有内容或新的PDF文件。

图像水印示例

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

// Open the existing PDF document in modify mode
var document = PdfReader.Open("sample.pdf", PdfDocumentOpenMode.Modify);

// Load the watermark image
XImage watermark = XImage.FromFile("watermark.png");

foreach (var page in document.Pages)
{
    // Create a graphics object from the page
    XGraphics gfx = XGraphics.FromPdfPage(page);

    // Draw the image watermark at the specified position and size
    gfx.DrawImage(watermark, 50, 100, watermark.PixelWidth / 2, watermark.PixelHeight / 2);
}

// Save the modified PDF document
document.Save("watermarked.pdf");
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using PdfSharp.Pdf.IO;

// Open the existing PDF document in modify mode
var document = PdfReader.Open("sample.pdf", PdfDocumentOpenMode.Modify);

// Load the watermark image
XImage watermark = XImage.FromFile("watermark.png");

foreach (var page in document.Pages)
{
    // Create a graphics object from the page
    XGraphics gfx = XGraphics.FromPdfPage(page);

    // Draw the image watermark at the specified position and size
    gfx.DrawImage(watermark, 50, 100, watermark.PixelWidth / 2, watermark.PixelHeight / 2);
}

// Save the modified PDF document
document.Save("watermarked.pdf");
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf.IO

' Open the existing PDF document in modify mode
Private document = PdfReader.Open("sample.pdf", PdfDocumentOpenMode.Modify)

' Load the watermark image
Private watermark As XImage = XImage.FromFile("watermark.png")

For Each page In document.Pages
	' Create a graphics object from the page
	Dim gfx As XGraphics = XGraphics.FromPdfPage(page)

	' Draw the image watermark at the specified position and size
'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(watermark, 50, 100, watermark.PixelWidth / 2, watermark.PixelHeight / 2)
Next page

' Save the modified PDF document
document.Save("watermarked.pdf")
$vbLabelText   $csharpLabel

探索PDFsharp添加水印到PDF的最佳替代方案:图6

此方法放置图像水印; 但是,不像IronPDF,不透明度处理必须单独管理。 就像文字水印示例一样,在现有PDF上应用基于图像的水印使用PDFsharp需要比IronPDF的简化水印API更为复杂和详细的设置。

对比IronPDF和PDFsharp的水印功能

易用性

  • IronPDF: 提供高级功能,可通过最少的代码简化水印处理。 它对复杂操作进行抽象,使其成为需要快速高效解决方案的开发人员的理想选择。
  • PDFSharp: 需要使用图形API的手动实现,这增加了复杂性和开发时间。它更适合需要对渲染进行细粒度控制但对额外编码感到舒服的开发人员。

性能

  • IronPDF: 优化用于高速PDF处理,能够高效处理大型文档而不会显著影响性能。
  • PDFSharp: 虽然轻量级,但可能需要额外的优化来处理大型PDF。 相比IronPDF,具有多个变换的复杂水印任务可能导致性能较慢。

自定义选项

  • IronPDF: 内置支持不透明度、旋转、定位和字体大小自定义。 用户可以轻松调整设置,而无需深入复杂的渲染逻辑。
  • PDFSharp: 对不透明度、透明效果和变换处理需要额外编码。 虽然功能强大,但要求开发人员进行更高水平的自定义,包括使用var格式进行特定渲染任务。

兼容性

  • IronPDF: 与.NET 6+、.NET Core和.NET Framework完全兼容,使其适合现代和遗留应用程序。
  • PDFSharp: 支持.NET Framework和.NET Core(通过PDFSharpCore),但可能缺少某些新框架中可用的现代功能。

许可和成本

  • IronPDF: 一个需要付费许可的商业产品,但包括永久许可选项、客户支持和持续更新。
  • PDFSharp: 开源且可免费使用,对那些喜欢不受限制许可模式但愿意自己处理支持和更新的开发人员来说是一个具有成本效益的解决方案。

结论

探索PDFsharp添加水印到PDF的最佳替代方案:图7

对于需要一种简单高效方式来为PDF添加水印的开发人员来说,IronPDF 是一个优越的选择,因为其用户友好的API和内置功能。 然而,如果预算有限并且您不介意编写额外代码,PDFsharp 是一个坚实的开源替代选择。最终,最佳选择取决于项目要求、编码专业知识和可用资源。

通过免费下载试用 IronPDF,探索它如何将您的C# PDF项目提升到新高度!

请注意PDFsharp 是其各自所有者的注册商标。 本网站与PDFsharp无关,未获得其认可或赞助。所有产品名称、徽标和品牌均为其各自所有者的财产。 比较仅供参考,反映的是撰写时的公开信息。

常见问题解答

如何使用 .NET 库将水印添加到 PDF?

您可以通过使用 IronPDF™ 的简单 API 将水印添加到 PDF 中,该 API 支持文本和图像水印,并具有可定制的选项,如不透明度和旋转。

使用高级 .NET PDF 库进行加水印有什么优势?

像 IronPDF™ 这样的高级 .NET PDF 库提供了高级功能,便于加水印,与现代 .NET 框架兼容,并提供 PDF 注释和 HTML 到 PDF 转换等附加功能。

为什么水印在 PDF 文档中很重要?

水印对于文档安全、品牌推广和版本控制很重要。它有助于防止未经授权的复制,确保品牌一致性,并将文档标记为机密。

IronPDF™ 和 PDFsharp® 在 PDF 加水印方面有什么区别?

IronPDF™ 提供了一种更直观的 API,可以通过最少的代码轻松加水印,而 PDFsharp® 需要更多的手动工作和额外的代码来进行转换和不透明度设置。

与开源选项相比,IronPDF™ 如何改进 PDF 操作?

IronPDF™ 提供内置的高级功能,使得进行 PDF 操作(如加水印、注释和转换)更加容易,而在开源选项(如 PDFsharp®)中则需要更复杂的编码。

.NET 库可以向 PDF 添加哪些类型的水印?

使用像 IronPDF™ 这样的库,您可以添加文本水印、图像水印和透明水印,并可以选择定制定位、不透明度和旋转。

IronPDF™ 适合处理大规模 PDF 文档吗?

是的,IronPDF™ 已优化为高速处理,并且可以高效处理大规模 PDF 文档而不会出现性能问题。

在选择高级和开源 .NET PDF 库时,我应该考虑哪些因素?

考虑易用性、可用功能、兼容性、性能和支持。像 IronPDF™ 这样的高级库提供了广泛的功能和支持,而像 PDFsharp® 这样的开源库是免费的,但需要更复杂的编码,缺乏官方支持。

我可以在 .NET Core 中使用 IronPDF™ 吗?

可以,IronPDF™ 兼容 .NET 6+、.NET Core 和 .NET Framework,使其适用于不同的开发环境。

IronPDF™ 除了加水印还有哪些额外功能?

除了加水印之外,IronPDF™ 还支持 PDF 注释、HTML 到 PDF 转换、数字签名等,提供全面的 PDF 操作能力。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。