C#代碼實現(xiàn)為PDF文檔添加印章
為 PDF 文檔添加印章可以增強文檔的視覺 效果,并傳達諸如審批、機密或緊急等重要信息。通過這一簡單操作,用戶可以使用自定義圖片或文本對文檔進行標記,從而更直觀地傳遞關(guān)鍵信息。無論是在辦公場景還是個人項目中,為 PDF 添加印章都能提升文檔的清晰度與專業(yè)感。本文將介紹如何使用 C# 為 PDF 文檔添加印章。
安裝 PDF 組件
開始之前,需要在 .NET 項目中添加用于處理 PDF 的相關(guān) DLL 引用??梢酝ㄟ^下載安裝包手動引用 DLL,也可以直接通過 NuGet 進行安裝。
PM> Install-Package Spire.PDF
前置知識
在 PDF 文檔中,印章(Stamp)是一種注釋或圖形元素,用于向文檔添加補充信息。常見的用途包括標記審批狀態(tài)、機密等級或時間信息等。
在 C# 中,可以通過 PDF 相關(guān)類 庫中的印章注釋對象來創(chuàng)建橡皮章效果,并結(jié)合模板對象作為繪制區(qū)域,在其中添加文本、圖片、圖形以及日期時間等內(nèi)容。
使用 C# 為 PDF 添加動態(tài)印章
動態(tài)印章是一種可自定義的 PDF 注釋,可用于顯示審批狀態(tài)、簽名信息或其他動態(tài)內(nèi)容。與靜態(tài)印章不同,動態(tài)印章中的部分信息可以實時更新,例如當前日期、時間、用戶名或自定義文本等。
下面是為 PDF 添加動態(tài)印章的基本步驟:
- 創(chuàng)建 PdfDocument 對象。
- 使用 PdfDocument.LoadFromFile() 方法加載 PDF 文件。
- 創(chuàng)建指定大小的 PdfTemplate 對象。
- 使用 PdfTemplate.Graphics.DrawString() 在模板中繪制文本,并添加日期、時間等動態(tài)信息。
- 創(chuàng)建 PdfRubberStampAnnotation 對象,并將模板設(shè)置為其外觀。
- 使用 PdfPageBase.Annotations.Add() 方法將印章添加到指定 PDF 頁面。
- 將結(jié)果保存為新的 PDF 文件。
示例代碼如下:
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)
{
// 創(chuàng)建 PdfDocument 對象
PdfDocument doc = new PdfDocument();
// 加載 PDF 文檔
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");
// 獲取指定頁面
PdfPageBase page = doc.Pages[1];
// 創(chuàng)建 PdfTemplate 對象
PdfTemplate template = new PdfTemplate(220, 50);
// 創(chuàng)建兩種字體
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", 16f, FontStyle.Bold), true);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Times New Roman", 10f, FontStyle.Bold), true);
// 創(chuàng)建純色畫刷和漸變畫刷
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);
// 創(chuàng)建畫筆
PdfPen pen = new PdfPen(solidBrush);
// 創(chuàng)建圓角矩形路徑
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));
// 創(chuàng)建橡皮章,并設(shè)置大小和位置
RectangleF rectangle2 = new RectangleF(
55,
page.ActualSize.Height - 55 - 70,
240,
55);
PdfRubberStampAnnotation stamp =
new PdfRubberStampAnnotation(rectangle2);
// 創(chuàng)建 PdfAppearance 對象,并將模板設(shè)置為其普通狀態(tài)外觀
PdfAppearance apprearance = new PdfAppearance(stamp);
apprearance.Normal = template;
// 將外觀應(yīng)用到印章
stamp.Appearance = apprearance;
// 將印章注釋添加到頁面注釋集合
page.Annotations.Add(stamp);
// 保存文件
doc.SaveToFile("DynamicStamp.pdf", FileFormat.PDF);
// 釋放資源
doc.Dispose();
}
}
}使用 C# 為 PDF 添加圖片印章
圖片印章是添加到 PDF 文檔中的一種圖形元素,通常以注釋或覆蓋層的形式顯示。它一般由一張圖片組成,可以放置在 PDF 頁面中的指定位置,用于展示公司 Logo、簽名、水印或?qū)徟鷺擞浀葍?nèi)容。
下面是為 PDF 添加圖片印章的基本步驟:
- 創(chuàng)建 PdfDocument 對象。
- 使用 PdfDocument.LoadFromFile() 方法加載 PDF 文件。
- 創(chuàng)建指定大小的 PdfTemplate 對象。
- 使用 PdfTemplate.Graphics.DrawImage() 方法在模板中繪制圖片。
- 創(chuàng)建 PdfRubberStampAnnotation 對象,并將模板設(shè)置為其外觀。
- 使用 PdfPageBase.Annotations.Add() 方法將印章添加到指定 PDF 頁面。
- 將結(jié)果保存為新的 PDF 文件。
示例代碼如下:
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)
{
// 創(chuàng)建 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;
// 根據(jù)圖片大小創(chuàng)建 PdfTemplate 對象
PdfTemplate template = new PdfTemplate(width, height, true);
// 在模板上繪制圖片
template.Graphics.DrawImage(image, 0, 0, width, height);
// 創(chuàng)建橡皮章注釋,并設(shè)置其位置和大小
RectangleF rect = new RectangleF(
page.ActualSize.Width - width - 50,
page.ActualSize.Height - height - 50,
width,
height);
PdfRubberStampAnnotation stamp =
new PdfRubberStampAnnotation(rect);
// 創(chuàng)建 PdfAppearance 對象
PdfAppearance pdfAppearance = new PdfAppearance(stamp);
// 將模板設(shè)置為外觀的普通狀態(tài)
pdfAppearance.Normal = template;
// 將外觀應(yīng)用到印章
stamp.Appearance = pdfAppearance;
// 將印章注釋添加到 PDF
page.Annotations.Add(stamp);
// 保存文件
doc.SaveToFile("ImageStamp.pdf");
// 釋放資源
doc.Dispose();
}
}
}方法補充
在 C# 中為 PDF 添加印章通常有三種實現(xiàn)路徑:將圖片作為圖像印章插入、生成動態(tài)文本印章,或創(chuàng)建包含多方信息的復(fù)合印章。綜合對比來看,商業(yè)庫 (Aspose/Spire) 適合追求開發(fā)效率和企業(yè)級交付的場景;iText (AGPL) 適合開源項目;PdfPig 則是無許可證風(fēng)險的免費選擇。
主流方案對比速覽
| 對比維度 | Aspose.PDF | Spire.PDF | iText (iTextSharp/iText7) | PdfPig | GroupDocs.Signature |
|---|---|---|---|---|---|
| 優(yōu)勢與劣勢 | 優(yōu)勢:功能全面、API穩(wěn)定且文檔詳盡;劣勢:商業(yè)授權(quán),成本較高 | 優(yōu)勢:API 簡潔直觀,上手快;劣勢:商業(yè)授權(quán) | 優(yōu)勢:功能強大,社區(qū)版開源;劣勢:沿用 AGPL 協(xié)議,閉源商用需購買商業(yè)授權(quán) | 優(yōu)勢:開源免費,MIT 許可,無商業(yè)限制;劣勢:功能相對基礎(chǔ) | 優(yōu)勢:API 語義化,專注于簽名與印章場景;劣勢:商業(yè)授權(quán) |
| 核心特點 | 工業(yè)級 PDF 處理 | 豐富的擴展功能,支持場景多 | 電子簽名與印章的行業(yè)標準 | 輕量級,專注于 PDF 解析 | 簽名專用,功能強大 |
| 代碼量 | 較少 | 很少 | 中等 | 較多 | 較少 |
| 費用 | ?????? | ?????? | ???? (社區(qū)版 AGPL 免費) / ?????? (商業(yè)版) | ?? 免費 | ?????? |
| 適用場景 | 企業(yè)級應(yīng)用,追求穩(wěn)定性和功能完整性 | 快速實現(xiàn)復(fù)雜 PDF 功能,兼顧文檔的生成與編輯 | 數(shù)字簽名、電子印章等安全需求高的場景 | 需要基礎(chǔ)印章功能且預(yù)算有限,追求輕量級和低開銷的開源項目 | 主要關(guān)注文檔數(shù)字簽名,希望快速集成的場景 |
實時建議:如果你的首要目標是 快速實現(xiàn) 且 開發(fā)環(huán)境允許,那么商業(yè)庫 (如 Spire 或 Aspose) 是最省時省力的選擇。 但如果你的項目要求免費且希望主動權(quán)完全掌握在自己手中,PdfPig 是值得嘗試的方向。
以下將以 Aspose.PDF 和 Spire.PDF 為例,提供兩種主流方案的詳細代碼示例。
使用 ImageStamp 類添加圖片印章 (Aspose.PDF)
操作步驟:
- 使用
Document對象加載 PDF,并用ImageStamp創(chuàng)建印章。 - 設(shè)置印章的屬性(如背景、位置、尺寸等)。
- 將印章添加到指定頁面,并保存更新后的文檔。
using Aspose.Pdf;
using Aspose.Pdf.Text;
class Program
{
static void Main(string[] args)
{
const string dataDir = "YOUR DOCUMENT DIRECTORY";
// 1. 加載文檔
Document pdfDocument = new Document(dataDir + "AddImageStamp.pdf");
// 2. 創(chuàng)建基于圖片的印章對象
ImageStamp imageStamp = new ImageStamp(dataDir + "aspose-logo.jpg");
imageStamp.Background = true; // 設(shè)為背景印章
imageStamp.XIndent = 100; // 左邊距
imageStamp.YIndent = 100; // 上邊距
imageStamp.Height = 300; // 高度
imageStamp.Width = 300; // 寬度
imageStamp.Rotate = Rotation.on270; // 旋轉(zhuǎn)角度
imageStamp.Opacity = 0.5; // 透明度 (0.5 = 50%)
// 3. 將印章添加到第一頁
pdfDocument.Pages[1].AddStamp(imageStamp);
// 4. 保存文檔
pdfDocument.Save(dataDir + "AddImageStamp_out.pdf");
}
}(代碼來源:Aspose.PDF 官方文檔)
使用 PdfFileStamp 類批量添加完整印章 (Aspose.PDF Facades)
操作步驟:
- 借助
PdfFileStamp對象,可一鍵連接 PDF 文檔。 - 使用
Stamp類靈活配置印章的圖片、文字及位置參數(shù)。 - 將配置好的單一印章應(yīng)用到文檔的每一頁并輸出最終文件。
using Aspose.Pdf.Facades;
class Program
{
static void Main(string[] args)
{
// 1. 創(chuàng)建 PdfFileStamp 對象并綁定 PDF 文檔
PdfFileStamp fileStamp = new PdfFileStamp();
fileStamp.BindPdf("YOUR_DOCUMENT_DIRECTORY/AddImageStampAll.pdf");
// 2. 創(chuàng)建并配置 Stamp 對象
Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp();
stamp.BindImage("YOUR_DOCUMENT_DIRECTORY/aspose-logo.jpg");
stamp.SetOrigin(200, 200); // 設(shè)置位置(X, Y 坐標)
stamp.Rotation = 90.0F; // 設(shè)置旋轉(zhuǎn)角度
stamp.IsBackground = true; // 設(shè)置為背景
// 3. 將配置好的 Stamp 添加到文檔的所有頁面
fileStamp.AddStamp(stamp);
// 4. 保存并關(guān)閉
fileStamp.Save("YOUR_OUTPUT_DIRECTORY/AddImageStampAll_out.pdf");
fileStamp.Close();
}
}直接撰寫動態(tài)文本印章 (Spire.PDF)
- 加載 PDF 文檔。
- 創(chuàng)建
PdfPageTemplate模板并綁定到目標頁面。 - 在模板上繪制自定義文本及樣式,實現(xiàn)文本印章效果。
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
class Program
{
static void Main(string[] args)
{
// 1. 加載文檔
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");
// 2. 獲取第一頁及頁面尺寸
PdfPageBase page = doc.Pages[0];
System.Drawing.SizeF pageSize = page.Size;
// 3. 創(chuàng)建模板并綁定到頁面
PdfPageTemplate template = new PdfPageTemplate();
template.Size = new SizeF(200, 60);
template.Bounds = new RectangleF(50, pageSize.Height - 60, 200, 60); // 左下角
page.AddTemplate(template);
// 4. 在模板上繪制文本(來客訪案例)
using (PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋體", 12)))
{
template.Graphics.DrawString("申請人:張三", font, PdfBrushes.Black, new PointF(10, 15));
template.Graphics.DrawString("日期:2026-05-08", font, PdfBrushes.Black, new PointF(10, 35));
}
doc.SaveToFile("result.pdf");
doc.Close();
}
}代碼解析:該方案通過 PdfPageTemplate 創(chuàng)建一個與頁面綁定的可繪制區(qū)域,可以在其中直接繪制文本,無需依賴原始圖像素材即可生成動態(tài)的文本印章。
總結(jié)
本文介紹了如何使用 C# 為 PDF 文檔添加動態(tài)印章和圖片印章。通過印章功能,開發(fā)者可以在 PDF 中快速添加審批信息、時間標記、公司 Logo 或其他自定義內(nèi)容,從而提升文檔的專業(yè)性與可讀性。
文章分別演示了動態(tài)印章與圖片印章的實現(xiàn)方式,包括創(chuàng)建模板、繪制文本或圖片、設(shè)置印章外觀以及將印章添加到指定頁面等核心操作。借助這些方法,可以更加靈活地實現(xiàn) PDF 文檔的標記與管理需求。
到此這篇關(guān)于C#代碼實現(xiàn)為PDF文檔添加印章的文章就介紹到這了,更多相關(guān)C# PDF添加印章內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#遞歸實現(xiàn)將一整數(shù)逆序后放入一數(shù)組中
這篇文章主要介紹了C#遞歸實現(xiàn)將一整數(shù)逆序后放入一數(shù)組中,是遞歸算法的一個簡單應(yīng)用,需要的朋友可以參考下2014-10-10
WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
這篇文章主要介紹了WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法,涉及WinForm窗口滾動字幕設(shè)置的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
C#中獲取二維數(shù)組的行數(shù)和列數(shù)以及多維數(shù)組各個維度的長度
這篇文章介紹了C#中獲取二維數(shù)組的行數(shù)和列數(shù)以及多維數(shù)組各個維度的長度,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
C#使用SqlConnection連接到SQL Server的代碼示例
這篇文章主要介紹了C#使用SqlConnection連接到SQL Server的代碼示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
給c#添加SetTimeout和SetInterval函數(shù)
Javascript中的SetTimeout和SetInterval函數(shù)很方便,把他們移植到c#中來。2008-03-03

