如何在 C# 中為 PDF 新增圖章和浮水印

How to Stamp Text & Image on PDFs

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

在 PDF 上加蓋文字和圖片涉及將其他內容覆蓋到現有 PDF 文件上。 這些內容通常被稱為“印章”,可以是文字、圖片或兩者的組合。 印章通常用於向 PDF 添加信息、標籤、水印或註釋。

在 IronPdf 中總共有 4 種蓋章器可用。 本文將講述 TextStamperImageStamperHTMLStamperBarcodeStamper。 HTMLStamper 特別強大,因為它可以利用所有 HTML 功能以及 CSS 樣式。

快速入門:立即在 PDF 上加蓋文字

本快速指南演示如何使用 IronPDF 以最小的努力在 PDF 文件上加蓋文字。 透過運用 TextStamper 類別,開發人員可以輕鬆地將文字註釋(如水印或標籤)添加到任何 PDF 文件中。 下面的範例展示了一個使用 ApplyStamp 方法在 PDF 上加蓋文字的簡單實作,確保加蓋過程快速且高效。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pdf = new IronPdf.PdfDocument("input.pdf");
    var stamper = new IronPdf.TextStamper("Confidential", 50, 50);
    pdf.ApplyStamp(stamper);
    pdf.SaveAs("stamped.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步)

  1. 下載 C# 庫以加蓋文字和圖片
  2. 建立並配置所需的印章類
  3. 使用 ApplyStamp 方法將印章應用於 PDF
  4. 使用 ApplyMultipleStamps 方法應用多個印章
  5. 指定將印章應用於的特定頁面


加蓋文字範例

首先,從 TextStamper 類別建立一個物件。 此物件將包含所有配置,以指定我們希望文字印章顯示的方式。 將 TextStamper 物件傳遞給 ApplyStamp 方法。 Text 屬性將是顯示的文字。 此外,我們可以指定字體系列、字體樣式及印章的位置。

: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

要在 TextStamper 中實現多行文字,請像在 HTML 中一樣使用 <br> 標籤。 例如,"line 1
line 2" 將在第一行生成 "line 1",在第二行生成 "line 2"。


加蓋圖片範例

類似於文字印章,我們首先從 ImageStamper 類別建立一個物件,然後使用 ApplyStamp 方法將圖片應用於文件。 此方法的第二個參數還接受頁面索引,可用於將印章應用於單頁或多頁。 在下面的範例中,我們指定圖片應在 PDF 的第 1 頁上加蓋。

提示所有頁碼皆採用0為起始的索引方式。

: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

輸出PDF


應用多個印章

使用 ApplyMultipleStamps 方法透過傳遞一個印章陣列來將多個印章加到文件上。

: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

輸出PDF


印章位置

為了定義印章的放置位置,我們使用一個 3x3 格網,其中包括三個水平欄和三個垂直行。 您可以選擇水平對齊:左、中和右,以及垂直對齊:上、中和下。 為了增加精確度,您可以調整每個位置的水平和垂直偏移。 請參見下方圖片以獲得此概念的視覺表示。

class="content-img-align-center">
class="center-image-wrapper">Stamp location
  • HorizontalAlignment:印章相對於頁面的水平對齊。 默認設置為 HorizontalAlignment.Center。
  • VerticalAlignment:印章相對於頁面的垂直對齊。 默認設置為 VerticalAlignment.Middle。
  • HorizontalOffset:水平偏移。 默認為 0,默認單位為 IronPdf.Editing.MeasurementUnit.Percentage。 正值表示向右偏移,而負值表示向左偏移。
  • VerticalOffset:垂直偏移。 默認為 0,默認單位為 IronPdf.Editing.MeasurementUnit.Percentage。 正值表示向下偏移,而負值表示向上偏移。

為了指定 HorizontalOffsetVerticalOffset 屬性,我們實例化 Length 類。 Length 的默認測量單位是百分比,但它也可以使用像英寸、毫米、厘米、像素和點數等測量單位。

代码

: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 範例

我們可以使用另一個印章類來加蓋文字和圖片。 HtmlStamper 類可用於渲染帶有 CSS 樣式的 HTML 設計,然後將其加蓋到 PDF 文件上。 HtmlBaseUrl 屬性用於指定 HTML 字串資源的基礎 URL,例如 CSS 和圖片文件。

代码

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

ChromePdfRenderer renderer = new ChromePdfRenderer();

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

// Create HTML stamper
HtmlStamper htmlStamper = new HtmlStamper()
{
    Html = @"<img src='https://ironpdf.com/img/svgs/iron-pdf-logo.svg'>
    <h1>Iron Software</h1>",
    VerticalAlignment = VerticalAlignment.Top,
};

// Stamp the HTML stamper
pdf.ApplyStamp(htmlStamper);

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

Private renderer As New ChromePdfRenderer()

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

' Create HTML stamper
Private htmlStamper As New HtmlStamper() With {
	.Html = "<img src='https://ironpdf.com/img/svgs/iron-pdf-logo.svg'>
    <h1>Iron Software</h1>",
	.VerticalAlignment = VerticalAlignment.Top
}

' Stamp the HTML stamper
pdf.ApplyStamp(htmlStamper)

pdf.SaveAs("stampHtml.pdf")
$vbLabelText   $csharpLabel
  • Html:將被加蓋到你的 PDF 上的 HTML 片段。 所有對 JavaScript、CSS 和圖片文件的外部引用將會相對於印章類的 HtmlBaseUrl 屬性。
  • HtmlBaseUrl:HTML 基礎 URL,在其中外部 CSS、JavaScript 和圖片文件的引用將是相對的。
  • CssMediaType:啟用 Media="screen" 的 CSS 樣式和樣式表。 通過設置 AllowScreenCss=false,IronPdf 從 HTML 使用 CSS 渲染印章,媒體="print" 如同在瀏覽器打印對話框中打印網頁。 默認值為 PdfCssMediaType.Screen。

加蓋條碼範例

BarcodeStamper 類可以直接在現有 PDF 文件上加蓋條碼。 此印章支持的條碼類型包括 QRCode、Code128 和 Code39。

代码

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

ChromePdfRenderer renderer = new ChromePdfRenderer();

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

// Create barcode stamper
BarcodeStamper barcodeStamper = new BarcodeStamper("IronPdf!!", BarcodeEncoding.Code39)
{
    VerticalAlignment = VerticalAlignment.Top,
};

// Stamp the barcode stamper
pdf.ApplyStamp(barcodeStamper);

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

Private renderer As New ChromePdfRenderer()

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

' Create barcode stamper
Private barcodeStamper As New BarcodeStamper("IronPdf!!", BarcodeEncoding.Code39) With {.VerticalAlignment = VerticalAlignment.Top}

' Stamp the barcode stamper
pdf.ApplyStamp(barcodeStamper)

pdf.SaveAs("stampBarcode.pdf")
$vbLabelText   $csharpLabel
  • Value:條碼的字串值。
  • BarcodeType:條碼的編碼類型,支持的類型包括 QRCode、Code128 和 Code39。默認為 QRCode。
  • Width:所渲染條碼的寬度,以像素為單位。 默認為 250px。
  • Height:所渲染條碼的高度,以像素為單位。 默認為 250px。

探索印章選項

除了上面提到的和解釋的選項外,以下是更多可供印章類使用的選項。

  • Opacity:允許印章透明。 0 完全不可見,100 完全不透明。
  • Rotation:按指定旋轉印章,範圍從 0 到 360 度。
  • MaxWidth:輸出印章的最大寬度。
  • MaxHeight:輸出印章的最大高度。
  • MinWidth:輸出印章的最小寬度。
  • MinHeight:輸出印章的最小高度。
  • Hyperlink:讓此印章的加蓋元素具有點擊超連結。 注意:由連結(a)標籤創建的 HTML 鏈接不保留於加蓋後。
  • Scale:對印章應用百分比比例,讓它們變大或變小。 默認為 100(百分比),這沒有影響。
  • IsStampBehindContent:設置為 true 以讓印章加在內容後面。 如果內容是不透明的,印章可能不可見。
  • WaitFor:一個方便的包裝器,用於等待各種事件或等待一段時間。
  • Timeout:渲染超時,以秒為單位。 默認值為 60。

常見問題解答

在PDF文件上新增文字和圖片的目的是什麼?

在 PDF 上新增文字和圖像印章,可以將標籤、浮水印或註釋等額外內容疊加到現有 PDF 文件上。這可以透過 IronPDF 的各種印章類別來實現,例如 `TextStamper` 和 `ImageStamper`。

如何使用 C# 在 PDF 上新增文字和圖片?

若要開始使用 C# 在 PDF 上新增文字和影像,請透過 NuGet 安裝 IronPDF 庫,配置對應的印章類,然後使用ApplyStamp方法將印章套用到 PDF 上。

在 IronPDF 中,我可以使用哪些類型的印章來進行 PDF 印章?

IronPDF 提供多種印章類型:`TextStamper` 用於文本,`ImageStamper` 用於圖像,`HTMLStamper` 用於帶有 CSS 的 HTML 內容,以及 `BarcodeStamper` 用於 QRCode 和其他條碼類型。

如何使用 C# 將文字圖章套用到 PDF 中的特定頁面?

在 IronPDF 中,您可以透過建立 `TextStamper` 物件、配置其屬性並使用具有頁面索引參數的ApplyStamp方法來定位特定頁面,從而將文字圖章套用至特定頁面。

是否可以在PDF上同時套用多個圖章?

是的,IronPDF 允許您使用ApplyMultipleStamps方法一次套用多個圖章,您可以在該方法中傳遞一個已配置的圖章物件陣列。

PDF 檔案中圖章的位置有哪些自訂選項?

IronPDF 允許使用 3x3 網格自訂印章位置,對齊選項包括水平方向的左、中、右,以及垂直方向的上、中、下,以及其他偏移設定。

HTML內容能否用於在PDF上新增印章?

是的,IronPDF 的 `HTMLStamper` 類別允許將帶有 CSS 樣式的 HTML 內容新增到 PDF 中,為複雜的設計提供了一個多功能的解決方案。

IronPDF 支援哪些類型的條碼加入 PDF 檔案?

IronPDF 的 `BarcodeStamper` 支援多種條碼類型,包括 QRCode、Code128 和 Code39,可讓您將條碼資訊新增至 PDF 文件中。

IronPDF 為自訂 PDF 圖章提供了哪些進階選項?

IronPDF 為 PDF 圖章提供進階自訂選項,包括不透明度、旋轉、最大寬度、最大高度、超連結、縮放、圖章位於內容之後、等待和逾時等屬性。

使用 IronPDF 進行 PDF 加蓋時,常見的故障排除技巧有哪些?

確保您已安裝正確版本的 IronPDF,驗證您的圖章物件的配置,並檢查您的程式碼是否正確指定了套用圖章的頁面索引或區域。

IronPDF 是否完全相容於 .NET 10,以便在 PDF 上新增文字和圖像?

是的-IronPDF 完全相容 .NET 10。諸如 `TextStamper`、`ImageStamper`、`ApplyStamp`、`ApplyMultipleStamps` 等圖章功能以及對齊/自訂選項,無需額外配置即可在 .NET 10 專案中直接使用。這些功能在 .NET 10 下的所有平台和專案類型中均受支援。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布