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 :


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="" xmlns:xsi=""
      <name>Maven Repository Switchboard</name>


Create Java class

Now create a Java servlet class named ‘’ 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.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 {
        try {
            Document document = new Document();
            PdfWriter.getInstance(document, response.getOutputStream());
            document.add(new Paragraph("Hello World"));
            document.add(new Paragraph(new Date().toString()));
        } 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 -


Create web.xml file

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

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

  <display-name>Itext Servlet App</display-name>


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 –


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.