C#给PDF文件添加水印
本文实例为大家分享了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(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
上一篇:深入理解C&#9839; 7.0中的Tuple特性
栏 目:C#教程
本文标题:C#给PDF文件添加水印
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5820.html
您可能感兴趣的文章
- 01-10C#实现多线程下载文件的方法
- 01-10C#文件断点续传实现方法
- 01-10C#实现多线程写入同一个文件的方法
- 01-10C#编程获取资源文件中图片的方法
- 01-10C#实现读取被进程占用的文件实现方法
- 01-10C#删除只读文件或文件夹(解决File.Delete无法删除文件)
- 01-10C# readnodefile()不能读取带有文件名为汉字的osg文件解决方法
- 01-10C#路径,文件,目录及IO常见操作汇总
- 01-10C#获取动态生成的CheckBox值
- 01-10winform实现拖动文件到窗体上的方法


阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频 器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-11ajax实现页面的局部加载
- 04-02jquery与jsp,用jquery
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10SublimeText编译C开发环境设置
- 01-10C#中split用法实例总结
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文