C#给PDF文件添加水印
程序员文章站
2023-11-25 15:34:04
本文实例为大家分享了c#添加pdf文件水印的具体代码,供大家参考,具体内容如下
using system;
using system.collections.g...
本文实例为大家分享了c#添加pdf文件水印的具体代码,供大家参考,具体内容如下
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using itextsharp.text.html.simpleparser; using itextsharp.text.pdf; using itextsharp.text; using system.io; using system.web.ui.htmlcontrols; using system.drawing; //itextsharp.dll version:5.1.10 protected void button1_click(object sender, eventargs e) { string source =@"d:\my.sample\c#net\exoport2pdf\web2\chap1011.pdf"; //模板路径 string output = @"d:\my.sample\c#net\exoport2pdf\web2\chap1012.pdf"; //导出水印背景后的pdf string watermark = @"d:\my.sample\c#net\exoport2pdf\web2\gp0.jpg"; // 水印图片 bool issurrcess = pdfwatermark(source, output, watermark, 10, 10); } public bool pdfwatermark(string inputfilepath, string outputfilepath, string modelpicname, float top, float left) { //throw new notimplementedexception(); pdfreader pdfreader = null; pdfstamper pdfstamper = null; try { pdfreader = new pdfreader(inputfilepath); int numberofpages = pdfreader.numberofpages; itextsharp.text.rectangle psize = pdfreader.getpagesize(1); float width = psize.width; float height = psize.height; pdfstamper = new pdfstamper(pdfreader, new filestream(outputfilepath, filemode.create)); pdfcontentbyte watermarkcontent; itextsharp.text.image image = itextsharp.text.image.getinstance(modelpicname); image.grayfill = 20;//透明度,灰色填充 //image.rotation//旋转 //image.rotationdegrees//旋转角度 //水印的位置 if (left < 0) { left = width - image.width + left; } image.setabsoluteposition(left, (height - image.height) - top); //每一页加水印,也可以设置某一页加水印 for (int i = 1; i <= numberofpages; i++) { watermarkcontent = pdfstamper.getundercontent(i); watermarkcontent.addimage(image); } //strmsg = "success"; return true; } catch (exception ex) { ex.message.trim(); return false; } finally { if (pdfstamper != null) pdfstamper.close(); if (pdfreader != null) pdfreader.close(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。