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 }
附dll下载地址:https://files-cdn.cnblogs.com/files/yhnet/iTextSharp.7z