如何在 C# 中為 PDFs 添加印章和水印

使用 C# 和 IronPDF 在 PDF 上新增文字和圖像印章

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 Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronPDF

    PM > Install-Package IronPdf

  2. 複製並運行這段程式碼。

    var pdf = new IronPdf.PdfDocument("input.pdf");
    var stamper = new IronPdf.TextStamper("Confidential", 50, 50);
    pdf.ApplyStamp(stamper);
    pdf.SaveAs("stamped.pdf");
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronPDF,免費試用!
    arrow pointer


郵票文字範例

首先,建立一個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 中實現多行文本,請使用<br>標籤,就像HTML中的標籤一樣。 例如,"第 1 行
"line 2"將在第一行產生"line 1",在第二行產生"line 2"。


郵票圖像範例

與文字圖章類似,我們首先從ImageStamper類別建立一個對象,然後使用ApplyStamp方法將圖像應用到文件中。 此方法的第二個參數也接受頁碼索引,可用於將印章套用至單一或多個頁面。 在下面的範例中,我們指定影像應蓋章在 PDF 的第 1 頁。

所有頁面索引均採用從零開始的索引方式。

: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 的網格,其中有三列和三行。 您可以選擇水平對齊方式:左對齊、居中對齊和右對齊;以及垂直對齊方式:上對齊、居中對齊和下對齊。 為了提高精度,您可以調整每個位置的水平和垂直偏移。 請參考下圖以直覺的方式了解這個概念。

印章位置
  • HorizontalAlignment :印章相對於頁面的水平對齊方式。 預設為 HorizontalAlignment.Center。
  • VerticalAlignment :印章相對於頁面的垂直對齊方式。 預設為 VerticalAlignment.Middle。
  • HorizontalOffset :水平偏移量。 預設值為 0,預設單位為 IronPdf.Editing.MeasurementUnit.Percentage。 正值表示向右偏移,負值表示向左偏移。
  • VerticalOffset :垂直偏移量。 預設值為 0,預設單位為 IronPdf.Editing.MeasurementUnit.Percentage。 正值表示向下偏移,負值表示向上偏移。

為了指定HorizontalOffsetVerticalOffset屬性,我們實例化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 字串資源(例如 CSS 和圖片檔案)的基本 URL。

程式碼

: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 和映像檔的外部引用都將相對於 Stamper 類別的 HtmlBaseUrl 屬性。
  • HtmlBaseUrl :HTML 基本 URL,外部 CSS、Javascript 和圖片檔案的引用將以此為相對路徑。
  • CssMediaType :啟用 Media="screen" CSS 樣式和樣式表。 透過設定 AllowScreenCss=false,IronPdf 會使用 CSS 將 HTML 中的 Stamp 渲染為 media="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 :使此印章的印章元素具有點擊超連結。 注意:透過 link(a) 標籤建立的 HTML 連結不會被標記保留。
  • Scale :對郵票套用百分比縮放,使其變大或變小。 預設值為 100(百分比),但沒有任何效果。
  • IsStampBehindContent :設定為 true 可將圖章套用至內容的後方。 如果內容物不透明,印章可能看不見。
  • WaitFor :一個方便的包裝器,用於等待各種事件或等待一段時間。
  • Timeout :渲染超時時間(秒)。 預設值為 60。

常見問題解答

在 PDF 上加蓋文字和圖片的目的是什麼?

在 PDF 上加蓋文字和圖片可讓您將標籤、水印或註釋等額外內容疊加到現有的 PDF 文件中。這是通過使用 IronPDF 的各種 stamper 類如 `TextStamper` 和 `ImageStamper` 來實現的。

如何開始使用 C# 在 PDF 上加蓋文字和圖片?

要開始使用 C# 在 PDF 上加蓋文字和圖片,請透過 NuGet 安裝 IronPDF 庫,配置適當的 stamper 類,並使用 ApplyStamp 方法將印章應用到 PDF 上。

IronPDF 中有哪些印章類型可用於 PDF 加蓋?

IronPDF 提供了幾種類型的印章:`TextStamper` 用於文字,`ImageStamper` 用於圖片,`HTMLStamper` 用於含有 CSS 的 HTML 內容,還有 `BarcodeStamper` 用於 QRCode 和其他條碼類型。

如何使用 C# 在 PDF 的特定頁面上加蓋文字印章?

在 IronPDF 中,您可以通過創建一個 `TextStamper` 物件,配置其屬性,然後使用帶有頁面索引參數的 ApplyStamp 方法以定位特定頁面來加蓋文字印章。

是否可以在 PDF 上同時應用多個印章?

是的,IronPDF 允許您使用 ApplyMultipleStamps 方法一次應用多個印章,您可以傳遞一個配置好的 stamper 物件數組。

IronPDF 提供了哪些自訂選項可用於 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,驗證您的 stamper 物件的配置,並檢查您的代碼是否正確指定了頁面索引或加蓋位置。

IronPDF 是否與 .NET 10 完全相容,可在 PDF 上標示文字和圖片?

是 - IronPDF 與 .NET 10 完全相容。在 .NET 10 專案中,`TextStamper`、`ImageStamper`、`ApplyStamp`、`ApplyMultipleStamps` 等戳記功能以及對齊/自訂選項均可開箱即用,無需額外設定。在 .NET 10 下,所有平台和專案類型都支援這些功能。

Chaknith Bin
軟體工程師
Chaknith 在 IronXL 和 IronBarcode 上工作。他對 C# 和 .NET 擁有深厚的專業知識,幫助改進了軟體並支持客戶。他從用戶互動中得到的見解有助於改善產品、文檔和整體體驗。
準備好開始了嗎?
Nuget 下載 16,493,056 | Version: 2025.11 剛發表