Java iText PDF Add WaterMark Example

In this tutorial, we will explain how to add a Watermark to a PDF document using iText, with example Java code. Watermarking is often done to document to maintain the document integrity and iText offers some way to perform watermarking in PDF files. To run the example given in this post, you will need iText version 5.0.6. On the same lines as per our previous post, we will document each and every segment of the watermarking code and then provide the complete code in the end.

1) To perform Watermarking on PDF files, we need two basic things (1) the PDF file (2) Watermark image. we will be using the following image file for Watermarking.
Image for Watermarking in PDF documents

We will also use the PDF file created out of our beginners tutorial on iText for this purpose.

2) To watermark an existing PDF, we need to retrieve existing information about the PDF document such as getting the number of pages in the document for Watermarking. We will make use of the "PdfReader" class to read the existing PDF document. Also, to identify the number of pages in the PDF to be watermarked, we will use the method "getNumberOfPages" on the "PdfReader" class. The code example is provided below
PdfReader Read_PDF_To_Watermark = new PdfReader("Sample.pdf");
int number_of_pages = Read_PDF_To_Watermark.getNumberOfPages();
3) Till this point, we have obtained only a read only version of the PDF document to be watermarked. We will use PdfStamper class to create a watermarked PDF. PDF stamper always creates a new PDF file and helps to modify contents in PDF file.
PdfStamper stamp = new PdfStamper(Read_PDF_To_Watermark, new FileOutputStream("New_PDF_With_Watermark_Image.pdf"));

4) We will now create a PdfContentByte object. If you think a PDF as a superimposed layers of pages, each layer has a content which is wrapped under a PdfContentByte object. We need this object to add the image as a watermark to the PDF document.

Each PDF page has two extra layers; one that sits on top of all text / graphics and one that goes to the bottom. All user added content gets in-between these two. If we get into this bottommost content, we can write anything under that we want. To get into this bottommost layer, we can use the " getUnderContent" method of PdfStamper object.  This is documented in iText API Reference as shown below:
public PdfContentByte getUnderContent(int pageNum)
    Gets a PdfContentByte to write under the page of the original document.
    Parameters:
        pageNum - the page number where the extra content is written
    Returns:
        a PdfContentByte to write under the page of the original document
The code example is provided below;
PdfContentByte add_watermark;            
while (i < number_of_pages) {
              i++;
              add_watermark = stamp.getUnderContent(i);
              add_watermark.addImage(watermark_image);
            }
The "addImage" method adds the watermark to the image. In our case, it is the image provided in step (1).

5) AddImage method as shown in step 4, accepts an image with absolute positioning. The Image should be of type com.itextpdf.text.Image.  We can get our watermark.jpg image and set the positioning we want on the page for this image using "setAbsolutePosition" method.  As an example;
Image watermark_image = Image.getInstance("watermark.jpg");
watermark_image.setAbsolutePosition(200, 400);
6) We can close the PdfStamper object. This will write the EOF information and close the PDF document.
stamp.close();
That summarizes the procedure to add a watermark image to PDF document.  The complete Java code is provided below. If you run this example, you will get a watermarked PDF document with our image "watermark.jpg" serving as a watermark!
import java.io.FileOutputStream;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class watermark {
    public static void main(String[] args) {
        try {
            PdfReader Read_PDF_To_Watermark = new PdfReader("Sample.pdf");
            int number_of_pages = Read_PDF_To_Watermark.getNumberOfPages();
            PdfStamper stamp = new PdfStamper(Read_PDF_To_Watermark, new FileOutputStream("New_PDF_With_Watermark_Image.pdf"));
            int i = 0;
            Image watermark_image = Image.getInstance("watermark.jpg");
            watermark_image.setAbsolutePosition(200, 400);
            PdfContentByte add_watermark;            
            while (i < number_of_pages) {
              i++;
              add_watermark = stamp.getUnderContent(i);
              add_watermark.addImage(watermark_image);
            }
            stamp.close();
        }
        catch (Exception i1) {
            i1.printStackTrace();
        }
    }
}

1 comment:


  1. Thanks for your post, I am havving a Issue with it.

    It seems to work "as is" I just changed the "image" and pdf documents for the full route (c://tmp//*)

    The program runs with no errors, it creates the copy of the document and it is a bit larger than the original (mesured in KB) but there is no watermark at all in the document nor printed or in the reader.

    I tried to change the image but the result was the same

    my code:


    PdfReader Read_PDF_To_Watermark = new PdfReader("C:\\Users\\me\\Desktop\\wm\\origin.pdf");
    int number_of_pages = Read_PDF_To_Watermark.getNumberOfPages();
    PdfStamper stamp = new PdfStamper(Read_PDF_To_Watermark, new FileOutputStream("C:\\Users\\me\\Desktop\\wm\\New.pdf"));
    int i = 0;
    Image watermark_image = Image.getInstance("C:\\Users\\me\\Desktop\\wm\\watermark.jpg");
    watermark_image.setAbsolutePosition(200, 400);
    PdfContentByte add_watermark;
    while (i < number_of_pages) {
    i++;
    add_watermark = stamp.getUnderContent(i);
    add_watermark.addImage(watermark_image);
    }
    stamp.close();
    }
    catch (IOException | DocumentException i1) {
    }

    ReplyDelete