I have been working on adding watermarks to PDF documents for a long time. Make a record as follows:
The itextsharp component will be used first. You can download it from the official website, and I will add it in this article.
Add reference to the Code as follows:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using iTextSharp.text.pdf;using System.IO;using iTextSharp.text;
Creates a PDF file that displays the specified image.
/// <Summary> /// create a PDF file that displays the specified image /// </Summary> /// <Param name = "picpdfpath"> </param> // /<Param name = "picpath"> </param> // <returns> </returns> Public static bool createpdfbypic (string picpdfpath, string picpath) {// create a document DOC = new document (); try {// create a writer and associate the document object with the author writer. getinstance (Doc, new filestream (picpdfpath, filemode. create, fileaccess. readwrite); // open a document DOC. O Pen (); // Add image IMG = image to the document. getinstance (picpath); // IMG. setabsoluteposition (); Doc. add (IMG); Return true;} catch (exception ex) {return false; throw ex;} finally {If (Doc! = NULL) {Doc. Close ();}}}
Add an image watermark to a PDF document
/// <Summary> /// add an image watermark /// </Summary> /// <Param name = "inputfilepath"> </param> /// <Param name = "outputfilepath"> </param> // <Param name = "modelpicname"> </param> /// <Param name = "TOP"> </param>/ // <Param name = "Left"> </param> // <returns> </returns> Public static bool embedded watermark (string inputfilepath, string outputfilepath, string modelpicname, float top, float left) {// throw new notimplementedexc Eption (); pdfreader = NULL; export Stamper = 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; Specify Stamper = new specify Stamper (pdfreader, new filestream (outputfilepath, filemode. create); Revoke contentbyte watermarkcontent; I Textsharp. text. image image = itextsharp. text. image. getinstance (modelpicname); image. grayfill = 20; // transparency, gray fill // image. rotation // rotate // 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); // Add a watermark to each page. You can also set a watermark for (INT I = 1; I <= num Berofpages; I ++) {// watermarkcontent = paistamper. getundercontent (I); // watermarkcontent = watermark Stamper. getovercontent (I); // watermark watermarkcontent on the upper layer of the content. addimage (image);} // strmsg = "success"; return true;} catch (exception ex) {Throw ex;} finally {If (pdfstamper! = NULL) Begin Stamper. Close (); If (pdfreader! = NULL) pdfreader. Close ();}}
Add normal deflection angle text watermark
/// <Summary> /// Add a text watermark with a normal deflection angle /// </Summary> /// <Param name = "inputfilepath"> </param> /// <Param name = "outputfilepath"> </param> // <Param name = "watermarkname"> </param> // <Param name = "permission"> </ param> Public static void setwatermark (string inputfilepath, string outputfilepath, string watermarkname) {pdfreader = NULL; inclustamper = NULL; try {pdfreader = new pdfreader (Indium Utfilepath); export Stamper = new export Stamper (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; required contentbyte content; basefont font = basefont. createfont (@ "C: \ WINDOWS \ fonts \ simfang. TTF ", basefont. identity_h, basefont. embedded); pdfgsta Te GS = new pdfgstate (); For (INT I = 1; I <total; I ++) {content = 1_stamper. getovercontent (I); // Add a watermark to the top of the content // content = effecstamper. getundercontent (I); // Add a watermark to the bottom of the content // transparency Gs. fillopacity = 0.3f; content. setgstate (GS); // content. setgrayfill (0.3f); // start to write the text content. begintext (); content. setcolorfill (basecolor. light_gray); content. setfontandsize (font, 100); content. settextmatrix (0, 0); content. showtextali Gned (element. align_center, watermarkname, width/2-50, 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 (except Stamper! = NULL) Begin Stamper. Close (); If (pdfreader! = NULL) pdfreader. Close ();}}
Add skewed watermark
/// <Summary> /// Add a skewed watermark /// </Summary> /// <Param name = "inputfilepath"> </param> /// <Param name = "outputfilepath"> </param> /// <Param name = "watermarkname"> </param> /// <Param name = "userpassword"> </param>/ // <Param name = "ownerpassword"> </param> // <Param name = "permission"> </param> Public static void setwatermark (string inputfilepath, string outputfilepath, string watermarkname, string userpassword, String ownerpassword, int permission) {pdfreader = NULL; inclustamper = NULL; try {pdfreader = new pdfreader (inputfilepath); retry = new inclustamper (pdfreader, new filestream (outputfilepath, filemode. create); // set the password // your Stamper. setencryption (false, userpassword, ownerpassword, permission); int Total = pdfreader. numberofpages + 1; required contentbyte 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 = 1_stamper. getovercontent (I); // Add a watermark to the top of the content // content = effecstamper. getundercontent (I); // Add the watermark content below the content. begintext (); content. setcolorfill (Ba Secolor. dark_gray); content. setfontandsize (font, 50); // set the watermark text font skew to begin if (j> = 15) {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 setting ends content. endtext (); // draw a circle // content. ellipse (250,450,350,550); // content. setlinewidth (1f); // content. stroke () ;}} catch (exception ex) {Throw ex;} finally {If (except Stamper! = NULL) Begin Stamper. Close (); If (pdfreader! = NULL) pdfreader. Close ();}}
Example of calling a method:
string path = "D://my.pdf"; HtmlToPDFUtil.HtmlToPdf(Request.Url.AbsoluteUri, path); //HtmlToPDFUtil.HtmlToPdf(sbUrlList.ToString(), path); PDFSetWaterMark.PDFWatermark(path, "D://my.pdf", Server.MapPath("/HtmlToPdf/Tools/sy.bmp"), 0, 0); PDFSetWaterMark.setWatermark("D://my.pdf", "D://my2.pdf", "TEST"); //PDFSetWaterMark.setWatermark("D://my.pdf", "D://my2.pdf", "TEST", "", "", 1);
Attachment: itextsharp