Stamp Text & Image on PDFs using C# and IronPDF
IronPDF使用BarcodeStamper類將文字、圖像、HTML內容和條形碼加蓋到現有的PDF文件中,並具有精確的定位控制。
加蓋是在現有的PDF文件上覆蓋附加內容。 加蓋可以在PDF上新增資訊、標籤、水印或註釋。 使用加蓋功能新增頁眉和頁腳或使用IronPDF的綜合加蓋功能建立自定義水印。
IronPDF提供四個加蓋器:BarcodeStamper。 HTMLStamper利用所有HTML功能並帶有CSS樣式,類似於將HTML字串轉換為PDF。
快速入門:立即在PDF上加蓋文字
使用IronPDF的TextStamper類為PDF文件新增文字注釋、水印或標籤。 以下範例展示了使用ApplyStamp方法在PDF上加蓋文字。
-
使用NuGet套件管理器安裝https://www.nuget.org/packages/IronPdf
-
複製並運行這段程式碼片段。
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"); -
部署以在您的實時環境中測試
今天就開始在您的專案中使用IronPDF,透過免費試用
簡化工作流程(5個步驟)
- 下載C#程式庫以加蓋文字和圖像
- 建立並配置所需的加蓋器類
- 使用
ApplyStamp方法將加蓋應用於PDF - 使用
ApplyMultipleStamps方法應用多個加蓋 - 指定特定頁面以應用加蓋
我如何在PDF上加蓋文字?
建立一個包含所有顯示配置的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")
輸出PDF的樣子如何?
我如何建立多行文字加蓋?
在<br>標籤。 例如,"line 1
line 2"在第一行生成"line 1",在第二行生成"line 2"。這樣即可建立地址加蓋或多行水印。
我如何在PDF上加蓋圖像?
建立一個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")
圖像加蓋的輸出樣子如何?
支持哪些圖像格式?
ImageStamper支持PNG、JPEG、GIF和SVG通過URI引用或文件路徑。 確保遠程圖像的網路連接正常。 對於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")
加蓋重疊時會發生什麼?
我可以混合不同型別的加蓋嗎?
BarcodeStamper。 同時建立具有頁眉、頁腳、水印和識別程式碼的複雜文件佈局。
我如何控制加蓋位置?
使用一個3x3的網格定義加蓋位置,具有三個水平列和三個垂直行。 選擇水平對齊方式:左,中心,右。 選擇垂直對齊方式:頂部,中間,底部。 調整水平和垂直偏移量以獲得精確的定位。 請參閱下面的圖像以獲得視覺表示。
關鍵定位屬性是什麼?
HorizontalAlignment:相對於頁面的水平對齊。 預設值:HorizontalAlignment.Center。VerticalAlignment:相對於頁面的垂直對齊。 預設值:VerticalAlignment.Middle。HorizontalOffset:水平偏移。 預設值:0,單位:MeasurementUnit.Percentage。 正值向右偏移,負值向左偏移。VerticalOffset:垂直偏移。 預設值:0,單位:MeasurementUnit.Percentage。 正值向下偏移,負值向上偏移。
我如何設置精確的偏移量?
使用VerticalOffset。 預設測量單位為百分比。 可用的單位包括英寸、毫米、厘米、像素和點。 這允許在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)
}
我如何加蓋HTML內容?
使用HtmlStamper類加蓋文字和圖像。 HtmlStamper使用CSS樣式渲染HTML設計,然後將其加蓋到PDF文件上。 HtmlBaseUrl屬性指定HTML字串資產的基本URL,如CSS和圖像文件。 這使用與將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")
HTML加蓋器的基本屬性是什麼?
Html:要加蓋到PDF的HTML片段。 JavaScript、CSS及圖像的外部引用相對於HtmlBaseUrl。HtmlBaseUrl:外部CSS、JavaScript及圖像文件引用的基本URL。CssMediaType:啟用Media="screen" CSS樣式。 設置AllowScreenCss=false使用媒介為"print"的CSS來渲染加蓋。 預設值:PdfCssMediaType.Screen。
為什麼要使用HTML加蓋器而不是文字加蓋器?
HTMLStamper提供完整的HTML和CSS支持,用於複雜的佈局、多種字體、顏色和嵌入的圖像在一個加蓋中。適合用於豐富內容的水印或頁眉。 保持一致的品牌形象並支持響應式CSS設計。
我如何加蓋條形碼?
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")
有哪些條形碼配置選項?
Value:條形碼的字串值。BarcodeType:支持QRCode、Code128和Code39的編碼型別。預設值:QRCode。Width:以像素為單位的渲染條形碼寬度。 預設值:250px。Height:以像素為單位的渲染條形碼高度。 預設值:250px。
我應該什麼時候使用每種條形碼型別?
QRCode處理URLs和大資料集(最多4296個字母數字字元)。 Code128適合有高密度要求的字母數字資料。 Code39適用於簡單的數字或大寫字母組合。 在選擇條形碼型別時考慮掃描裝置的相容性。
有哪些其他加蓋選項?
這些屬性提供對加蓋外觀和行為的精細控制,類似於建立自定義水印。
我如何控制加蓋外觀?
Opacity:控制透明度。 0是不可見,100是不透明。Rotation:將加蓋順時針旋轉0到360度。MaxWidth:輸出加蓋的最大寬度。MaxHeight:輸出加蓋的最大高度。MinWidth:輸出加蓋的最小寬度。MinHeight:輸出加蓋的最小高度。Hyperlink:為加蓋的元素新增單擊超連結。 注意:HTML連結(a標籤)無法通過加蓋保留。Scale:對加蓋應用百分比縮放。 預設值:100(無效果)。IsStampBehindContent:將加蓋置於內容後。 如果內容是不透明的,加蓋可能不可見。WaitFor:等待事件或時間。參見使用WaitFor來延遲PDF渲染。Timeout:渲染超時以秒為單位。 預設值:60。
最常用的屬性有哪些?
IsStampBehindContent建立水印。 Rotation調整視覺定位和大小。 範例:
// 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
}
摘要
IronPDF的加蓋功能將文字、圖像、HTML內容和條形碼新增到現有的PDF文件。 四個專用的加蓋器類具有豐富的自定義選項,可以建立帶有水印、頁眉、頁腳和註釋的專業文件。 包含多種測量單位和對齊選項的定位系統確保每一像素都精確無誤。 使用簡單的文字加蓋或具有CSS樣式的複雜HTML佈局。 IronPDF的加蓋API為企業級PDF操作提供靈活性和控制力。
常見問題
如何在 C# 中將文字浮水印新增到我的 PDF 文件中?
using IronPDF 的 TextStamper 類別來新增文字浮水印。建立一個 TextStamper 物件,設置屬性,如文字、字體大小和透明度,然後使用 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 一次性在多個頁面上加蓋。

