跳至页脚内容
使用IRONPDF

如何在C#中添加PDF印章

在PDF上添加文字和图片涉及将附加内容覆盖在现有PDF文档上。 这些内容通常称为“印章”,可以是文字、图片或两者的结合。 通常,用户使用印章在PDF上添加信息、标签、水印或注释。

  1. TextStamper:TextStamper是您向PDF添加描述性文本的解决方案。 无论您是要增强现有文档,在同一页面上放置文本,还是加入其他PDF文档中的详细信息,此印章器都允许您用文件描述和信息自定义您的文件。
  2. ImageStamper:ImageStamper是将图像置于PDF中的首选工具。 无论是用于文件描述的徽标、现有文档的插图,还是用于同一页面或其他PDF文档的视觉元素,此印章器可确保图像的无缝集成。
  3. HtmlStamper:HtmlStamper将自定义提升到一个新水平,允许您将HTML内容打印到PDF上。 这包括创建动态元素,如互动内容、描述和文件规范,为传统PDF自定义之外提供灵活性。
  4. BarcodeStamper用于打印条形码:BarcodeStamper简化了向PDF中添加条形码的过程。 无论是用于跟踪签署的文档、临时文件或文件附件,此印章器都可确保条形码在PDF中的高效集成。
  5. BarcodeStamper用于打印二维码:BarcodeStamper 专注于在PDF中放置二维码。 非常适合创建互动内容或文件附件,此印章器允许您在同一页面或其他PDF文档上嵌入二维码,确保轻松获取附加信息。

这些专用印章类使用户可以轻松增强PDF文档,添加从基本文本到复杂的HTML设计和动态条形码等各种元素。 This article will explore the functionalities of three main stampers: Adding Text with TextStamper, Placing Images with ImageStamper, and Integrating HTML with HtmlStamper. HTMLStamper特别强大,因为它可以利用所有HTML特性,加上CSS样式,为印章过程增添了额外的多样性。

如何在PDF上印章文字和图像

  1. 下载C#库以印章文字和图像。
  2. 创建并配置所需的印章类。
  3. 使用'ApplyStamp'方法将印章应用到PDF。
  4. 使用'ApplyMultipleStamps'方法应用多个印章。
  5. 指定特定页面以应用这些印章。

在PDF中配置和应用文字印章

首先,从TextStamper类创建对象以支持在PDF中印章文字。 该类的对象包含所有配置,以指定如何呈现文字印章。 将textStamper对象传递给'ApplyStamp'方法。 Text属性定义要在PDF上显示的内容。

此外,可以指定字体、字型以及印章的位置。这种自定义扩展到互动元素、文件描述和PDF上同一页或其他页面的现有内容。 然后,用实际文件名导出PDF。

完成配置后,使用指定的文件名导出输出PDF文件,封装所有设置,提升文档的专业性。

using IronPdf;
using IronPdf.Editing;

// Initialize the PDF renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PDF document from HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

// Create a TextStamper object and configure its properties
TextStamper textStamper = new TextStamper()
{
    Text = "Text Stamper!",
    FontFamily = "Bungee Spice",
    UseGoogleFont = true,
    FontSize = 30,
    IsBold = true,
    IsItalic = true,
    VerticalAlignment = VerticalAlignment.Top,
};

// Apply the text stamp to the PDF document
pdf.ApplyStamp(textStamper);

// Save the modified PDF document
pdf.SaveAs("stampText.pdf");
using IronPdf;
using IronPdf.Editing;

// Initialize the PDF renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PDF document from HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

// Create a TextStamper object and configure its properties
TextStamper textStamper = new TextStamper()
{
    Text = "Text Stamper!",
    FontFamily = "Bungee Spice",
    UseGoogleFont = true,
    FontSize = 30,
    IsBold = true,
    IsItalic = true,
    VerticalAlignment = VerticalAlignment.Top,
};

// Apply the text stamp to the PDF document
pdf.ApplyStamp(textStamper);

// Save the modified PDF document
pdf.SaveAs("stampText.pdf");
Imports IronPdf
Imports IronPdf.Editing

' Initialize the PDF renderer
Private renderer As New ChromePdfRenderer()

' Create a PDF document from HTML content
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>")

' Create a TextStamper object and configure its properties
Private textStamper As New TextStamper() With {
	.Text = "Text Stamper!",
	.FontFamily = "Bungee Spice",
	.UseGoogleFont = True,
	.FontSize = 30,
	.IsBold = True,
	.IsItalic = True,
	.VerticalAlignment = VerticalAlignment.Top
}

' Apply the text stamp to the PDF document
pdf.ApplyStamp(textStamper)

' Save the modified PDF document
pdf.SaveAs("stampText.pdf")
$vbLabelText   $csharpLabel

配置和应用PDF中的图像印章

与文字印章类似,从ImageStamper类创建一个对象,然后使用ImageStamper应用方法将图像应用到文档中。 此方法的第二个参数也可以包含页面索引,允许将印章应用到单个或多个页面。 此特定实例可以指示系统将图像作为印章应用,特别是在PDF的第一页。

所有页码索引都是基于零的索引。

using IronPdf;
using IronPdf.Editing;

// Initialize the PDF renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PDF document from HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

// Create an ImageStamper object with the image URL
ImageStamper imageStamper = new ImageStamper(new Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg"))
{
    VerticalAlignment = VerticalAlignment.Top,
};

// Apply the image stamp to the first page of the PDF document
pdf.ApplyStamp(imageStamper, 0);

// Save the modified PDF document
pdf.SaveAs("stampImage.pdf");
using IronPdf;
using IronPdf.Editing;

// Initialize the PDF renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PDF document from HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

// Create an ImageStamper object with the image URL
ImageStamper imageStamper = new ImageStamper(new Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg"))
{
    VerticalAlignment = VerticalAlignment.Top,
};

// Apply the image stamp to the first page of the PDF document
pdf.ApplyStamp(imageStamper, 0);

// Save the modified PDF document
pdf.SaveAs("stampImage.pdf");
Imports IronPdf
Imports IronPdf.Editing

' Initialize the PDF renderer
Private renderer As New ChromePdfRenderer()

' Create a PDF document from HTML content
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>")

' Create an ImageStamper object with the image URL
Private imageStamper As New ImageStamper(New Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg")) With {.VerticalAlignment = VerticalAlignment.Top}

' Apply the image stamp to the first page of the PDF document
pdf.ApplyStamp(imageStamper, 0)

' Save the modified PDF document
pdf.SaveAs("stampImage.pdf")
$vbLabelText   $csharpLabel

应用多个印章

要向文档添加多个印章,请使用IronPDF中的应用多个印章方法,并传递一组印章器。 它让您可以一次添加各种元素,如文字、图片或标签。 在这个例子中,创建了两个具有不同文本和对齐方式的文字印章,pdf.ApplyMultipleStamps应用了这两个印章到PDF上,最终文档保存为multipleStamps.pdf。 此方法简化了添加各种印章的过程,提供了一种便利的方法来使用多个元素增强PDF,是否在同一页面、另一个PDF,甚至是空白页上。

using IronPdf;
using IronPdf.Editing;

// Initialize the PDF renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PDF document from HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

// Create two TextStamper objects with different configurations
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,
};

// Add the stampers to an array
Stamper[] stampersToApply = { stamper1, stamper2 };

// Apply multiple stamps to the PDF document
pdf.ApplyMultipleStamps(stampersToApply);

// Save the modified PDF document
pdf.SaveAs("multipleStamps.pdf");
using IronPdf;
using IronPdf.Editing;

// Initialize the PDF renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PDF document from HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

// Create two TextStamper objects with different configurations
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,
};

// Add the stampers to an array
Stamper[] stampersToApply = { stamper1, stamper2 };

// Apply multiple stamps to the PDF document
pdf.ApplyMultipleStamps(stampersToApply);

// Save the modified PDF document
pdf.SaveAs("multipleStamps.pdf");
Imports IronPdf
Imports IronPdf.Editing

' Initialize the PDF renderer
Private renderer As New ChromePdfRenderer()

' Create a PDF document from HTML content
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>")

' Create two TextStamper objects with different configurations
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
}

' Add the stampers to an array
Private stampersToApply() As Stamper = { stamper1, stamper2 }

' Apply multiple stamps to the PDF document
pdf.ApplyMultipleStamps(stampersToApply)

' Save the modified PDF document
pdf.SaveAs("multipleStamps.pdf")
$vbLabelText   $csharpLabel

在PDF文档上指定印章位置

要定义印章的位置,使用三行三列的3x3网格。 您可以选择水平对齐:左、中和右,以及垂直对齐:上、中和下。 您可以为每个位置调整水平和垂直偏移量以获得更高的精度。 请参见下图以获得此概念的视觉表示。

如何在C#中添加PDF印章,图1:PDF印章定位 PDF印章定位

  • HorizontalAlignment:相对于页面的印章水平对齐。
  • VerticalAlignment:相对于页面的印章垂直对齐。
  • HorizontalOffset:水平偏移。 默认值是0,默认单位是IronPdf.Editing.MeasurementUnit.Percentage。 正值表示向右的偏移,而负值表示向左的偏移。
  • VerticalOffset:垂直偏移。 默认值是0,默认单位是IronPdf.Editing.MeasurementUnit.Percentage。 正值表示向下的偏移,而负值表示向上的偏移。

要指定HorizontalOffsetVerticalOffset属性,实例化指定长度类以获得详细的测量。 Length的默认测量单位是百分比,但也可以使用英寸、毫米、厘米、像素和点等测量单位。

using IronPdf.Editing;

// Create an ImageStamper object with an image URL
ImageStamper imageStamper = new ImageStamper(new Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg"))
{
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Top,

    // Specify offsets for precise positioning
    HorizontalOffset = new Length(10), // 10% offset to the right
    VerticalOffset = new Length(10), // 10% offset downward
};
using IronPdf.Editing;

// Create an ImageStamper object with an image URL
ImageStamper imageStamper = new ImageStamper(new Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg"))
{
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Top,

    // Specify offsets for precise positioning
    HorizontalOffset = new Length(10), // 10% offset to the right
    VerticalOffset = new Length(10), // 10% offset downward
};
Imports IronPdf.Editing

' Create an ImageStamper object with an image URL
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

在PDF中配置和应用HTML印章

还有另一个印章类可以用来同时印章文字和图像。 用于HTML集成的HtmlStamper类可以呈现带有CSS样式的HTML设计,并将其印章到PDF文档上。 InnerHtmlBaseUrl属性用于指定HTML字符串资源(如CSS和图像文件)的基本URL。

HtmlStamper类应用于PDF。 此印章对象包括一个图像和文字,您可以在要印章到PDF上的HTML片段中定义这些。 JavaScript、CSS和图像文件的所有外部引用都将相对于内部Html属性。 此代码允许您根据HTML内容中提到的具体文件规格自定义PDF。 最后,修改后的PDF以文件名'stampHtml.pdf'保存。

using IronPdf;
using IronPdf.Editing;

// Initialize the PDF renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PDF document from HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

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

// Apply the HTML stamp to the PDF document
pdf.ApplyStamp(htmlStamper);

// Save the modified PDF document
pdf.SaveAs("stampHtml.pdf");
using IronPdf;
using IronPdf.Editing;

// Initialize the PDF renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PDF document from HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");

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

// Apply the HTML stamp to the PDF document
pdf.ApplyStamp(htmlStamper);

// Save the modified PDF document
pdf.SaveAs("stampHtml.pdf");
Imports IronPdf
Imports IronPdf.Editing

' Initialize the PDF renderer
Private renderer As New ChromePdfRenderer()

' Create a PDF document from HTML content
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>")

' Create an HtmlStamper object and configure its properties
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
}

' Apply the HTML stamp to the PDF document
pdf.ApplyStamp(htmlStamper)

' Save the modified PDF document
pdf.SaveAs("stampHtml.pdf")
$vbLabelText   $csharpLabel

HTML印章选项

除了上述提到并解释的选项,以下是更多可供印章类使用的选项。

  • Opacity:允许印章透明。 0是完全不可见的,而100是完全不透明的。
  • Rotation:按顺时针方向从0到360度旋转印章。
  • MaxWidth:输出印章的最大宽度。
  • MaxHeight:输出印章的最大高度。
  • MinWidth:输出印章的最小宽度。
  • MinHeight:输出印章的最小高度。
  • Hyperlink:使此印章机的印章元素具有点击超链接。 注意:在最终输出中,链接(a)标签创建的HTML链接不会通过印章保留。
  • Scale:将百分比比例应用于印章,以使其变大或变小。 默认值为100(百分比),没有效果。
  • IsStampBehindContent:设置为true,以使印章在内容后面应用。 如果内容不透明,印章可能不可见。
  • WaitFor:一个方便的包装器,用于等待各种事件或一些时间。
  • Timeout:渲染超时,单位为秒。 默认值为60。

IronPDF的印章选项提供高级自定义功能,允许用户通过透明度、精确旋转和控制尺寸来增强PDF。 像超链接和缩放这样的功能便于引入所有互动元素,遵循文件规格,仅突出显示内容。 IsStampBehindContent选项战略性地将印章放置在确保它们是同一对象的一部分,而不是字段上。 同时,WaitFor功能有效地管理渲染事件,使IronPDF成为PDF自定义的多功能工具,包括原始页面旋转。

结论

总结来说,IronPDF的印章功能为增强PDF文档提供了多样化和用户友好的解决方案。 无论是添加简单的文字标签、引入图像,还是利用HTML和CSS的强大功能与HTMLStamper结合,IronPDF都可以满足广泛的自定义需求。

使用方便和展示文字和图像印章应用的实用示例,使其对于具有不同技术水平的用户都易于理解。印章选项包括不透明度、旋转和缩放,形成一个综合工具包,使用户能够轻松自定义PDF。 IronPDF的印章功能作为一个可靠且高效的工具,赋予用户轻松提升其PDF文档的能力。

Essentially, Mastering PDF Enhancements with IronPDF effortlessly elevates PDFs for both basic and advanced needs including Extracting Embedded Texts and Images, Handling PDF Forms with Ease, Efficient Merging or Splitting of PDF Files, and Formatting PDFs with Custom Headers and Footers programmatically. 如有疑问或功能请求,IronPDF支持团队乐意为您提供帮助。

常见问题解答

什么是 PDF 盖章,它如何在 C# 中使用?

PDF 盖章涉及向现有 PDF 文档中添加文本、图像或 HTML 内容。在 C# 中,可以使用 IronPDF 的盖章类如 TextStamperImageStamper 高效地应用这些元素。

有哪些盖章类可用于 PDF 自定义?

IronPDF 提供了若干专门的盖章类,包括用于文本的 TextStamper,用于图像的 ImageStamper,用于 HTML 内容的 HtmlStamper,以及用于条形码和 QR 码的 BarcodeStamper

如何在 C# 中将文字盖章应用于 PDF?

要应用文字盖章,创建一个 TextStamper 对象,设置文本内容和字体等属性,然后使用 ApplyStamp 方法将其应用于 PDF。

我可以将 HTML 内容嵌入到 PDF 文档中吗?

可以,使用 IronPDF 的 HtmlStamper,可以将 HTML 内容嵌入到 PDF 文档中,允许具有动态和样式化的元素。

如何控制印章在 PDF 页面上的放置位置?

IronPDF 允许您使用 3x3 网格系统来对齐并通过水平和垂直偏移进行精确调整来控制印章的放置。

有哪些选项可用于自定义 PDF 盖章?

在 IronPDF 中,可以通过不透明度、旋转、缩放、超链接以及在现有内容后面分层的能力等选项自定义 PDF 盖章。

如何将多种类型的盖章应用于单个 PDF 文档?

使用 IronPDF,可以使用 ApplyMultipleStamps 方法高效地将各种类型的盖章(如文本和图像)应用于单个 PDF 文档。

可以向 PDF 添加 QR 代码吗?

可以,您可以使用 IronPDF 的 BarcodeStamper 向 PDF 添加 QR 代码,这可用于交互和信息目的。

盖章抽象类在 PDF 盖章中起到什么作用?

IronPDF 中的盖章抽象类是创建专门盖章类的基础,提供盖章操作的共同功能。

如何在 C# 中将图像盖章应用于 PDF?

创建一个 ImageStamper 对象,配置其属性,并使用 ApplyStamp 方法,任选页索引,将图像盖章至 PDF 文档。

IronPDF 是否支持 .NET 10 中的 PDF Stamper 类?

是的,IronPDF 完全兼容 .NET 10。该库不仅支持 .NET 10,还支持更早的版本,例如 .NET 9、.NET 8、.NET Core、.NET Standard 和 .NET Framework。这意味着所有图章类(包括TextStamperImageStamperHtmlStamperBarcodeStamper )都可以在 .NET 10 项目中直接使用,无需任何修改。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。