如何在 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 樣式。

開始使用 IronPDF

立即在您的專案中使用IronPDF,並享受免費試用。

第一步:
green 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")
VB   C#

輸出 PDF

要在TextStamper中實現多行文字,請使用<br>標籤,如同在HTML中一樣。 例如,「line 1 <br> 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")
VB   C#

輸出 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")
VB   C#

輸出 PDF


蓋章位置

要定義印章的放置位置,我們使用一個3x3網格,包含三個水平列和三個垂直行。 您可以選擇水平對齊方式:左、中和右,以及垂直對齊方式:上、中和下。 為了增加精確度,您可以調整每個位置的水平和垂直偏移。 請參考下面的圖像,以視覺化呈現這個概念。

蓋章位置
  • 水平對齊:印章相對於頁面的水平對齊。 預設設定為HorizontalAlignment.Center。
  • 垂直對齊:印章相對於頁面的垂直對齊。 預設設定是 VerticalAlignmentCenter.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)
}
VB   C#

Stamp 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")
VB   C#
  • Html:要蓋在您的PDF上的HTML片段。 所有對JavaScript、CSS和圖像文件的外部引用將相對於Stamper Class的HtmlBaseUrl屬性。
  • HtmlBaseUrl:HTML基座URL,用於相對引用外部CSS、Javascript和圖像文件。
  • CssMediaType:啟用 Media="screen" CSS 樣式和樣式表。 將 AllowScreenCss 設為 false 時,IronPdf 使用適用於 media="print" 的 CSS,從 HTML 渲染印章,就像在瀏覽器列印對話框中列印網頁一樣。 預設值為 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")
VB   C#
  • :條碼的字符串值。
  • 條碼類型:條碼的編碼類型,支持的類型包括QRCode、Code128和Code39。默認為QRCode。
  • 寬度:以像素為單位的渲染條碼寬度。 預設值為250px。
  • 高度:以像素計的條碼渲染高度。 預設值為250px。

探索印章選項

除了上面提到並解釋的選項外,以下是提供給印章類的更多選項。

  • 不透明度:允許印章透明。 0 是完全透明,100 是完全不透明。
  • 旋轉:根據指定,將郵票順時針旋轉0至360度。
  • MaxWidth:輸出印章的最大寬度。
  • MaxHeight:輸出印章的最大高度。
  • MinWidth:輸出印章的最小寬度。
  • MinHeight:輸出印章的最小高度。
  • 超連結:使此 Stamper 的加蓋元素具有點擊時的超連結功能。 注意:HTML 連結由鏈接創建(一)標籤不是通過蓋章來保留的。
  • 縮放:對印章應用百分比縮放,使其變大或變小。 預設值為100(百分比),這沒有任何效果。
  • IsStampBehindContent:設置為 true 以將印章放置在內容後面。 如果內容不透明,印章可能看不見。
  • WaitFor:一個方便的封裝器,用於等待各種事件或僅僅等待一段時間。
  • 超時:渲染超時(秒)。 預設值為60。
Chaknith related to 探索印章選項

查克尼思·賓

軟體工程師

Chaknith 是開發者界的夏洛克福爾摩斯。他第一次意識到自己可能有個軟體工程的未來,是在他為了娛樂而參加程式挑戰的時候。他的重點是 IronXL 和 IronBarcode,但他也引以為豪的是,他幫助客戶解決所有產品的問題。Chaknith 利用他與客戶直接對話中獲得的知識,以進一步改進產品。他的實際反饋超越了 Jira 工單,並支持產品開發、文件撰寫和行銷,以提升客戶的整體體驗。不在公司時,他通常在學習機器學習、寫程式和徒步旅行。