Examples of implementation of PDF-generated image text watermarking in C #

Source: Internet
Author: User
This article mainly introduces the C # implementation of the PDF generated image text Watermark class, combined with a complete example of C # for the creation of PDF files, add text, watermark and other related operations skills, the need for friends can refer to the next

The example of this paper describes the C # implementation of the PDF generated image text Watermark class. Share to everyone for your reference, as follows:


public class pdfsetwatermark{//<summary>///Create a PDF showing the specified picture///</summary>//<param name= "    Picpdfpath "></param>//<param name=" Picpath "></param>//<returns></returns> public static bool Createpdfbypic (string Picpdfpath, String picpath) {//Create a new Document DOC = new documents ()      ; try {//Create a writer (writer) associated with the Document Object Pdfwriter.getinstance (Doc, New FileStream (Picpdfpath, Filemode.cre        Ate, FileAccess.ReadWrite)); Open a document Doc.        Open ();        Add content to the document Image img = image.getinstance (picpath); Img.        SetAbsolutePosition (); Doc.        Add (IMG);      return true;        } catch (Exception ex) {return false;      Throw ex; } finally {if (doc! = null) {Doc.        Close (); }}}///<summary>//Plus picture watermark///</summary>//<param name= "Inputfilepath" ></pa   Ram> <param name= "Outputfilepath" ></param>//<param name= "Modelpicname" ></param>//<p    Aram Name= "Top" ></param>//<param name= "left" ></param>//<returns></returns>    public static bool Pdfwatermark (string Inputfilepath, String Outputfilepath, String modelpicname, float top, float left)      {//throw new notimplementedexception ();      Pdfreader pdfreader = null;      Pdfstamper pdfstamper = null;        try {pdfreader = new Pdfreader (Inputfilepath);        int numberofpages = pdfreader.numberofpages;        ITextSharp.text.Rectangle psize = pdfreader.getpagesize (1); Float width = psize.        Width; float height = psize.        Height;        Pdfstamper = new Pdfstamper (Pdfreader, New FileStream (Outputfilepath, FileMode.Create));        Pdfcontentbyte watermarkcontent;        ITextSharp.text.Image Image = iTextSharp.text.Image.GetInstance (modelpicname); Image.    Grayfill = 20;//transparency, gray fill    Image. rotation//rotating//image. rotationdegrees//rotation angle//watermark position if (left < 0) {left = WIDTH/2-image.        Width + left; }//image. SetAbsolutePosition (left, (height-image).        Height)-top); Image. SetAbsolutePosition (left, (HEIGHT/2-image).        Height)-top); Each page watermark, you can also set a page watermark for (int i = 1; I <= numberofpages; i++) {//watermarkcontent = Pdfstamper. Getundercontent (i);//content watermark Watermarkcontent = pdfstamper.getovercontent (i);//content Upper watermark watermarkcontent.        AddImage (image);        }//strmsg = "Success";      return true;      } catch (Exception ex) {throw ex;        } finally {if (pdfstamper! = null) pdfstamper.close ();      if (Pdfreader! = null) pdfreader.close (); }}///<summary>///Add normal deflection angle text watermark///</summary>//<param name= "Inputfilepath" ></pa ram>//<param name="Outputfilepath" ></param>//<param name= "Watermarkname" ></param>//<param name= "Permissi On "></param> public static void Setwatermark (String inputfilepath, String Outputfilepath, String watermarkname      ) {Pdfreader pdfreader = null;      Pdfstamper pdfstamper = null;        try {pdfreader = new Pdfreader (Inputfilepath);        Pdfstamper = new Pdfstamper (Pdfreader, New FileStream (Outputfilepath, FileMode.Create));        int total = pdfreader.numberofpages + 1;        ITextSharp.text.Rectangle psize = pdfreader.getpagesize (1); Float width = psize.        Width; float height = psize.        Height;        Pdfcontentbyte content; Basefont font = Basefont.createfont (@ "C:\WINDOWS\Fonts\SIMFANG.        TTF ", Basefont.identity_h, basefont.embedded);        Pdfgstate GS = new Pdfgstate (); for (int i = 1, i < total; i++) {content = Pdfstamper.getovercontent (i);//Add watermark above content//content = Pdfstamper.getundErcontent (i);//Add watermark below content//transparency GS.          fillopacity = 0.3f; Content.          Setgstate (GS); Content.          Setgrayfill (0.3f); Begins writing to the text content.          Begintext (); Content.          Setcolorfill (Basecolor.light_gray); Content.          Setfontandsize (font, 100); Content.          Settextmatrix (0, 0); Content.          Showtextaligned (Element.align_center, Watermarkname, WIDTH/2-HEIGHT/2-50, 55); Content.          Setcolorfill (Basecolor.black); Content.          Setfontandsize (font, 8); Content.          Showtextaligned (Element.align_center, watermarkname, 0, 0, 0); Content.        Endtext ();      }} catch (Exception ex) {throw ex;        } finally {if (pdfstamper! = null) pdfstamper.close ();      if (Pdfreader! = null) pdfreader.close (); }}///<summary>///Add tilt watermark///</summary>//<param name= "Inputfilepath" ></param&gt    ; <param name= "OUtputfilepath "></param>//<param name=" Watermarkname "></param>//<param name=" Userpasswo RD "></param>//<param name=" OwnerPassword "></param>//<param name=" permission "></p aram> public static void Setwatermark (String inputfilepath, String Outputfilepath, String watermarkname, String user      PassWord, string ownerpassword, int permission) {Pdfreader pdfreader = null;      Pdfstamper pdfstamper = null;        try {pdfreader = new Pdfreader (Inputfilepath);        Pdfstamper = new Pdfstamper (Pdfreader, New FileStream (Outputfilepath, FileMode.Create));        Set Password//pdfstamper.setencryption (False,userpassword, OwnerPassword, permission);        int total = pdfreader.numberofpages + 1;        Pdfcontentbyte content; Basefont font = Basefont.createfont (@ "C:\WINDOWS\Fonts\SIMFANG.        TTF ", Basefont.identity_h, basefont.embedded);        Pdfgstate GS = new Pdfgstate (); Gs. Fillopacity = 0.2f;//transparency int j = Watermarkname.length;        char c;        int rise = 0;          for (int i = 1; i < total; i++) {rise = 500; Content = Pdfstamper.getovercontent (i);//watermark above contents//content = Pdfstamper.getundercontent (i);//Add watermark under content C Ontent.          Begintext (); Content.          Setcolorfill (Basecolor.dark_gray); Content.          Setfontandsize (font, 50); Set Watermark text font tilt start if (j >=) {content.            Settextmatrix (200, 120); for (int k = 0; k < J; k++) {content.              Settextrise (Rise);              c = Watermarkname[k]; Content.              Showtext (c + "");            Rise-= 20; }} else {content.            Settextmatrix (180, 100); for (int k = 0; k < J; k++) {content.              Settextrise (Rise);              c = Watermarkname[k]; Content.              Showtext (c + "");        Rise-= 18;    }}//font settings end content.          Endtext (); Draw a round//content.          Ellipse (250, 450, 350, 550); Content.          Setlinewidth (1f); Content.        Stroke ();      }} catch (Exception ex) {throw ex;        } finally {if (pdfstamper! = null) pdfstamper.close ();      if (Pdfreader! = null) pdfreader.close (); }    }}
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.