How to add IMAGE as Watermark In Pdf in iText in java


In this core java tutorial we will learn How to add image as Watermark In Pdf using iText library - core java tutorial in Java with program and examples.

Follow these steps to add image as Watermark In Pdf using iText library >

register the page event helper in main method
pdfWriter.setPageEvent(new MyPdfPageEventHelper());

Create class MyPdfPageEventHelper which extends PdfPageEventHelper class and override onEndPagemethod to create page event helper, onEndPage method gets called as soon as document is closed i.e. immediately after document.close(); is called.

Then create the waterMarkImage (either get it from local system or URL), then set the waterMarkImage size, alignment etc.
In this example we will get image from URL and then set the waterMarkImage on whole page.


How to Create the Table In Pdf in java - iText java tutorial

Create Greek List In pdf in java using iText - iText java tutorial

How to Create Password Protected Pdf in java - iText Example

How to add TEXT as Watermark In Pdf in java - iText java tutorial


Download jars required to execute program >

Or, you may download jars from here.


Program/Example How to add IMAGE as Watermark In Pdf in java
import java.io.FileOutputStream;
import java.net.URL;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
*  How to add Image as Watermark In Pdf
*  Example Using iText library - core java tutorial
*
*/
public class AddImageAsWatermarkInPdfExample {
   public static void main(String[] args) {
          try {
                
                 String pdfFilePath = "E:/Add Image As Watermark In Pdf.pdf";
                 Document document = new Document();
                 PdfWriter pdfWriter = PdfWriter
                              .getInstance(document, new FileOutputStream(
                                            pdfFilePath));
                 pdfWriter.setPageEvent(new MyPdfPageEventHelper()); // register the
                                                                                                      // page event helper
                 document.open();
                 System.out.println("Writing Paragraph to PDF");
                
                 for(int i=0; i<40; i++) //For loop-To show text is overlapping the waterMark image
                 document.add(new Paragraph(
                             "This document contains a Watermark Image (Background Image) "));
                 document.close();
                
                
                 System.out.println("PDF created in >> "+ pdfFilePath);
          } catch (Exception e) {
                 e.printStackTrace();
          }
   }
}
/**
* Extend PdfPageEventHelper class and override onEndPagemethod
* to create page event helper.
*
* onEndPage method gets called as soon as document is closed i.e.
* immediately after document.close(); is called
*/
class MyPdfPageEventHelper extends PdfPageEventHelper {
   @Override
   public void onEndPage(PdfWriter pdfWriter, Document document) {
          System.out.println("Creating Waterwark Image in PDF");
         
          try {
                
                 //Use this method if you want to get image from your Local system
                 //Image waterMarkImage = Image.getInstance("E:/tiger.jpg");
                
                 String urlOfWaterMarKImage = "https://lh5.googleusercontent.com/"
                              + "koRHmyNXUGLIjtkHFOJ-1tE5KKl-vZAW3AWLlynkAZSY3Z9m9HBn"
                              + "boAPiakPTUiySX7W1I8xDwD4g49sD2JfgDDNJIPUdYVDWHfVomeh"
                              + "FJHWUk1tDPYlpiB32eZ5TDCKUAAZUIQ"   ;
                
                 //Get waterMarkImage from some URL
                 Image waterMarkImage = Image.getInstance(new URL(urlOfWaterMarKImage));
                
                 //Get width and height of whole page
                 float pdfPageWidth = document.getPageSize().getWidth();
                 float pdfPageHeight = document.getPageSize().getHeight();
                 //Set waterMarkImage on whole page
                 pdfWriter.getDirectContentUnder().addImage(waterMarkImage,
                              pdfPageWidth, 0, 0, pdfPageHeight, 0, 0);
          }catch(Exception e){
                 e.printStackTrace();
          }
   }
}
/* OUTPUT of above program/Example -
Writing Paragraph to PDF
Creating Waterwark Image in PDF
PDF created in >> E:/Add Image As Watermark In Pdf.pdf
*/


PDF formed after executing above java program(How to add IMAGE as Watermark In Pdf in iText in java) will look like this >


Summary -
So in this core java tutorial we learned How to add image as Watermark In Pdf using iText library in core java.
We registered the page event helper in main method

Created class MyPdfPageEventHelper which extends PdfPageEventHelper class and override onEndPagemethod to create page event helper, onEndPage method gots called as soon as document is closed.
Then created the waterMarkImage from URL and then we set the waterMarkImage on whole page.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

References >



RELATED LINKS>

iText library tutorial in java - How to Create and work with PDF files in java


Creating IMAGES in pdf in java - iText tutorial >

Adding the Images In Pdf in java example using iText - iText java tutorial

How to Add And Rotate Images In Pdf in java - iText java tutorial example

How can you Add the Images from URL In Pdf in java program





Adding WATERMARK in pdf in java - iText tutorial >

How to add TEXT as Watermark In Pdf in java - iText java tutorial

How to add IMAGE as Watermark In Pdf in iText in java example



Creating CHAPTER, SECTION, CHUNK, PARAGRAPH AND PHRASE in pdf in java - iText tutorial >

Create Chapter And Section In Pdf in java - iText java tutorial

Create Chunk In Pdf in java - iText java tutorial program

how to Create Paragraph In Pdf - iText java tutorial program

Create Left, center and right aligned Paragraph In Pdf - iText java tutorial

Create Phrase In Pdf (with lead) Using Itext - iText java tutorial example



Set FONT NAME, SIZE, STYLE, COLOUR in pdf in java - iText tutorial >

How to Set the Font Name, Size, Style and Colour In Pdf using itext in java


Create ANCHOR (HYPERLINK), SUPERSCRIPT, SUBSCRIPT, UNDERLINE AND STRIKETHROUGH in pdf in java - iText tutorial >

How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial

How To Set SuperScript And SubScript In Pdf in java - iText java tutorial

UnderLine Text In Pdf in java - iText java tutorial example

Strikethrough Text In Pdf in java - iText java tutorial program


HTML to PDF in java - iText tutorial >

Convert Html To Pdf in java using iText - iText java tutorial


Create new page, set page height and width in java - iText tutorial >

How To Create New Pages In Pdf Using Itext - iText java tutorial example

How to Find Pdf Page Height And Width in java - iText java tutorial program



HEADER and FOOTER in pdf in java - iText tutorial >

How To Set Header and Footer in pdf in java using Itext Example - iText java tutorial



BAR AND PIE CHARTS in pdf in java - iText tutorial >

How to create Bar Chart In Pdf in java - iText java tutorial

Create Pie Chart In Pdf in java using itext - iText java tutorial



MODIFY/ EDIT pdf in java - iText tutorial >

How to Modify - Add Text To Existing PDF in java - iText java tutorial example

How To Read And Add Image To Existing Pdf in java Example - iText java tutorial



Solve common Exceptions in itext in java - iText tutorial >

How to solve the Document Has No Pages IOException in iText in java


Alternates to itext in java - iText tutorial >

What are alternatives to iText library for creating Pdf in java?




No comments:

Post a Comment