Core Java

iText PDFstamper Example

In the previous examples we have seen how the PdfReader and Pdfwriter classes in the IText library work. In this example, we will demonstrate the working of another important class, PDFStamper.

PDFStamper class is be used to modify existing PDF document by adding extra content to the pages. The extra content are the objects supported by the PdfContentByte. We will see how the objects can be added using the PDFStamper.
 
 
 
 
 

1. Setup the Project

Let’s setup the project by creating a simple Maven project and selecting the skip archetype selection. Update the contents of pom.xml with the contents of the file below:

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> 

This will import the Maven dependencies for the project. Now we are all set for the example.

Here’s a simple HelloWorld PDF document :

Fig 1 : Original Document
Fig 1 : Original Document

We will modify this PDF to include the phrase “Hello JCGians!!”. Let’s write a program for this:

PDFStamperExample.java

package com.jcg.examples;


import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;


class PDFStamperExample
{
		public static void main(String[] args)
		{
				try
				{
						PdfReader pdfReader = new PdfReader("HelloWorld.pdf");
						PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("HelloWorldModified.pdf"));
						PdfContentByte canvas = pdfStamper.getOverContent(1);
						ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Hello people!"), 250, 750, 0);
						pdfStamper.close();
						pdfReader.close();
				}
				catch (IOException e)
				{
						e.printStackTrace();
				}
				catch (DocumentException e)
				{
					e.printStackTrace();
				}
		}
}

We create an object of com.itextpdf.text.pdf.PdfReader and pass the path of the PDF we wish to modify. A FileOutputStream object is also created, which has the path to the new modified file to be created by the progrmamme. Next, we create an instance of com.itextpdf.text.pdf.PdfStamper class by passing the pdfReader and a FileOutputStream objects created earlier. Next, we need to access the com.itextpdf.text.pdf.PdfContentByte object to add the PDF objects like Phrase, Paragraph etc. The PdfStamper#getOverContent() method returns the reference to the underlying PdfContentByte objects. Adding the objects to the document via this object adds them over the layer above this document. We will discuss about this in detail later. In case, we need to add to more than one page we will have to iterate over the pages using the PdfReader#getNumberOfPages(). Then we close the PDFStamper and PdfReader. Not closing the objects leads to the generated PDF being corrupted.

Here’s the snapshot of the output PDF Document:

Fig 2 : Modified Document
Fig 2 : Modified Document

One difference that the user needs to understand is about PdfStamper#getOverContent() vs PdfStamper#getUnderContent() methods. The getOverContent() method returns the access to the ContentByte over the existing document and getUnderContent() method returns access to the canvas under the existing document.

Similarly, we can add any type of PDF Objects to the existing document via the PdfStamper class like image, phrase, paragraph etc.

2. Download the Source Code

Here we demonstrated how we can use the PDFStamper class in IText Library to modify the contents of an existing PDF Document.

Download
You can download the source code of this example here: PdfStamperExample.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