Click here to Skip to main content
Click here to Skip to main content

PDF Generation Using Templates and OpenOffice and Itext in Java

By , 8 Jun 2011
 

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 java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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();
		generateAshwinFriends();
	}

	/**
	 *
	 */
	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.setFormFlattening(true);

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

		stamper.close();
		pdfTemplate.close();
	}
}

Here is a sample 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>com.linkwithweb.reports</groupId>
  <artifactId>ReportTemplateTutorial</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>ReportTemplateTutorial</name>
  <description>ReportTemplateTutorial</description>
  <dependencies>
  	<dependency>
  		<groupId>com.lowagie</groupId>
  		<artifactId>itext</artifactId>
  		<version>2.1.7</version>
  	</dependency>
  </dependencies>
</project>

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


License

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

About the Author

AshwinRayaprolu
Chief Technology Officer Northalley
United States United States
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionI need your help.memberlianjiny17 Jul '11 - 7:44 
Your article looks very wonderful.Thumbs Up | :thumbsup:
Hi, I'm coding java.
I gonna create pdf in java.
I want to draw round rectangle containing text.
How can i do it using your good idea?
AnswerRe: I need your help.memberAshwinRayaprolu17 Jul '11 - 8:17 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 8 Jun 2011
Article Copyright 2011 by AshwinRayaprolu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid