Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Adding watermark to your PDF using Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.57k
    Comment on it

    Hi there,

    In this blog we will see how to add watermark images to our PDF files using Java.  Watermarks have been used to discourage counterfeiting. To generate watermarked PDFs we need an additional itext.jar file to be included in our class path which can be downloaded from here.

     

    Java code for add watermark to our PDF

    import java.io.FileOutputStream;
    
    import com.lowagie.text.Image;
    import com.lowagie.text.pdf.PdfContentByte;
    import com.lowagie.text.pdf.PdfReader;
    import com.lowagie.text.pdf.PdfStamper;
    
    public class AddWatermarkImageToPDFFile {
    	public static void main(String[] args) {
    		try {
    			PdfReader reader = new PdfReader("input.pdf");
    			int n = reader.getNumberOfPages();
    			PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(
    					"PDFWithWatermarkImage.pdf"));
    			int i = 0;
    			PdfContentByte under;
    			Image img = Image.getInstance("images.png");
    			img.setAbsolutePosition(50, 300);
    			while (i < n) {
    				i++;
    				under = stamp.getUnderContent(i);
    				under.addImage(img);
    			}
    			stamp.close();
    		} catch (Exception de) {
    			de.printStackTrace();
    		}
    	}
    }

     

    Our PDF before adding watermark

     

    PDF after adding watermark

     

    Thanks :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: