Search code examples
javapdfpdf-generationitextjdeveloper

ClassNotFoundException when handling PDF files using iText in Jdeveloper


I need your help in solving the following error

java.lang.ClassNotFoundException: com.itextpdf.text.DocumentException

which is produced by running a simple void method to show the PDF file in the Jdeveloper. I added the itext jar file in the Libraries and the version which I am having is 5.5.6.

If I am writing a simple PDF, the method will be executed. However when I use the below code, it generates the error. The code is:

public class pdf {
    private StreamedContent streamedContent;
    private static final long serialVersionUID = 1L;

  @PostConstruct     
    public void  CreatePDF() throws DocumentException, IOException {
      try{
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        OutputStream out = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();

        PdfPTable table = new PdfPTable(1);
        PdfPCell cell = new PdfPCell(new Phrase("\u0627\u0644\u0633\u0644\u064a\u0637\u064a"));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
        table.addCell(cell);
        document.add(table);

        document.close();
        out.close();

        InputStream in =new ByteArrayInputStream(((ByteArrayOutputStream)out).toByteArray());

                    streamedContent = new DefaultStreamedContent(in, "application/pdf");
                //-------
                Map<String, Object> session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
                byte[] b = (byte[]) session.get("reportBytes");
                if (b != null) {
                    streamedContent = new DefaultStreamedContent(new ByteArrayInputStream(b), "application/pdf");
                }         
      }
        catch (Exception e) {
                }
    }

Solution

  • The iText WAR file is kept under the /lib folder and it has worked fine. The compatible version is iText 2.1.7 please refer to the Dependencies in the PrimeFaces Guide