This article mainly for you to introduce in detail the C # to the PDF file to add watermark code, with a certain reference value, interested in small partners can refer to
The example of this article for everyone to share the C # add PDF file watermark specific code for your reference, the specific content is as follows
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;using itextsharp.text.html.simpleparser;using itextsharp.text.pdf;using ITextSharp.text ; using system.io;using system.web.ui.htmlcontrols;using system.drawing;//itextsharp.dll version:5.1.10protected void Button1_Click (object sender, EventArgs e) {string source =@ "D:\my.sample\c#net\exoport2pdf\web2\chap1011.pdf"; Template path String output = @ "D:\my.sample\c#net\exoport2pdf\web2\chap1012.pdf"; Export the watermark background after the PDF string watermark = @ "D:\my.sample\c#net\exoport2pdf\web2\gp0.jpg"; Watermark picture bool issurrcess = Pdfwatermark (source, output, watermark, 10, 10); } public 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-image. Width + left; } image. SetAbsolutePosition (left, (height-image). Height)-top); Watermark per page, you can also set a page watermark for (int i = 1; I <= numberofpages; i++) {watermarkcontent = Pdfstamper.getunde Rcontent (i); Watermarkcontent.addimage (image); }//strmsg = "Success"; return true; } catch (Exception ex) {ex. Message.trim (); return false; } finally {if (Pdfstamper! = nulL) pdfstamper.close (); if (Pdfreader! = null) pdfreader.close (); }