首页 > 编程语言 >C# 通过iTextSharp实现pdf文件盖章(通过在内容中插入盖章图片的形式)

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

时间:2023-05-05 15:00:24浏览次数:41  
标签:盖章 C# text System iTextSharp IO 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);
        }
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)

 

标签:盖章,C#,text,System,iTextSharp,IO,pdf
From: https://www.cnblogs.com/lwk9527/p/17374151.html

相关文章

  • C# 生成印章
    1、界面实现及按钮事件 ///点击按钮事件privatevoidbutton2_Click(objectsender,EventArgse){try{stringimageUrl="C:\\Users\\Administrator\\Desktop\\新建文件夹(2)";stringimageForm......
  • bootstrap-select组件
    bootstrap-select组件mmkkuoi于2021-10-1312:08:55发布10178收藏19分类专栏:js文章标签:bootstrapselect版权华为云开发者联盟该内容已被华为云开发者联盟社区收录加入社区js专栏收录该内容2篇文章0订阅订阅专栏阅读目录一、组件开源地址以及API说......
  • ArcGIS Pro创建、发布、调用GP服务全过程示例(等高线分析)
    在之前的文章介绍过使用ArcMap发布GP分析服务,由于ArcGIS后续不在更新ArcMap,改用ArcGISPro,本文对ArcGISPro发布GP分析服务进行说明。本文以等高线分析为例,使用ArcGISPro软件,从GP分析服务的创建、发布、调用全过程进行演示。使用ArcMap发布GP服务请跳转:本文示例使用(因为本人po......
  • CT感应取电无线测温解决高压柜内测温难题
    安科瑞虞佳豪随着社会经济的不断发展,电力系统向着高电压、高容量的方向前进着,电力系统全新的技术与设备层出不穷,电力的输送能力不断提升。然而,高压电气设备承载的高压电力负荷也让其自身的温升问题成为了威胁电网稳定的元凶,设备温度已经成为了当下电网输电设备稳定运行的重要参数......
  • STM32单片机引脚要职能配置为输入或者输出模式,并不能像51一样准双向,那么如何进行但总
    如题随便找个端口举例对应的程序为 难道需要写之后立即初始化为输入?然后赶紧读?然后再赶紧初始化为输出?再往外写?是的,还真他妈就是这么傻逼的操作 ......
  • ssh远程连接报错ssh_exchange_identification: Connection closed by remote host
    被远程主机拒绝此类报错为原因1:ssh连接数量过多导致如果问题是偶尔能登录一次,大多不能登录,建议往第一点方向排查[root@localhost~]#cat/etc/ssh/sshd_config|grepMaxSessions#MaxSessions10[root@localhost~]#cat/etc/ssh/sshd_config|grepMaxStart#MaxStartups10......
  • CosineSimilarity
    余弦相似度implementation'org.apache.commons:commons-text:1.10.0'MeasurestheCosinesimilarityoftwovectorsofaninnerproductspaceandcomparestheanglebetweenthem.ForfurtherexplanationabouttheCosineSimilarity,refertohttp://en.......
  • el-select数据太多造成页面卡顿?el-select实现触底加载
    当我们使用el-select下拉框的时候,会遇到后端放回的数据太过庞大(成千上万条),导致页面渲染的时候造成卡顿现象。这时候我们可以利用触底加载方法减少资源的消耗,避免页面卡顿。思路:这时候我们可以利用vue的自定义指令,监听到他的下拉滚动事件,当滚动到最后时,(下拉宽高度+可滑动高度距离......
  • 带序号的echart
    letoption={backgroundColor:"transparent",grid:{left:'50',right:'80',bottom:0,top:20,containLabel:false,},xAxis:[{gridIndex:0,......
  • C#通过Spire.OCR读取图片文字
    1、项目属性修改首先创建一个winform窗体程序,然后将其目标平台属性修改为【×64】,【Spire.OCR】只支持【×64】,所以这一步不能少  2、添加引用通过管理Nuget包实现添加引用步骤 3、包安装完成后将包中dll复制到项目文件夹>bin>Debug目录下 4、代码中实现选择图片并......