可用于审核后的pdf文件签字盖章
具体实现代码如下:
/// <summary>
/// 第一页盖章
/// </summary>
/// <param name="pdfPath">源pdf地址</param>
/// <param name="outPdfPath">盖章后生成pdf地址</param>
/// <param name="imagePath">盖章图片地址</param>
public static void SignFirstPage(string pdfPath, string outPdfPath, string imagePath)
{
//创建盖章后生成pdf
System.IO.Stream outputPdfStream = new System.IO.FileStream(outPdfPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
//读取原有pdf
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfPath);
iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, outputPdfStream);
//获取内容
//GetUnderContent 加在内容下层
//GetOverContent 加在内容上层
//盖章在第一页
iTextSharp.text.pdf.PdfContentByte pdfContentByte = pdfStamper.GetUnderContent(1);
//获取图片
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagePath);
//设置图片比例
image.ScalePercent(40);
//读取pdf文件第一页尺寸
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
//设置图片的绝对位置,位置偏移方向为:左到右,下到上
image.SetAbsolutePosition(psize.Width / 10 * 7, psize.Height / 10);
//图片添加到文档
pdfContentByte.AddImage(image);
pdfStamper.Close();
pdfReader.Close();
//直接打开盖章后文件
System.Diagnostics.Process.Start(outPdfPath);
}
/// <summary>
/// 最后一页盖章
/// </summary>
/// <param name="pdfPath">源pdf地址</param>
/// <param name="outPdfPath">盖章后生成pdf地址</param>
/// <param name="imagePath">盖章图片地址</param>
public static void SignLastPage(string pdfPath, string outPdfPath, string imagePath)
{
//创建盖章后生成pdf
System.IO.Stream outputPdfStream = new System.IO.FileStream(outPdfPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
//读取原有pdf
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfPath);
iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, outputPdfStream);
//读取页数
int pdfPageSize = pdfReader.NumberOfPages;
//获取内容
//GetUnderContent 加在内容下层
//GetOverContent 加在内容上层
//盖章在最后一页
iTextSharp.text.pdf.PdfContentByte pdfContentByte = pdfStamper.GetUnderContent(pdfPageSize);
//获取图片
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagePath);
//设置图片比例
image.ScalePercent(40);
//读取pdf文件第一页尺寸
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
//设置图片的绝对位置,位置偏移方向为:左到右,下到上
image.SetAbsolutePosition(psize.Width / 10 * 7, psize.Height / 10);
//图片添加到文档
pdfContentByte.AddImage(image);
pdfStamper.Close();
pdfReader.Close();
//直接打开盖章后文件
System.Diagnostics.Process.Start(outPdfPath);
}
/// <summary>
/// PDF盖章,循环到每页
/// </summary>
/// <param name="pdfPath">源pdf地址</param>
/// <param name="outPdfPath">盖章后生成pdf地址</param>
/// <param name="imagePath">盖章图片地址</param>
public static void SignEachPage(string pdfPath, string outPdfPath, string imagePath)
{
//读取pdf
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfPath);
//读取页数
int pdfPageSize = pdfReader.NumberOfPages;
//创建新pdf
System.IO.FileStream outputStream = new System.IO.FileStream(outPdfPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, outputStream);
//文件内容
iTextSharp.text.pdf.PdfContentByte waterMarkContent;
//读取第一页尺寸
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
//读取盖章图片
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagePath);
//图片缩放到一定百分比
image.ScalePercent(40);
//设置图片位置,位置偏移方向为:左到右,下到上
image.SetAbsolutePosition(psize.Width / 10 * 7, psize.Height / 10);
//循环给每页盖章
for (int i = 1; i <= pdfPageSize; i++)
{
//GetUnderContent 加在内容下层
//GetOverContent 加在内容上层
waterMarkContent = pdfStamper.GetUnderContent(i);
//添加
waterMarkContent.AddImage(image);
}
pdfStamper.Close();
pdfReader.Close();
//直接打开盖章后文件
System.Diagnostics.Process.Start(outPdfPath);
}
最后效果如下图所示
个人记录,不喜勿喷,谢谢