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. Itextsharp version is 5.5
1 /// <summary>2 ///add an oblique watermark3 /// </summary>4 /// <param name= "Pdfstream" >PDF file Stream</param>5 /// <param name= "Watermarkname" >Watermark String</param>6 /// <param name= "width" >Page Width</param>7 /// <param name= "height" >page Height</param>8 PublicMemoryStream Setwatermark (MemoryStream pdfstream,stringWatermarkname,floatWidthfloatheight)9 {Ten Try One { A intFontSize = -;//Set Font size - intspan = +;//Set Vertical Displacement -MemoryStream OutStream =NewMemoryStream (); thePdfreader Pdfreader =NewPdfreader (pdfstream); -Pdfstamper Pdfstamper =NewPdfstamper (Pdfreader, outstream); -PdfStamper.Writer.CloseStream =false; - intTotal = Pdfreader.numberofpages +1; + pdfcontentbyte content; -Basefont font = Basefont.createfont (@"C:\WINDOWS\Fonts\STCAIYUN. TTF", Basefont.identity_h, basefont.embedded);//Chinese Cloud Font +Pdfgstate GS =Newpdfgstate (); AGs. Fillopacity =0.15f;//Transparency at intWatermarknamelenth =watermarkname.length; - CharC; - intRise =0; - stringSpanstring =" ";//Horizontal Displacement - for(inti =1; I < total; i++) - { inRise = Watermarknamelenth *span; -Content = Pdfstamper.getovercontent (i);//Add a watermark above the content to //content = pdfstamper.getundercontent (i);//Add a watermark underneath the content + content. Setgstate (GS); - content. Begintext (); the content. Setcolorfill (basecolor.green); * content. Setfontandsize (font, fontSize); $ intHeightnumbert = (int) Math.ceiling (decimal) Height/(decimal) rise);//the number of vertical repetitions, into the pitchPanax Notoginseng intPanlewith = (fontSize + span) *Watermarknamelenth; - intWidthnumber = (int) Math.ceiling (decimal) Width/(decimal) panlewith);//number of horizontal repetitions the + //set watermark text font tilt start A for(intW =0; W < Widthnumber; w++) the { + for(inth =1; H <= Heightnumbert; h++) - { $ intYleng = 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 start of the X, Y axis - for(intK =0; K < Watermarknamelenth; k++) - { theContent. Settextrise (Yleng);//added at the specified y-axis value -c =Watermarkname[k];WuyiContent. Showtext (c +spanstring); theYleng-=span; - } Wu } - } About content. Endtext (); $ } - if(Pdfstamper! =NULL) - pdfstamper.close (); - A if(Pdfreader! =NULL) + pdfreader.close (); the - returnOutStream; $ } the Catch(Exception ex) the { the Throwex; the } -}
C # Add a text watermark