zoukankan      html  css  js  c++  java
  • C# PDF文件添加水印

    使用iTextSharp对pdf文件添加水印,代码如下:

    PdfReader pdfReader = null;
                PdfStamper pdfStamper = null;
                FileStream fileStream = null;
                try
                {
                    pdfReader = new PdfReader(filePath);
                    fileStream = new FileStream(outfilePath, FileMode.Create);
                    pdfStamper = new PdfStamper(pdfReader, fileStream);
                    int total = pdfReader.NumberOfPages;
                    iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
                    PdfContentByte content;  
    
                    #region 添加水印信息
                    //遮挡身份证
                    //iTextSharp.text.Image jia_identity = iTextSharp.text.Image.GetInstance(imgInfoPath);
                    //jia_identity.SetAbsolutePosition(180, 695);  //水印的位置
                    //content = pdfStamper.GetOverContent(2);  //要放置的页数
                    //content.AddImage(jia_identity);
    
                    //遮挡姓名
                    //iTextSharp.text.Image yi_name = iTextSharp.text.Image.GetInstance(imgInfoPath);
                    //yi_name.SetAbsolutePosition(210, 655);  //水印的位置
                    ////yi_name.ScalePercent(4f);  //图片比例
                    //content = pdfStamper.GetOverContent(2);  //要放置的页数
                    //content.AddImage(yi_name);
                    #endregion
                    
                    //增加隐形文本域,方便iphone5的 ios10版本看到甲乙双方的签名数据
                    TextField yi_sign = new TextField(pdfStamper.Writer, new iTextSharp.text.Rectangle(460, 430, 461, 431), "date");
                    yi_sign.BackgroundColor = BaseColor.WHITE;
                    yi_sign.BorderWidth = 1;
                    yi_sign.BorderColor = BaseColor.BLACK;
                    yi_sign.BorderStyle = 4;
                    yi_sign.FontSize = 11f;
                    pdfStamper.AddAnnotation(yi_sign.GetTextField(), 9);
                    pdfStamper.FormFlattening = true;
    
                    return true;
                }
                catch (Exception ex)
                {
                    WebAPIHelper.RecordOperationLog("转换pdf文件出错:", ex.Message);
                    return false;
                }
                finally
                {
                    #region 释放资源
                    if (pdfStamper != null)
                    {
                        pdfStamper.Close();
                    }
                    if (pdfReader != null)
                    {
                        pdfReader.Close();
                    }
                    if (fileStream != null)
                    {
                        fileStream.Close();
                        fileStream.Dispose();
                    }
                    #endregion
                }
    View Code

    附dll下载地址:https://files-cdn.cnblogs.com/files/yhnet/iTextSharp.7z

  • 相关阅读:
    使用RedisDesktopManager工具,解决连接失败问题
    安装redis,搭建环境
    CentOS 7下GitLab搭建及配置
    Metasploit之Hash攻击(Hashdump、Quarks PwDump、Windows Credentials Editor、Mimikatz)
    Metasploit之令牌窃取
    Metasploit之漏洞利用( Metasploitable2)
    Metasploit之主机扫描
    Metasploit模块简介
    WAF的那些事
    XXE漏洞介绍 & XXE漏洞攻击 & 修复建议
  • 原文地址:https://www.cnblogs.com/yhnet/p/12447480.html
Copyright © 2011-2022 走看看