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

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

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

IronPDF 使用 TextStamper, ImageStamper, HTMLStamper, 和 BarcodeStamper 類別在 C# 中以精確的定位控制將文字、圖片、HTML 內容和條碼戳印到現有的 PDF 文件上。

<! -- 引言實作示意圖 --> <!--說明:說明程式碼概念的圖表或截圖 -->

Stamping 將額外的內容覆蓋在現有的 PDF 文件上。 圖章可在 PDF 上加入資訊、標籤、水印或註解。 使用圖章來加入頁首和頁尾,或利用 IronPDF 的全面圖章功能來建立自訂水印。

IronPDF 提供四種戳記器:TextStamperImageStamperHTMLStamperBarcodeStamperHTMLStamper 利用所有 HTML 功能與 CSS 定型,類似於 將 HTML 字串轉換為 PDF

快速入門:立即在 PDF 上新增圖章文字

使用 IronPDF 的 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.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 上標示文字?

<! -- 圖章文字範例實作示意圖 --> <!--說明:說明程式碼概念的圖表或截圖 -->

建立一個包含所有顯示配置的 TextStamper 物件。 將 TextStamper 物件傳給 ApplyStamp 方法。 Text 屬性會設定顯示的文字。 設定字型族、字型樣式和圖章位置。

TextStamper 提供廣泛的自訂選項,類似於 PDF 文件中的 管理字型。 將 UseGoogleFont 設定為 true,即可使用系統字型或 Google 字型。

: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> 標籤來處理多行文字。 例如,"行 1
行 2 "會在第一行產生 "行 1",在第二行產生 "行 2"。這可建立地址戳記或多行水印。


如何在 PDF 上標示圖片?

建立 ImageStamper 物件,並使用 ApplyStamp 方法將影像套用至文件。 該方法的第二個參數接受一個頁面索引,用於戳記單個或多個頁面。 以下範例為 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

圖章輸出是什麼樣子?

支援哪些影像格式?

ImageStamper 透過 URI 引用或檔案路徑支援 PNG、JPEG、GIF 和 SVG。 確保遠端影像的正常網路連線。 對於 Azure 環境,請考慮使用 來自 Azure Blob Storage 的影像


如何套用多個印章?

使用 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

當郵戳重疊時會發生什麼?

我可以混合使用不同類型的印章嗎?

ApplyMultipleStamps 接受包含任何圖章類型的陣列。將 TextStamper, ImageStamper, HTMLStamper, 和 BarcodeStamper 結合在一個操作中。 同時建立具有頁眉、頁腳、水印和識別碼的複雜文件版面。


如何控制圖章位置?

使用 3x3 網格定義圖章位置,其中有三個水平列和三個垂直行。 選擇水平對齊方式:左、中、右。 選擇垂直對齊方式:上、中、下。 調整水平與垂直偏移,以達到精確定位。 請參閱下面的圖片,以獲得視覺上的呈現。

IronPDF Stamping API 對齊網格顯示水平/垂直定位選項和偏移範例

關鍵定位屬性是什麼?

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

如何設定精確的偏移量?

使用 Length 類指定 HorizontalOffsetVerticalOffset 。 預設測量單位為百分比。 可用的單位包括英吋、毫米、公分、像素和點。 這可實現精確定位,例如在 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 內容加上圖章?

使用 HtmlStamper 類來標示文字和圖像。 HtmlStamper使用 CSS 設定來渲染 HTML 設計,然後將它們印在 PDF 文件上。 HtmlBaseUrl 屬性指定 HTML 字串資產 (如 CSS 和影像檔案) 的基本 URL。 這與 將 HTML 轉換為 PDF 使用相同的渲染引擎。

程式碼

: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 屬性?

  • Html:將 HTML 片段印在 PDF 上。 對 JavaScript、CSS 和圖片的外部引用是相對於 HtmlBaseUrl 而言。
  • HtmlBaseUrl:外部 CSS、JavaScript 和圖片檔案引用的基本 URL。
  • CssMediaType: 啟用 Media="screen" CSS 樣式。 設定 AllowScreenCss=false 會使用 CSS media="print" 來渲染圖章。 預設:PdfCssMediaType.Screen

為何使用 HTML Stamper 而非 Text Stamper?

HTMLStamper在一個圖章中提供完整的 HTML 和 CSS 支援,以支援複雜的佈局、多種字型、顏色和內嵌圖片。是豐富內容水印或標頭的理想選擇。 保持一致的品牌形象,並配合回應式 CSS 設計


我該如何標示 BarCode?

BarcodeStamper 類會直接在現有的 PDF 文件上標示條碼。 支援 QRCode、Code128 及 Code39 條碼類型。

程式碼

: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 有哪些配置選項?

  • Value: 條碼的字串值。
  • BarcodeType:支援 QRCode、Code128 和 Code39 的編碼類型。預設值:QRCode。
  • 寬度:渲染的 BarCode 寬度,單位為像素。 預設:250px。
  • 高度:渲染後的 BarCode 高度,單位為像素。 預設:250px。

我應該在何時使用每種 BarCode 類型?

QRCode 可處理 URL 和大型資料集(最多可達 4,296 個字母數字字符)。 Code128 適合具有高密度要求的字母數字資料。 Code39 適用於簡單的數字或大寫字母組合。 選擇 BarCode 類型時,請考慮掃描設備的相容性。


有哪些額外的 Stamper 選項?

這些屬性提供了對圖章外觀和行為的精細控制,類似於 建立自訂水印

如何控制圖章外觀?

  • 透明度:控制透明度。 0 代表隱形,100 代表不透明。
  • 旋轉:順時針旋轉圖章,從 0 到 360 度。
  • MaxWidth:輸出戳記的最大寬度。
  • MaxHeight:輸出圖章的最大高度。
  • MinWidth:輸出戳記的最小寬度。
  • MinHeight:輸出圖章的最小高度。
  • 超連結:將按一下的超連結加到蓋章元素。 注意:HTML 連結 (a 標籤) 不會因戳記而保留。
  • 刻度:對圖章套用百分比刻度。 預設值:100 (無效)。
  • IsStampBehindContent:在內容後方放置圖章。 如果內容不透明,可能看不到圖章。
  • WaitFor:等待事件或時間。請參閱 使用 WaitFor 延遲 PDF 渲染
  • Timeout :渲染超時時間(秒)。 預設值:60。

哪些屬性最常使用?

OpacityIsStampBehindContent 會建立水印。 ScaleRotation 可調整視覺定位和大小。 範例:

// 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。 四種具有廣泛客製化選項的專業 stamper 類別可建立具有水印、頁首、頁尾和註解的專業文件。 具有各種測量單位和對齊方式選項的定位系統可確保像素完美的位置。 使用簡單的文字戳記或複雜的 HTML 佈局與 CSS 定型。 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 用於 HTML 內容與完整的 CSS 定型,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 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 17,386,124 | 版本: 2026.2 剛剛發布