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:
Applied the Water Mark sucessfully
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.