如何在C#中将印戳和水印添加到PDF

Stamp Text & Image on PDFs using C# and IronPDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF 通过 以及 类,并具备精确的定位控制功能。

盖章功能可在现有 PDF 文档上叠加附加内容。 图章功能可在 PDF 中添加信息、标签、水印或注释。 利用 IronPDF 全面的印章功能,添加页眉和页脚,或创建自定义水印。

IronPDF 提供四种印章工具:,以及。 `` 充分利用了所有 HTML 功能并结合 CSS 样式,类似于将 HTML 字符串转换为 PDF

快速入门:即时在 PDF 上添加文本水印 using IronPdf 的 类,可在 PDF 文档中添加文本注释、水印或标签。 下面的示例演示了如何使用 方法在 PDF 上添加水印。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 复制并运行这段代码。

    var pdf = new IronPdf.PdfDocument("input.pdf");
    var stamper = new IronPdf.Editing.TextStamper()
    {
        Text = "Confidential",
        FontSize = 50,
        Opacity = 50,
        VerticalAlignment = IronPdf.Editing.VerticalAlignment.Middle,
        HorizontalAlignment = IronPdf.Editing.HorizontalAlignment.Center
    };
    pdf.ApplyStamp(stamper);
    pdf.SaveAs("stamped.pdf");
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronPDF

    arrow pointer


如何在 PDF 上添加水印?

创建一个 对象,其中包含所有显示配置。 将 对象传递给 方法。 属性用于设置显示文本。 配置字体家族、字体样式和水印位置。

提供了丰富的自定义选项,类似于在 PDF 文档中[管理字体](https://ironpdf.com/how-to/manage-fonts/)。 请使用系统字体,或通过将 设置为 true 来使用 Google Fonts。

:path=/static-assets/pdf/content-code-examples/how-to/stamp-text-image-stamp-text.cs
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");
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")
$vbLabelText   $csharpLabel

生成的 PDF 文件是什么样子的?

如何创建多行文本印章?


中,请使用 标签处理多行文本。 例如,"line 1
line 2" 会在第一行显示 "line 1",在第二行显示 "line 2"。这可用于生成地址戳或多行水印。


如何在 PDF 上添加水印?

创建一个 对象,并使用 方法将图像应用到文档中。 该方法的第二个参数接受页面索引,用于对单页或多页进行水印处理。 下面的示例在 PDF 的第 1 页上添加了水印。

提示所有页面索引均采用从零开始的编号。

对于复杂的图像操作,可探索将图像添加到 PDF 中或处理 base64 编码的图像

:path=/static-assets/pdf/content-code-examples/how-to/stamp-text-image-stamp-image.cs
using IronPdf;
using IronPdf.Editing;
using System;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

// 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
Imports System

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>")

' Create image stamper
Private 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")
$vbLabelText   $csharpLabel

图像水印的输出效果是怎样的?

支持哪些图像格式?

`` 支持通过 URI 引用或文件路径加载 PNG、JPEG、GIF 和 SVG 格式文件。 请确保远程图片的网络连接正常。 对于 Azure 环境,建议使用来自 Azure Blob Storage 的图片


如何应用多个印章?

using `` 并通过传递一个印章数组来应用多个印章。 此方法可高效地在单次操作中处理多个图章。

:path=/static-assets/pdf/content-code-examples/how-to/stamp-text-image-multiple-stamps.cs
using IronPdf;
using IronPdf.Editing;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

// Create two text stampers
TextStamper stamper1 = new TextStamper()
{
    Text = "Text stamp 1",
    VerticalAlignment = VerticalAlignment.Top,
    HorizontalAlignment = HorizontalAlignment.Left,
};

TextStamper stamper2 = new TextStamper()
{
    Text = "Text stamp 2",
    VerticalAlignment = VerticalAlignment.Top,
    HorizontalAlignment = HorizontalAlignment.Right,
};

Stamper[] stampersToApply = { stamper1, stamper2 };

// Apply multiple stamps
pdf.ApplyMultipleStamps(stampersToApply);

pdf.SaveAs("multipleStamps.pdf");
Imports IronPdf
Imports IronPdf.Editing

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>")

' Create two text stampers
Private stamper1 As New TextStamper() With {
	.Text = "Text stamp 1",
	.VerticalAlignment = VerticalAlignment.Top,
	.HorizontalAlignment = HorizontalAlignment.Left
}

Private stamper2 As New TextStamper() With {
	.Text = "Text stamp 2",
	.VerticalAlignment = VerticalAlignment.Top,
	.HorizontalAlignment = HorizontalAlignment.Right
}

Private stampersToApply() As Stamper = { stamper1, stamper2 }

' Apply multiple stamps
pdf.ApplyMultipleStamps(stampersToApply)

pdf.SaveAs("multipleStamps.pdf")
$vbLabelText   $csharpLabel

当图章重叠时会发生什么?

可以混合使用不同类型的印章吗?

接受包含任何类型印章的数组。组合 以及 `` 组合使用。 可同时创建包含页眉、页脚、水印和识别码的复杂文档版式。


如何控制水印位置?

请使用 3x3 网格(包含三列和三行)来定义水印位置。 请选择水平对齐方式:左对齐、居中、右对齐。 请选择垂直对齐方式:顶部、中间、底部。 调整水平和垂直偏移量以实现精确定位。 请参见下图以获取直观展示。

IronPDF 盖章 API 对齐网格,展示水平/垂直定位选项及偏移示例

关键定位属性有哪些?

  • :相对于页面的水平对齐方式。 默认:.
  • :相对于页面的垂直对齐方式。 默认:.
  • : 水平偏移量。 默认值:0,单位:. 正值向右对齐,负值向左对齐。
  • : 垂直偏移量。 默认值:0,单位:. 正值向下偏移,负值向上偏移。

如何设置精确的偏移量?

请使用 `` 类。 默认计量单位为百分比。 可用单位包括英寸、毫米、厘米、像素和点。 这使得能够进行精确定位,例如在 PDF 文档中设置自定义边距

代码

:path=/static-assets/pdf/content-code-examples/how-to/stamp-text-image-stamp-location.cs
using IronPdf.Editing;
using System;

// Create text stamper
ImageStamper imageStamper = new ImageStamper(new Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg"))
{
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Top,

    // Specify offsets
    HorizontalOffset = new Length(10),
    VerticalOffset = new Length(10),
};
Imports IronPdf.Editing
Imports System

' Create text stamper
Private imageStamper As New ImageStamper(New Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg")) With {
	.HorizontalAlignment = HorizontalAlignment.Center,
	.VerticalAlignment = VerticalAlignment.Top,
	.HorizontalOffset = New Length(10),
	.VerticalOffset = New Length(10)
}
$vbLabelText   $csharpLabel

如何对 HTML 内容进行水印处理?

using 类对文本和图像进行水印处理。 渲染带有 CSS 样式的 HTML 设计,然后将其叠加到 PDF 文档上。 `` 属性指定了 CSS 和图像文件等 HTML 字符串资源的基准 URL。 此功能使用与将 HTML 转换为 PDF/A 相同的渲染引擎。

代码

:path=/static-assets/pdf/content-code-examples/how-to/stamp-text-image-multiple-stamps.cs
using IronPdf;
using IronPdf.Editing;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

// Create two text stampers
TextStamper stamper1 = new TextStamper()
{
    Text = "Text stamp 1",
    VerticalAlignment = VerticalAlignment.Top,
    HorizontalAlignment = HorizontalAlignment.Left,
};

TextStamper stamper2 = new TextStamper()
{
    Text = "Text stamp 2",
    VerticalAlignment = VerticalAlignment.Top,
    HorizontalAlignment = HorizontalAlignment.Right,
};

Stamper[] stampersToApply = { stamper1, stamper2 };

// Apply multiple stamps
pdf.ApplyMultipleStamps(stampersToApply);

pdf.SaveAs("multipleStamps.pdf");
Imports IronPdf
Imports IronPdf.Editing

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>")

' Create two text stampers
Private stamper1 As New TextStamper() With {
	.Text = "Text stamp 1",
	.VerticalAlignment = VerticalAlignment.Top,
	.HorizontalAlignment = HorizontalAlignment.Left
}

Private stamper2 As New TextStamper() With {
	.Text = "Text stamp 2",
	.VerticalAlignment = VerticalAlignment.Top,
	.HorizontalAlignment = HorizontalAlignment.Right
}

Private stampersToApply() As Stamper = { stamper1, stamper2 }

' Apply multiple stamps
pdf.ApplyMultipleStamps(stampersToApply)

pdf.SaveAs("multipleStamps.pdf")
$vbLabelText   $csharpLabel

HTML Stamper 的核心属性有哪些?

  • : 用于嵌入 PDF 的 HTML 片段。 对 JavaScript、CSS 和图片的外部引用均相对于
  • ``:外部 CSS、JavaScript 和图像文件引用的基础 URL。
  • :启用 Media=&quot;screen&quot; 的 CSS 样式。 设置 将使用 CSS media="PRINT" 渲染水印。 默认:``.

为何选择 HTML Stamper 而不是 Text Stamper?

`` 支持完整的 HTML 和 CSS,可在单个水印中实现复杂布局、多种字体、颜色及嵌入图片。非常适合用于丰富内容的水印或页眉。 保持品牌形象的一致性,并兼容响应式CSS设计


如何打印BarCode?

`` 类可将 BarCode 直接印制在现有的 PDF 文档上。 支持 QRCode、Code128 和 Code39 BARCODE 类型。

代码

:path=/static-assets/pdf/content-code-examples/how-to/stamp-text-image-stamp-text.cs
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");
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")
$vbLabelText   $csharpLabel

BARCODE有哪些可用的配置选项?

  • ``:BarCode的字符串值。
  • ``:支持 QRCode、Code128 和 Code39 的编码类型。默认值:QRCode。
  • ``: 渲染后的BarCode宽度(单位:像素)。 默认值:250px。
  • ``:渲染后的BarCode高度(单位:像素)。 默认值:250px。

何时应使用哪种BarCode类型?

QRCode 支持处理 URL 和大型数据集(最多 4,296 个字母数字字符)。 适用于对高密度有要求的字母数字数据。 适用于简单的数字或大写字母组合。 选择BarCode类型时,请考虑扫描设备的兼容性。


还有哪些其他 Stamper 选项可用?

这些属性可对印章的外观和行为进行精细控制,类似于创建自定义水印

如何控制水印的外观?

  • ``:控制透明度。 0 表示透明,100 表示不透明。
  • ``:将图章顺时针旋转 0 到 360 度。
  • ``:输出时间戳的最大宽度。
  • ``:输出水印的最大高度。
  • ``:输出印记的最小宽度。
  • ``:输出印章的最小高度。
  • ``:为标记的元素添加点击生效的超链接。 注意:HTML 链接(a 标签)在翻译过程中不会被保留。
  • ``:对邮票应用百分比缩放。 默认值:100(无影响)。
  • ``:将水印置于内容后方。 若内容不透明,水印可能不可见。
  • ``:等待事件或时间。参见使用 WaitFor 延迟 PDF 渲染
  • ``: 渲染超时时间(单位:秒)。 默认:60。

哪些属性使用最频繁?

可用于创建水印。 可调整视觉位置和尺寸。 示例:

// Create a semi-transparent watermark behind content
TextStamper watermark = new TextStamper()
{
    Text = "CONFIDENTIAL",
    FontSize = 60,
    FontColorCode = "#CCCCCC",
    Opacity = 25,
    Rotation = -45,
    IsStampBehindContent = true,
    VerticalAlignment = VerticalAlignment.Middle,
    HorizontalAlignment = HorizontalAlignment.Center
};
// Create a semi-transparent watermark behind content
TextStamper watermark = new TextStamper()
{
    Text = "CONFIDENTIAL",
    FontSize = 60,
    FontColorCode = "#CCCCCC",
    Opacity = 25,
    Rotation = -45,
    IsStampBehindContent = true,
    VerticalAlignment = VerticalAlignment.Middle,
    HorizontalAlignment = HorizontalAlignment.Center
};
' Create a semi-transparent watermark behind content
Dim watermark As New TextStamper() With {
    .Text = "CONFIDENTIAL",
    .FontSize = 60,
    .FontColorCode = "#CCCCCC",
    .Opacity = 25,
    .Rotation = -45,
    .IsStampBehindContent = True,
    .VerticalAlignment = VerticalAlignment.Middle,
    .HorizontalAlignment = HorizontalAlignment.Center
}
$vbLabelText   $csharpLabel

摘要

IronPDF 的加盖功能可向现有 PDF 文档中添加文本、图像、HTML 内容和 BarCode。 四个具备丰富自定义选项的专业印章类,可生成带有水印、页眉、页脚和注释的专业文档。 支持多种测量单位和对齐选项的定位系统,可确保像素级精准定位。 可使用简单的文本印章或带有 CSS 样式的复杂 HTML 布局。 IronPDF 的加盖水印 API 为企业级 PDF 处理提供了灵活性和控制力。

常见问题解答

如何用 C# 在 PDF 文档中添加文本水印?

使用 IronPDF 的 TextStamper 类添加文本水印。创建一个 TextStamper 对象,设置 Text、FontSize 和 Opacity 等属性,然后使用 ApplyStamp 方法。您可以使用 VerticalAlignment 和 HorizontalAlignment 属性对水印进行定位,以便精确放置。

能否在现有 PDF 文件中添加图像作为图章?

是的,IronPDF 提供了 ImageStamper 类,用于向 PDF 添加图像。创建一个 ImageStamper 对象,指定图像文件,然后使用 ApplyStamp 方法。您可以使用基于零的页面索引来控制定位并指定要盖印的页面。

哪些类型的内容可以印制到 PDF 上?

IronPDF 支持四种类型的图章:TextStamper 用于文本注释和水印,ImageStamper 用于图像,HTMLStamper 用于具有完整 CSS 风格的 HTML 内容,BarcodeStamper 用于条形码。每个戳记类都提供精确的定位控制。

如何创建多行文本标记?

在 TextStamper 的 Text 属性中使用
标记来创建多行标记。例如,"line 1
line 2 "将在两个独立的行上显示文本,非常适合创建地址戳记或多行水印。

在 PDF 上印制文本时可以使用自定义字体吗?

是的,IronPDF 中的 TextStamper 支持系统字体和 Google 字体。将 UseGoogleFont 属性设为 true 即可使用 Google 字体,或使用 FontFamily 属性指定任何已安装的系统字体,以实现完全的排版控制。

是否可以只在特定页面上添加印章?

当然可以。ApplyStamp 方法接受一个页面索引参数,允许您对特定页面加盖戳记。IronPDF 使用基于零的索引,因此第 1 页为索引 0。您还可以使用 ApplyMultipleStamps 一次为多个页面添加图章。

Curtis Chau
技术作家

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

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

准备开始了吗?
Nuget 下载 19,014,616 | 版本: 2026.5 just released
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronPdf
运行示例看着你的HTML代码变成PDF文件。