Skip to content
Related Articles

Related Articles

Adding Water Marks to the Images in a PDF using Java
  • Last Updated : 08 Dec, 2020

In this article, we will learn how to add watermarks to the images in a PDF document using Java. Adding watermarks to the images in a PDF, we will use the iText library. These are the steps that should be followed to add watermarks to the images in a PDF using java.

1. Creating a PdfWriter object

The PdfWriter class represents the DocWriter for a PDF. The constructor of this class accepts a string, i.e. the path of the file where the PDF is to be created.

// importing the PdfWriter class. 
import com.itextpdf.kernel.pdf.PdfWriter; 

// path where the pdf is to be created. 
String path = "F:/JavaPdf/waterMarkImage.pdf"; 
PdfWriter pdfwriter = new PdfWriter(path);

2. Creating a PdfDocument object

The PdfDocument class is the class that represents the PDF Document in iText, To instantiate this class in write mode, you need to pass an object of the class PdfWriter (i.e. pdfwriter from above code ) to its constructor.



// Creating a PdfDocument object. 
// passing PdfWriter object constructor of pdfDocument. 
PdfDocument pdfdocument = new PdfDocument(pdfwriter);

3. Creating the Document object

The Document class is the root element when creating a self-sufficient PDF. One of the constructors of this class accepts an object of the class PdfDocument (i.e. pdfdocument).

// Creating a Document and passing pdfDocument object 
Document document = new Document(pdfdocument);

4. Create an Image object

We need the image object to manage the images. In order to create an image object, we need to create an ImageData object. We can create it by passing the string parameter that represents the path of the image to create() method of the ImageDataFactory class. Now we can create an image object by passing the ImageData object, as a parameter to the constructor of Image class.

// Create an ImageData object 
String imageFile = "F:/JavaPdf/image.png"; 
ImageData data = ImageDataFactory.create(imageFile); 

// Creating an Image object 
Image image = new Image(data);

5. Creating a template

A Template is an object of PdfFormXObject, later to this template we add an image and watermark.  The constructor of PdfFormXObject take a rectangle as a parameter, After that, we pass the instance of Template and pdfDocument to the Canvas object. add the image to the canvas object.

Now we add the image to canvas object and set the watermark.

//Creating template             
PdfFormXObject template = new PdfFormXObject(new Rectangle(
image.getImageScaledWidth(), image.getImageScaledHeight()));
Canvas canvas = new Canvas(template, pdfdocument).add(image); 
String watermark = "GeeksForGeeks"; 
canvas.setFontColor(DeviceGray.GRAY).showTextAligned(watermark, 120, 300, TextAlignment.CENTER);

6. Adding template to document and close the document



Now we add the templates to the document using add() method and close the document using close().

//adding template to document
Image imagew = new Image(template);          
document.add(imagew); 

//closing the document 
document.close();  
System.out.println("Applied the Water Mark sucessfully");

Dependencies for executing the below program:

kernel-7.1.13.jar
layout-7.1.13.jar
io-7.1.13.jar

Code Snippet:

Java




// Java program to Add Water Marks to the Images in a PDF
  
import com.itextpdf.kernel.colors.DeviceGray;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
  
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.property.TextAlignment;
  
public class AddingToImagesInPDF {
  
    public static void main(String args[]) throws Exception
    {
        String path = "F:/JavaPdf/waterMarkImage.pdf";
        PdfWriter pdfwriter = new PdfWriter(path);
  
        // Creating a PdfDocument object.
        // passing PdfWriter object constructor of
        // pdfDocument.
        PdfDocument pdfdocument
            = new PdfDocument(pdfwriter);
  
        // Creating a Document and passing pdfDocument
        // object
        Document document = new Document(pdfdocument);
  
        // Create an ImageData object
        String imageFile = "F:/JavaPdf/image.png";
        ImageData data = ImageDataFactory.create(imageFile);
  
        // Creating an Image object
        Image image = new Image(data);
        image.scaleToFit(400, 700);
  
        // Creating template
        PdfFormXObject template = new PdfFormXObject(
            new Rectangle(image.getImageScaledWidth(),
                          image.getImageScaledHeight()));
        Canvas canvas
            = new Canvas(template, pdfdocument).add(image);
        String watermark = "GeeksForGeeks";
        canvas.setFontColor(DeviceGray.GRAY)
            .showTextAligned(watermark, 120, 300,
                             TextAlignment.CENTER);
  
        // adding template to document
        Image imagew = new Image(template);
        document.add(imagew);
  
        // closing the document
        document.close();
        System.out.println(
            "Applied the Water Mark sucessfully");
    }
}

Output

Applied the Water Mark sucessfully

PDF

Adding Water Marks to the Images in a PDF using Java

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :