C# 通过iTextSharp实现pdf文件盖章(通过在内容中插入盖章图片的形式)

发布时间 2023-05-05 14:58:43作者: 流纹

具体盖章方法实现

    /// <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);
        }
View Code
    /// <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);
        }
View Code
     /// <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);
        }
View Code

效果图如下

 

 

参考文章:C# PdfStamper.GetOverContent方法代码示例 - 纯净天空 (vimsky.com)