Using iText with Codenvy

Instantly create Itext project with a Codenvy Factory:


This tutorial will explain the details of integrating the Itext into Codenvy. The explanation has been done with a very basic Java servlet project that makes use of the Itext api to generate a PDF and write to it.

Create a project

Login to your codenvy workspace and create a WAR project named as ‘ItextSample’. This will have the following structure :

project_structure

Specify dependencies in pom.xml

To make use of the Itext api, please update the pom.xml to include the Itext jar and the servletapi jar into your project as Maven dependencies –

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.codenvy</groupId>
  <artifactId>ItextSample</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ItextSample</name>
  <dependencies>
    <dependency>
      <groupId>com.itextpdf</groupId>
      <artifactId>itextpdf</artifactId>
      <version>5.4.4</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <url>http://repo1.maven.org/maven2</url>
    </repository>
  </repositories>

  <build>
    <finalName>ItextSample</finalName>
  </build>
</project>

Create Java class

Now create a Java servlet class named ‘ItextHelloWorldServlet.java’ that will create the PDF document and then write to it and then output the PDF to the response so that it can be seen on the browser. All this is done using the Itext apis inside the method doGet() in the sample source Java class.

package com.codenvy;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class ItextHelloWorldServlet extends HttpServlet{
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("application/pdf");
        try {
            Document document = new Document();
            PdfWriter.getInstance(document, response.getOutputStream());
            document.open();
            document.add(new Paragraph("Hello World"));
            document.add(new Paragraph(new Date().toString()));
            document.close();
        } catch (DocumentException de) {
            throw new IOException(de.getMessage());
        }
    }

   private static final long serialVersionUID = 4262544639420765610L;

}

The Document class is a generic class to which the PDF information will be written.
The PDFWriter is used to write data to the document and the data is written using the Paragraph class.

Create index.jsp

• Your codenvy project will have an index.jsp which will invoke the Servlet class -

<%
    response.sendRedirect("/hello");
%>

Create web.xml file

The servlet mapping ‘/hello’ is defined in the web.xml as follows –

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Itext Servlet App</display-name>
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.codenvy.ItextHelloWorldServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

Build and run the app

Now you build and run your Codenvy project and a PDF will be generated in the browser with ‘Hello World’ and the current date and time written to it –

itext

The Itext provides more apis that can be used in a similar way as above to perform various PDF functions. More details would be available on their website.