资源描述:
《C#-添加PDF水印(文本水印、图片水印)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、C#添加PDF水印一一文本水印、图片水印概述一般我们在向文档添加水印时,会分为直接添加文字水印和加载图片添加图片水印两种情况。常见的,在添加文字水印时会多以声明文档版权、权威性的文字、标语或者名称等;同样的,图片水印也通常可以是某组织的LOGO、印章、或者其他能够指示性的图片等。在下面的文档中,将介绍通过C#编程来添加PDF水印的方法,包括添加文本水印、图片水印两种。使用工具Spire.PDFfor.NETVisualStudio【示例1】添加PDF文本水印usingSpire・Pdf;usingSpire・Pdf
2、・Annotations;usingSpire・Pdf・Annotations・Appearance;usingSpire・Pdf.Graphics;usingSystem;usingSystem.Drawing;namespaceTextWatermark{classProgramstaticvoidMain(stringf]args){//创建PdfDocument对象PdfDocumentpdf=new卩dfDocument();//加载现有PDF文档pdf・LoadEromFile(''sample・pdf
3、");//创建TrueType字体PdfTrueTypcFontfont=newPdfTrucTypcFont(newFont("宋体〃,20f),true);//水印文字stringtext=〃版权所有侵权必究";//测量文字所占的位置大小,即高宽SizeFfontSize=font.MeasureString(text);//计算两个偏移量floatoffset1=(float)(fontSize.Width*System.Math.Sqrt(2)/4);floatoffset2=(float)(fontS
4、ize.Height*System.Math.Sqrt(2)/4);〃遍历文档每一页foreach(PdfPagcBascpageinpdf.Pages){//创建PdfTilingBrush对象,指定大小PdfTilingBrushbrush二newPdfTi1ingBrush(newSizeF(page.Canvas.Size.Width/2,page.Canvas.Size.Height/2));〃设置画刷透明度brush.Graphics.SetTransparcncy(0・9f);//将画刷中坐标系向右下
5、平移brush.Graphics.Transl3teTransform(brush.Size.Width/2-offset1一offset2,brush.Size.Height/2+offset1-offset2);//将坐标系逆时针旋转45度brush・Graphics-RotateTransfonn(-45);//在画刷上绘制文本brush.Graphics・DrawString(text,font,PdfBrushes・DarkGray,0,0);〃在PDF页面绘制跟页面一样大小的矩形,并使用定义的画刷填充p
6、age・Canvas.DrawRectangle(brush,newRectangleF(newPointF(0,0),page・Canvas・Size));//保存文档pdf.SaveToFi1e("output,pdf");System・Diagnostics・Process・Start(''output・pdf");!完成代码后,调试运行程序,生成文档,如下:Eoutput.pdf・AdobeReaderEditViewWindowHelp电必⑥@EZ1'Fl/2I95.2%卜
7、
8、Z]&ToolsCommen
9、tExtendedTheWorldEconomicForum(WEF)isaSwissnonprofitfoundation,basedinCologny,Geneva,Switzerland・Recognizedin2015bytheSwissauthoritiesasanMotherinternationalbody"underSwitzerland'sHostStateAct2007(HSA,SR192.12),itsmissioniscitedasrovingthestateoftheworldbyI,ac
10、ademic,andotherI,regional,andindustrycomagendas・forumisbestk己ndofJagagingbusiness,ponti,WORLDWORLDECONOMICFORUM—>水印效果g心沫^TMTRDVING-THE^STATEoritsannualmeetingatthe()F1fnDavos,amoun