PDF Add watermark

Source: Internet
Author: User

/// <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

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.