跳過到頁腳內容
產品比較

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

在PDF中添加水印是文件安全、品牌推广和版本控制的常见需求。 无论是将文件标注为机密、为官方报告打上品牌,还是防止未经授权的复制,水印都是一个必不可少的功能。

In C#, developers have multiple libraries to choose from, with IronPDF and PDFSharp being two of the most popular options. 然而,它们的方法、易用性、性能和许可结构有显著差异。 本文详细比较了在现有PDF上添加水印时IronPDF和PDFSharp的功能,提供了关于其功能、实现过程和自定义能力的见解。

在本文结束时,您将清楚了解哪个库基于易用性、性能和功能可用性最适合您的项目需求。

理解PDF水印

什么是水印?

水印是文件上的图形或文本覆盖层,作为标识、威慑或品牌元素。 水印可以是可见或不可见的,取决于其目的。

水印类型

  • 文字水印 – 通常是带有“机密”或“草稿”等信息的半透明覆盖层。
  • 图像水印 – 嵌入文档中的徽标、标志或图形。
  • 透明水印 – 不影响文档可读性的细微品牌标记。
  • 标记水印 – 更显眼、醒目的标记,确保可见性。

常见用例

  • 安全与保护 – 通过将文件标记为专有以防止未经授权的复制。
  • 品牌推广 – 添加公司徽标或签名以在文件中保持品牌一致性。
  • 版本控制 – 标记草稿、最终版本或文档修订。

IronPDF和PDFsharp概述

IronPDF

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

关键功能:

  • 简单且直观的API,所需代码量极少。
  • 支持文字和图像水印,具有自定义选项。
  • 提供不透明度控制、定位和旋转以实现精确放置。
  • 兼容.NET 6+、.NET Core和.NET Framework。
  • 提供永久许可模型以供长期使用。
  • Additional capabilities include PDF annotations, HTML-to-PDF conversion, and digital signatures.

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 - 文字水印输出

在这个代码示例中,我们看到了如何轻松地将水印应用于现有的PDF文件与IronPDF。 在这里,我们使用FromFile方法加载现有PDF。 然后,我们创建一个简单的字符串,格式化为HTML元素作为水印,并使用ApplyWatermark将其应用于PDF。 如输出图像所示,这在我们的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不同,不透明度处理必须单独管理。 如同文字水印示例一样,使用PDFsharp将基于图像的水印应用于现有PDF需要比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無關,未經PDFsharp贊助或認可。所有產品名稱、徽標和品牌均為其相應所有者的財產。 比較僅供信息參考,並反映撰寫時公開可用的信息。

常見問題解答

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

您可以通过利用 IronPDF 的简单 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 的开源库虽免费但需要更复杂的编码并且缺乏官方支持。

我可以使用 IronPDF 和 .NET Core 吗?

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

IronPDF 除了水印之外还提供哪些附加功能?

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

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。