C # add watermark to PDF file by code method sharing

Source: Internet
Author: User
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 (); }
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.