本文实例为大家分享了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.10protected 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.Set@R_403_4613@position(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#给PDF文件添加水印全部内容,希望文章能够帮你解决C#给PDF文件添加水印所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)