This article is mainly for you to introduce in detail the C # use Itextsharp to add a PDF watermark related data, with a certain reference value, interested in small partners can refer to
Using the Itextsharp to add a PDF watermark, because it is the interface dynamically generated PDF, so the use is all the form of memory flow, and the watermark is tiled is. The Itextsharp version is 5.5.
<summary>///Add tilt watermark////</summary>//<param name= "pdfstream" >pdf file stream </param>// <param name= "Watermarkname" > Watermark string </param>//<param name= "width" > Page width </param>///<para M name= "height" > page height </param> public memorystream setwatermark (MemoryStream pdfstream, string watermarkname, F Loat width, float height) {try {int fontSize = 50;//set font size int span = 40;//Set vertical offset Memo Rystream OutStream = new MemoryStream (); Pdfreader Pdfreader = new Pdfreader (pdfstream); Pdfstamper pdfstamper = new Pdfstamper (Pdfreader, OutStream); PdfStamper.Writer.CloseStream = false; int total = pdfreader.numberofpages + 1; Pdfcontentbyte content; Basefont font = Basefont.createfont (@ "C:\WINDOWS\Fonts\STCAIYUN. TTF ", Basefont.identity_h, basefont.embedded);//Chinese cloud font pdfgstate GS = new Pdfgstate (); Gs. fillopacity = 0.15f;//Transparency int WatermarknaMelenth = Watermarkname.length; char c; int rise = 0; String spanstring = "";//horizontal displacement for (int i = 1; I < total; i++) {rise = Watermarknamelenth * SPAN; Content = Pdfstamper.getovercontent (i);//Add watermark above contents//content = Pdfstamper.getundercontent (i); /Add watermark content underneath the contents. Setgstate (GS); Content. Begintext (); Content. Setcolorfill (Basecolor.green); Content. Setfontandsize (font, fontSize); int heightnumbert = (int) math.ceiling ((decimal) height/(decimal) rise);//number of vertical repetitions, input int panlewith = (fontSize + SPAN) * WATERMARKNAMELENTH; int widthnumber = (int) math.ceiling ((decimal) width/(decimal) Panlewith)///horizontal repeat//set watermark text font tilt start for (int w = 0, W < widthnumber; w++) {for (int h = 1; h <= heightnumbert; h++) {int Yleng = rise * H; Content. Settextmatrix (w * panlewith, Yleng);//x,y sets the absolute left of the watermark to start with the lower-left corner as the starting point for the X, Y axis for (int k = 0; k < watermarknamelenth; k++) { Content. Settextrise (Yleng);//Add c = watermarkname[k at the specified y-axis value; Content. Showtext (c + spanstring); Yleng-= span; }}} content. Endtext (); } if (Pdfstamper! = null) pdfstamper.close (); if (Pdfreader! = null) pdfreader.close (); return outstream; } catch (Exception ex) {throw ex; } }