Detailed code case for C # using Itextsharp to add PDF watermark

Source: Internet
Author: User
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; }    }
Related Article

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.