Core Java

iText Watermark Example

In the previous example we learnt how we can convert a HTML document to a PDF Document using the ITEXT library. In this example we will demonstrate how we can add watermark to a PDF Document using Itext.

Watermark are usually added to a document to prevent counterfeiting or to mark the name of the maker or the organization, for advertising the name of the organization in the document. Whatever, be the reason, let’s find out how we can achieve that in a PDF Document.
 
 
 
 

1. Project Set-Up

We shall use Maven to setup our project. Open eclipse and create a simple Maven project and check the skip archetype selection checkbox on the dialogue box that appears. Replace the content of the existing pom.xml

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>ITextExample</groupId>
	<artifactId>ITextExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.5.6</version>
		</dependency>
		<dependency>
			<groupId>org.bouncycastle</groupId>
			<artifactId>bcprov-jdk15on</artifactId>
			<version>1.52</version>
		</dependency>


	</dependencies>

</project>

That’s all from setting-up project point of view, let’s start with the actual code implementation now:

2. Implementation

com.itextpdf.text.pdf.PdfPageEventHelper class is used to listen for the page end event of the document via the onEndPage method. Whenever the page gets filled up with the contents it can accomodate, the onEndPage method is invoked and the watermark is added to the page of the document. This helps in all the pages of the document having the watermark.

We create a simple Water-Mark for JavaCodeGeeks with the letters JCG. The font color is Grey so it does not obscure the actual content of the document and is visible only as a background.

PDFEventListener.java

package com.jcg.examples;


import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;


public class PDFEventListener extends PdfPageEventHelper
{

		@Override
		public void onEndPage(PdfWriter writer, Document document)
		{
				PdfContentByte canvas = writer.getDirectContentUnder();
				Phrase watermark = new Phrase("JCG", new Font(FontFamily.TIMES_ROMAN, 190, Font.NORMAL, BaseColor.LIGHT_GRAY));
				ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, watermark, 337, 500, 45);
		}
}

Now we will use an instance of this PDFEventListener class to the com.itextpdf.text.pdf.PdfWriter. To do so we need to register the instance with the Pdfwriter instance by pdfWriter.setPageEventmethod.

CreateWatermarkedPDF.java

package com.jcg.examples;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.PdfWriter;


public class CreateWatermarkedPDF
{
	public static void main(String[] args)
	{
		try
		{
			Document document = new Document();
			PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("WaterMarkedDocument.pdf"));
			document.open();
			pdfWriter.setPageEvent(new PDFEventListener());
			Font font = new Font(FontFamily.TIMES_ROMAN, 20, Font.NORMAL, BaseColor.BLACK);
			document.add(new Phrase("Hi People!! This is an exaple to demostrate Watermark in using Itext",font));
			document.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (DocumentException e)
		{
			e.printStackTrace();
		}
	}
}

Upon execution of CreateWatermarkedPDF class a PDF document is created with the Water-Marked letters JCG in the background.

Here’s how the Water-Marked document looks like:

Fig 1 : WaterMarked PDF Document
Fig 1 : WaterMarked PDF Document

3. Download the Source Code

Here, we demonstrated how we can add water-mark while creating new document or to an existing document using the Itext library.

Download
You can download the source code of this example here: ItextWatermarkExample.zip

Chandan Singh

Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button