首页 > 编程语言 >C# Pdf添加文本水印(iTextSharp)

C# Pdf添加文本水印(iTextSharp)

时间:2023-05-05 15:22:33浏览次数:30  
标签:C# text reader System iTextSharp IO pdf Pdf

第一步通过Nuget添加iTextSharp引用

具体实现代码如下:

        /// <summary>
        /// 添加文本水印
        /// </summary>
        /// <param name="pdfPath">pdf文件</param>
        /// <param name="outPath">输出文件位置</param>
        /// <param name="addText">水印文字</param>
        public static void PdfAddWatermark(string pdfPath, string outPath, string addText)
        {
            //读取pdf
            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(pdfPath);
            //创建新pdf
            System.IO.Stream outStream = new System.IO.FileStream(outPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
            iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, outStream); ;
            int pdfTotalPage = reader.NumberOfPages;//总页数
            //第一页pdf尺寸
            iTextSharp.text.Rectangle psize = reader.GetPageSize(1);
            float width = psize.Width;
            float height = psize.Height;

            iTextSharp.text.pdf.PdfContentByte content;
            //【C:\WINDOWS\Fonts】系统字体存放位置
            iTextSharp.text.pdf.BaseFont font = iTextSharp.text.pdf.BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED);
            iTextSharp.text.pdf.PdfGState gs = new iTextSharp.text.pdf.PdfGState();
            for (int i = 1; i <= pdfTotalPage; i++)
            {
                //GetUnderContent内容下层
                //GetOverContent内容上层
                content = stamper.GetUnderContent(i);
                //透明度
                gs.FillOpacity = 0.3f;
                content.SetGState(gs);
                //content.SetGrayFill(0.3f);
                //开始写入文本
                content.BeginText();
                //设置颜色
                content.SetColorFill(iTextSharp.text.BaseColor.GRAY);
                //字体大小
                content.SetFontAndSize(font, 30);
                //设置文本矩阵
                content.SetTextMatrix(0, 0);
                //水印文本位置
                //中间
                content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width / 2, height / 4, -45);
                content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width / 2, height - 120, -45);
                content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width / 2, height - 360, -45);
                //右侧
                content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width - 100, height / 4, -45);
                content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width - 100, height - 120, -45);
                content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width - 100, height - 360, -45);
                //左侧
                content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width - 500, height / 4, -45);
                content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width - 500, height - 120, -45);
                content.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, addText, width - 500, height - 360, -45);
                content.EndText();
            }
            stamper.Close();
            reader.Close();
            System.Diagnostics.Process.Start(outPath);
        }

效果如下所示

 

标签:C#,text,reader,System,iTextSharp,IO,pdf,Pdf
From: https://www.cnblogs.com/lwk9527/p/17374228.html

相关文章

  • C# 通过iTextSharp实现关键字签字盖章(通过在内容中插入盖章图片的形式)
    此功能通过 iTextSharp 读取PDF文档信息,并循环查找每一页PDF文件,在整个PDF中只要是符合条件的地方都会盖章,如只需要在最后一页盖章,请将方法中For循环去掉,并将PdfContentBytecontentByte=pdfStamper.GetUnderContent(i);parser.ProcessContent<PdfLocation>(i,location);......
  • Apache 配置https虚拟主机
    一、安装带ssl的Apache2.2.211、安装apache之前需要先检查openssl是否安装完毕,yumlist"*openssl*",如果没有用yum安装下即可2、apache安装,网上文档很多,以下是专门针对ssl的编译参数#cd/usr/local/src/tarbag#wgethttp://labs.renren.com/apache-mirror//httpd/httpd-2.2......
  • C# 通过ICSharpCode.SharpZipLib实现文件压缩下载
    通过管理NuGet包添加ICSharpCode.SharpZipLib引用以完成,多文件或者文件夹压缩后下载效果1、压缩文件实体类///<summary>///文件路径集合,文件名集合,压缩文件名///</summary>publicclassFileNameListZip{///<summary>///文件路......
  • Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified
    导入一个新的springbootmaven项目启动一直报这个错,查出来的答案都说是加注解把数据库扫描给排除掉,这种方式其实有点鸵鸟,项目原先是没问题的,现在导入到自己的环境启动不起来,那肯定是不能去改动代码的。排查了一遍,发现是项目中的resources文件没有指定成资源文件,所以找不到数据库......
  • C#生成二维码(【ThoughtWorks.QRCode】【QRCoder】【ZXing.Net】)
    1、通过ThoughtWorks.QRCode实现生成二维码,可直接通过添加Nuget包引用///<summary>///ThoughtWorks.QRCode生成二维码///</summary>///<paramname="filePath">二维码生成后保存地址</param>///<paramname="qrCo......
  • Vue3 开发必备的 VSCode 插件
    分享6个Vue3开发必备的VSCode插件,可以直接用过VSCode的插件中心直接安装使用。1、Volar相信使用VSCode开发Vue2的同学一定对Vetur插件不会陌生,作为Vue2配套的VSCode插件,它的主要作用是对Vue单文件组件提供高亮、语法支持以及语法检测。而随着Vue3正式......
  • 《花雕学AI》31:ChatGPT--用关键词/咒语/提示词Prompt激发AI绘画的无限创意!
    你有没有想过用AI来画画?ChatGPT是一款基于GPT-3的聊天模式的AI绘画工具,它可以根据你输入的关键词/咒语/提示词Prompt来生成不同风格和主题的画作。Prompt是一些简短的文字,可以用来指导ChatGPT的创作过程。在这篇文章中,我将展示一些用ChatGPT和不同的Prompt创造出来的有趣和创意的A......
  • C# Spire.PDF 实现pdf文件盖章
    1、添加引用通过Spire.PDF实现合同盖章,社区版dll(免费,但是只支持10页以内的pdf文档),也可以直接通过VS管理NuGet包添加dll引用,收费版直接搜索Spire.PDF安装,免费社区版搜索FreeSpire.PDF安装2、参数定义与调用stringpdfPath="C:\\Users\\Administrator\\Desktop\\2月份工作......
  • 《CTFshow-Web入门》08. Web 71~80
    目录web71知识点题解web72知识点题解web73题解web74题解web75知识点题解web76题解web77知识点题解web78知识点题解web79题解web80知识点题解ctf-web入门web71知识点ob_get_contents():得到输出缓冲区的内容。ob_end_clean():清除缓冲区的内容,并将缓冲区关闭,但不会输出内......
  • C# 通过iTextSharp实现pdf文件盖章(通过在内容中插入盖章图片的形式)
    具体盖章方法实现///<summary>///第一页盖章///</summary>///<paramname="pdfPath">源pdf地址</param>///<paramname="outPdfPath">盖章后生成pdf地址</param>///<paramna......