C#利用iTextSharp组件给PDF文档添加图片/文字水印
最近在做关于PDF文档添加水印的功能,折腾了好久,终于好了。以下做个记录:
首先会用到iTextSharp组件,大家可以去官网下载,同时我也会在本文中附加进来。
代码中添加引用为:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingiTextSharp.text.pdf; usingSystem.IO; usingiTextSharp.text;
创建一个显示指定图片的pdf
//////创建一个显示指定图片的pdf /// ////// /// publicstaticboolCreatePDFByPic(stringpicPdfPath,stringpicPath) { //新建一个文档 Documentdoc=newDocument(); try { //建立一个书写器(Writer)与document对象关联 PdfWriter.GetInstance(doc,newFileStream(picPdfPath,FileMode.Create,FileAccess.ReadWrite)); //打开一个文档 doc.Open(); //向文档中添加内容 Imageimg=Image.GetInstance(picPath); //img.SetAbsolutePosition(); doc.Add(img); returntrue; } catch(Exceptionex) { returnfalse; throwex; } finally { if(doc!=null) { doc.Close(); } } }
为PDF文档添加图片水印
//////加图片水印 /// ////// /// /// /// /// publicstaticboolPDFWatermark(stringinputfilepath,stringoutputfilepath,stringModelPicName,floattop,floatleft) { //thrownewNotImplementedException(); PdfReaderpdfReader=null; PdfStamperpdfStamper=null; try { pdfReader=newPdfReader(inputfilepath); intnumberOfPages=pdfReader.NumberOfPages; iTextSharp.text.Rectanglepsize=pdfReader.GetPageSize(1); floatwidth=psize.Width; floatheight=psize.Height; pdfStamper=newPdfStamper(pdfReader,newFileStream(outputfilepath,FileMode.Create)); PdfContentBytewaterMarkContent; iTextSharp.text.Imageimage=iTextSharp.text.Image.GetInstance(ModelPicName); image.GrayFill=20;//透明度,灰色填充 //image.Rotation//旋转 //image.RotationDegrees//旋转角度 //水印的位置 if(left<0) { left=width/2-image.Width+left; } //image.SetAbsolutePosition(left,(height-image.Height)-top); image.SetAbsolutePosition(left,(height/2-image.Height)-top); //每一页加水印,也可以设置某一页加水印 for(inti=1;i<=numberOfPages;i++) { //waterMarkContent=pdfStamper.GetUnderContent(i);//内容下层加水印 waterMarkContent=pdfStamper.GetOverContent(i);//内容上层加水印 waterMarkContent.AddImage(image); } //strMsg="success"; returntrue; } catch(Exceptionex) { throwex; } finally { if(pdfStamper!=null) pdfStamper.Close(); if(pdfReader!=null) pdfReader.Close(); } }
添加普通偏转角度文字水印
//////添加普通偏转角度文字水印 /// ////// /// /// publicstaticvoidsetWatermark(stringinputfilepath,stringoutputfilepath,stringwaterMarkName) { PdfReaderpdfReader=null; PdfStamperpdfStamper=null; try { pdfReader=newPdfReader(inputfilepath); pdfStamper=newPdfStamper(pdfReader,newFileStream(outputfilepath,FileMode.Create)); inttotal=pdfReader.NumberOfPages+1; iTextSharp.text.Rectanglepsize=pdfReader.GetPageSize(1); floatwidth=psize.Width; floatheight=psize.Height; PdfContentBytecontent; BaseFontfont=BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF",BaseFont.IDENTITY_H,BaseFont.EMBEDDED); PdfGStategs=newPdfGState(); for(inti=1;i 添加倾斜水印
//////添加倾斜水印 /// ////// /// /// /// /// publicstaticvoidsetWatermark(stringinputfilepath,stringoutputfilepath,stringwaterMarkName,stringuserPassWord,stringownerPassWord,intpermission) { PdfReaderpdfReader=null; PdfStamperpdfStamper=null; try { pdfReader=newPdfReader(inputfilepath); pdfStamper=newPdfStamper(pdfReader,newFileStream(outputfilepath,FileMode.Create)); //设置密码 //pdfStamper.SetEncryption(false,userPassWord,ownerPassWord,permission); inttotal=pdfReader.NumberOfPages+1; PdfContentBytecontent; BaseFontfont=BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF",BaseFont.IDENTITY_H,BaseFont.EMBEDDED); PdfGStategs=newPdfGState(); gs.FillOpacity=0.2f;//透明度 intj=waterMarkName.Length; charc; intrise=0; for(inti=1;i =15) { content.SetTextMatrix(200,120); for(intk=0;k 调用方法的例子:
stringpath="D://my.pdf"; HtmlToPDFUtil.HtmlToPdf(Request.Url.AbsoluteUri,path); //HtmlToPDFUtil.HtmlToPdf(sbUrlList.ToString(),path); PDFSetWaterMark.PDFWatermark(path,"D://my.pdf",Server.MapPath("/HtmlToPdf/Tools/sy.bmp"),0,0); PDFSetWaterMark.setWatermark("D://my.pdf","D://my2.pdf","TEST"); //PDFSetWaterMark.setWatermark("D://my.pdf","D://my2.pdf","TEST","","",1);附件:iTextSharp
总结
到此这篇关于C#利用iTextSharp组件给PDF文档添加图片/文字水印的文章就介绍到这了,更多相关iTextSharp组件给PDF添加水印内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。