C # Add a text watermark

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.