/// <summary> ///Add a watermark or background picture to a PDF/// </summary> /// <param name= "Strsourcefilepath" >source file path</param> /// <param name= "Strtargetfilepath" >Destination file path</param> /// <param name= "Strwatermarkpicpath" >Watermark Picture Path</param> /// <param name= "Strtargetfiletmppath" >Target Temp file</param> /// <returns>Add Results</returns> Public Static BOOLAddwatermarkforpdf (stringStrsourcefilepath,stringStrtargetfilepath,stringStrwatermarkpicpath, FileInfo targetfiletmp) { BOOLBLNRT =false; Pdffileinfo PDF=NewPdffileinfo (Strsourcefilepath); if(!PDF. Bepdffile) {Throw NewApplicationException ("source file is not a valid PDF file"); } //Start processing intIntpagenums =PDF. Numberofpages; Pdffilestamp Filestamp=NULL; Try { intIntnumber =650; intintcount = (int) math.ceiling (Intpagenums/(decimal. Parse (Intnumber.tostring ())); for(intj =0; J < intcount; J + +) {Filestamp=NewPdffilestamp (Strsourcefilepath, Strtargetfilepath); for(inti =1; I <= intnumber; i++) { if(J * Intnumber + i >intpagenums) { Break; } Stamp Stamp=NewStamp (); if(!String.IsNullOrEmpty (Strwatermarkpicpath)) {Stamp. BindImage (Strwatermarkpicpath); } stamp. IsBackground=true; Stamp. PageNumber= J * Intnumber +i; Stamp. SetImageSize (PageSize.A4.Height, PageSize.A4.Width); Filestamp.addstamp (stamp); } filestamp.close (); Targetfiletmp.delete (); File.Copy (Strtargetfilepath, targetfiletmp.fullname); Strsourcefilepath=Targetfiletmp.fullname; } BLNRT=true; } Catch(Exception ex) {Throwex; } returnBLNRT; }
Specific Code
Note that adding a Crystal report to the PDF will cause an error when the page number is too large, or die. To prevent this, we will add watermarks to the PDF multiple times.
If you add 600 pages for the first time, save closing the current file, and then add the watermark to the watermark file, so this will not be wrong.
PDF Add watermark