为 PDF 文档添加印章可以增强文档的视觉效果,并传达诸如审批、机密或紧急等重要信息。通过这一简单操作,用户可以使用自定义图片或文本对文档进行标记,从而更直观地传递关键信息。无论是在办公场景还是个人项目中,为 PDF 添加印章都能提升文档的清晰度与专业感。本文将介绍如何使用 C# 为 PDF 文档添加印章。
安装 PDF 组件
开始之前,需要在 .NET 项目中添加用于处理 PDF 的相关 DLL 引用。可以通过下载安装包手动引用 DLL,也可以直接通过 NuGet 进行安装。
cs
PM> Install-Package Spire.PDF
前置知识
在 PDF 文档中,印章(Stamp)是一种注释或图形元素,用于向文档添加补充信息。常见的用途包括标记审批状态、机密等级或时间信息等。
在 C# 中,可以通过 PDF 相关类库中的印章注释对象来创建橡皮章效果,并结合模板对象作为绘制区域,在其中添加文本、图片、图形以及日期时间等内容。
使用 C# 为 PDF 添加动态印章
动态印章是一种可自定义的 PDF 注释,可用于显示审批状态、签名信息或其他动态内容。与静态印章不同,动态印章中的部分信息可以实时更新,例如当前日期、时间、用户名或自定义文本等。
下面是为 PDF 添加动态印章的基本步骤:
- 创建 PdfDocument 对象。
- 使用
PdfDocument.LoadFromFile()方法加载 PDF 文件。 - 创建指定大小的 PdfTemplate 对象。
- 使用
PdfTemplate.Graphics.DrawString()在模板中绘制文本,并添加日期、时间等动态信息。 - 创建 PdfRubberStampAnnotation 对象,并将模板设置为其外观。
- 使用
PdfPageBase.Annotations.Add()方法将印章添加到指定 PDF 页面。 - 将结果保存为新的 PDF 文件。
示例代码如下:
cs
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Annotations;
using Spire.Pdf.Annotations.Appearance;
namespace AddDynamicStamp
{
class Program
{
static void Main(string[] args)
{
// 创建 PdfDocument 对象
PdfDocument doc = new PdfDocument();
// 加载 PDF 文档
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");
// 获取指定页面
PdfPageBase page = doc.Pages[1];
// 创建 PdfTemplate 对象
PdfTemplate template = new PdfTemplate(220, 50);
// 创建两种字体
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", 16f, FontStyle.Bold), true);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Times New Roman", 10f, FontStyle.Bold), true);
// 创建纯色画刷和渐变画刷
PdfSolidBrush solidBrush = new PdfSolidBrush(Color.Blue);
RectangleF rectangle1 = new RectangleF(new PointF(0, 0), template.Size);
PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(
rectangle1,
new PdfRGBColor(Color.White),
new PdfRGBColor(Color.Blue),
PdfLinearGradientMode.Horizontal);
// 创建画笔
PdfPen pen = new PdfPen(solidBrush);
// 创建圆角矩形路径
int CornerRadius = 10;
PdfPath path = new PdfPath();
path.AddArc(template.GetBounds().X,
template.GetBounds().Y,
CornerRadius,
CornerRadius,
180,
90);
path.AddArc(template.GetBounds().X + template.Width - CornerRadius,
template.GetBounds().Y,
CornerRadius,
CornerRadius,
270,
90);
path.AddArc(template.GetBounds().X + template.Width - CornerRadius,
template.GetBounds().Y + template.Height - CornerRadius,
CornerRadius,
CornerRadius,
0,
90);
path.AddArc(template.GetBounds().X,
template.GetBounds().Y + template.Height - CornerRadius,
CornerRadius,
CornerRadius,
90,
90);
path.AddLine(template.GetBounds().X,
template.GetBounds().Y + template.Height - CornerRadius,
template.GetBounds().X,
template.GetBounds().Y + CornerRadius / 2);
// 在模板上绘制路径
template.Graphics.DrawPath(pen, path);
template.Graphics.DrawPath(linearGradientBrush, path);
// 在模板上绘制文本
String string1 = "APPROVED\n";
String string2 = "By Marketing Manager at " +
DateTime.Now.ToString("HH:mm, MMM dd, yyyy");
template.Graphics.DrawString(string1,
font1,
solidBrush,
new PointF(5, 5));
template.Graphics.DrawString(string2,
font2,
solidBrush,
new PointF(2, 28));
// 创建橡皮章,并设置大小和位置
RectangleF rectangle2 = new RectangleF(
55,
page.ActualSize.Height - 55 - 70,
240,
55);
PdfRubberStampAnnotation stamp =
new PdfRubberStampAnnotation(rectangle2);
// 创建 PdfAppearance 对象,并将模板设置为其普通状态外观
PdfAppearance apprearance = new PdfAppearance(stamp);
apprearance.Normal = template;
// 将外观应用到印章
stamp.Appearance = apprearance;
// 将印章注释添加到页面注释集合
page.Annotations.Add(stamp);
// 保存文件
doc.SaveToFile("DynamicStamp.pdf", FileFormat.PDF);
// 释放资源
doc.Dispose();
}
}
}
使用 C# 为 PDF 添加图片印章
图片印章是添加到 PDF 文档中的一种图形元素,通常以注释或覆盖层的形式显示。它一般由一张图片组成,可以放置在 PDF 页面中的指定位置,用于展示公司 Logo、签名、水印或审批标记等内容。
下面是为 PDF 添加图片印章的基本步骤:
- 创建 PdfDocument 对象。
- 使用
PdfDocument.LoadFromFile()方法加载 PDF 文件。 - 创建指定大小的 PdfTemplate 对象。
- 使用
PdfTemplate.Graphics.DrawImage()方法在模板中绘制图片。 - 创建 PdfRubberStampAnnotation 对象,并将模板设置为其外观。
- 使用
PdfPageBase.Annotations.Add()方法将印章添加到指定 PDF 页面。 - 将结果保存为新的 PDF 文件。
示例代码如下:
cs
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Annotations.Appearance;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddImageStamp
{
class Program
{
static void Main(string[] args)
{
// 创建 PdfDocument 对象
PdfDocument doc = new PdfDocument();
// 加载 PDF 文档
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");
// 获取指定页面
PdfPageBase page = doc.Pages[1];
// 加载图片文件
PdfImage image = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\confidential.png");
// 获取图片的宽度和高度
int width = image.Width;
int height = image.Height;
// 根据图片大小创建 PdfTemplate 对象
PdfTemplate template = new PdfTemplate(width, height, true);
// 在模板上绘制图片
template.Graphics.DrawImage(image, 0, 0, width, height);
// 创建橡皮章注释,并设置其位置和大小
RectangleF rect = new RectangleF(
page.ActualSize.Width - width - 50,
page.ActualSize.Height - height - 50,
width,
height);
PdfRubberStampAnnotation stamp =
new PdfRubberStampAnnotation(rect);
// 创建 PdfAppearance 对象
PdfAppearance pdfAppearance = new PdfAppearance(stamp);
// 将模板设置为外观的普通状态
pdfAppearance.Normal = template;
// 将外观应用到印章
stamp.Appearance = pdfAppearance;
// 将印章注释添加到 PDF
page.Annotations.Add(stamp);
// 保存文件
doc.SaveToFile("ImageStamp.pdf");
// 释放资源
doc.Dispose();
}
}
}
总结
本文介绍了如何使用 C# 为 PDF 文档添加动态印章和图片印章。通过印章功能,开发者可以在 PDF 中快速添加审批信息、时间标记、公司 Logo 或其他自定义内容,从而提升文档的专业性与可读性。
文章分别演示了动态印章与图片印章的实现方式,包括创建模板、绘制文本或图片、设置印章外观以及将印章添加到指定页面等核心操作。借助这些方法,可以更加灵活地实现 PDF 文档的标记与管理需求。