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.
package com.sample; import org.jboss.ejb3.annotation.Management; import org.jboss.ejb3.annotation.Service; @Service(objectName = "textToPdf:service=PDFConverter") @Management(PDFConverter.class) public class PDFConverterService implements PDFConverter{ // Lifecycle methods public void create() throws Exception {} @Override public void createReportPDF(String inputfileName,String outputfileName) { try { PDFCreator.createReportPDF(inputfileName,outputfileName); } catch (Exception e) { e.printStackTrace(); } } }
@Service(objectName = “textToPdf:service=PDFConverter”)
@Management(PDFConverter.class)
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.io.*; 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(); subList.add(cell); columns=subList.size(); } mainList.add(subList); } in.close(); rows=mainList.size(); // CREATE PDF DOCUMENT Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(outputfileName)); document.open(); 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); table.addCell(cell); } } document.add(table); document.close(); } }
17:04:36,364 INFO’ [JBossASKernel] installing bean: jboss.j2ee:jar=JBossServiceP DF.jar,name=PDFConverterService,service=EJB3 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 |
aaaaaaa|bbbbbbb|ccccccc
ddddddd|eeeeeee|fffffff
ggggggg|hhhhhhh|iiiiiii
This is the PDF output:
Resources:
Service POJO Tutorial