Advertise with us

Our eBooks

JBoss JMX Collection: how to create a PDF from text

Starting from now we will collect all interesting MBeans and POJO Services and make available to readers. In this tutorial we will show a POJO Service that transforms a text file into a PDF table using the iText library


The prerequisite of this sample is the iText library which needs to be downloaded from:

iText is an ideal library for developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.



  • Download the iText1.2.X.jar library and add to the application server library path.

To create a service POJO all you need is adding a few annotations to your POJO Class, which will act as a singleton service.

Here's the service POJO Class:
package com.sample;

import org.jboss.ejb3.annotation.Management;
import org.jboss.ejb3.annotation.Service;

@Service(objectName = "textToPdf:service=PDFConverter")
public class PDFConverterService implements PDFConverter{
        // Lifecycle methods
        public void create() throws Exception {} 

        public void createReportPDF(String inputfileName,String outputfileName) {
             try {
            } catch (Exception e) {
What is really interesting about the POJO service are the two annotations:
@Service(objectName = "textToPdf:service=PDFConverter")

With the first one (@Service) you are binding the POJO in the textToPdf domain using the PDFConverter service name

The Second annotation (@Management) contains the interface name which will be exposed to the client. The interface is nothing more than a plain Java interface:
package com.sample;

public interface PDFConverter {
    public void createReportPDF(String inputfileName,String outputfileName);    

The real class which does the job of converting a text file into a PDF formatted table is PDFCreator which basically reads the input file name stores the content into an Array using the "|" as separator between fields. 

package com.sample;

import java.util.ArrayList;
import java.util.StringTokenizer;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class PDFCreator {

    public static void createReportPDF(String inputfileName, String outputfileName) throws DocumentException, IOException {
          ArrayList mainList = new ArrayList();
          // READ TEXT FILE
          BufferedReader in = new BufferedReader(new FileReader(inputfileName));
          String line;
          int rows=0;
          int columns=0;
          while((line = in.readLine()) != null) {  
              ArrayList subList = new ArrayList();
              StringTokenizer st = new StringTokenizer(line,"|");   
              while (st.hasMoreElements()) {
                 String cell = st.nextToken();
        Document document = new Document();
            new FileOutputStream(outputfileName));;
        PdfPTable table = new PdfPTable(columns); 
        for (int i=0;i<mainList.size();i++) {
            ArrayList subList = (ArrayList)mainList.get(i);
            for (int y=0;y<subList.size();y++) {
                String cell = (String)subList.get(y);

Deploying the Service POJO is just a matter of packaging the files into a JAR archive and copying the archive into the deploy folder of JBoss AS. The following output will be produced on the JBoss AS Console:


17:04:36,364 INFO  [JBossASKernel] installing bean: jboss.j2ee:jar=JBossServiceP
17:04:36,364 INFO  [JBossASKernel]   with dependencies:
17:04:36,380 INFO  [JBossASKernel]   and supplies:
17:04:36,380 INFO  [JBossASKernel]      Class:com.sample.PDFConverter
17:04:36,380 INFO  [JBossASKernel]      jndi:PDFConverterService/remote
17:04:36,380 INFO  [JBossASKernel] Added bean(jboss.j2ee:jar=JBossServicePDF.jar
,name=PDFConverterService,service=EJB3) to KernelDeployment of: JBossServicePDF.jar
17:04:36,427 INFO  [EJBContainer] STARTED EJB: com.sample.PDFConverterService ejbName: PDFConverterService

For testing the POJO Service you can navigate to the JMX Console and add the input and output file name:

This is a sample of input file:


This is the PDF output:
jboss pdf java
Service POJO Tutorial
© 2021 All Rights Reserved.

Please publish modules in offcanvas position.