Click here to Skip to main content
15,114,888 members
Articles / Programming Languages / Java
Technical Blog
Posted 8 Jun 2011

Tagged as


3 bookmarked

PDF Generation Using Templates and OpenOffice and Itext in Java

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
8 Jun 2011Apache
PDF Generation Using Templates and OpenOffice and Itext in Java

Recently, I had a task wherein I had to create PDF dynamically by merging data and template. I have been using lots of PDF libraries since I started programming to do such tasks. The previous one I admired a lot was Jasper Reports. But after researching for some more time, I found out that IText with Openoffice Draw is the simplest way we can generate PDF forms.

This visual tutorial first explains how to create PDF forms in Openoffice Draw:

Save PDF to folder where you have your program or just change path for input source in program.

Now here is java code which can convert this PDF to final PDF by filling in Values using IText:

package com.linkwithweb.reports;


import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

 * @author Ashwin Kumar
public class ITextStamperSample {

	 * @param args
	 * @throws IOException
	 * @throws DocumentException
	public static void main(String[] args) throws IOException,
			DocumentException {
		// createSamplePDF();

	private static void generateAshwinFriends() throws IOException,
			FileNotFoundException, DocumentException {
		PdfReader pdfTemplate = new PdfReader("mytemplate.pdf");
		FileOutputStream fileOutputStream = new FileOutputStream("test.pdf");
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		PdfStamper stamper = new PdfStamper(pdfTemplate, fileOutputStream);

		stamper.getAcroFields().setField("name", "Ashwin Kumar");
		stamper.getAcroFields().setField("id", "1\n2\n3\n");
		stamper.getAcroFields().setField("relation", "self\nwife\nfriend\n");


Here is a sample pom.xml:

<project xmlns= 

A PDF file with data filled in placeholders is created with name test.pdf:


This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


About the Author

Chief Technology Officer Northalley
United States United States
A Technology evangelist with no technical language barriers. A strong believer that Simple Sofware Is Perfect Software. A staunch proponent of software / documentation automation in all domain's. And finally a true diciple of Google Search.

Comments and Discussions

QuestionValue is not coming in PDF Pin
Member 131068764-Apr-17 21:28
MemberMember 131068764-Apr-17 21:28 
QuestionI need your help. Pin
lianjiny17-Jul-11 8:44
Memberlianjiny17-Jul-11 8:44 
AnswerRe: I need your help. Pin
AshwinRayaprolu17-Jul-11 9:17
MemberAshwinRayaprolu17-Jul-11 9:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.