how to create pdf document from jTable witch contains multiple rows and pages using java?

75 views Asked by At

I want to export jTable (tblinvoice) (16 columns) data to a A4 sized pdf document using iText 5 and Java. the pdf might have multiple pages and each page should have table header and page number.

I have one jTable with 16 columns. Im trying with itext 5 and I was able to get result within one page of a pdf document (A4 size). if the jTable has multiple rows (for 2,3 pages) then I get a nullPointerException message.

`Document dc = new Document(PageSize.A4.rotate()); PdfWriter.getInstance(dc, new FileOutputStream(path + allin)); dc.open();

        PdfPTable table = new PdfPTable(17);
        table.setHeaderRows(1);
        table.setSplitLate(false);
        table.setSplitRows(false);
        table.setComplete(false);

        for (int q = 0; q < 1; q++) { //header setter
            table.addCell("No");
            table.addCell("Invoice No");
            table.addCell("Customer Name");
            table.addCell("Sales Date");
            table.addCell("Sales Time");
            table.addCell("Total Item");
            table.addCell("Total Price");
            table.addCell("Discount");
            table.addCell("Total Payable");
            table.addCell("Paid Amount");
            table.addCell("Balance");
            table.addCell("Payment Type");
            table.addCell("Hold Id");
            table.addCell("Return Note");
            table.addCell("Hold Note");
            table.addCell("Invoice Status");
            table.addCell("User ID / Name");

        }

        for (int i = 1; i < tblinvoice.getRowCount(); i++) { //cell 

            String id = tblinvoice.getValueAt(i, 0).toString();
            String cn = tblinvoice.getValueAt(i, 1).toString();
            String sd = tblinvoice.getValueAt(i, 2).toString();
            String st = tblinvoice.getValueAt(i, 3).toString();
            String ti = tblinvoice.getValueAt(i, 4).toString();
            String tp = tblinvoice.getValueAt(i, 5).toString();
            String dt = tblinvoice.getValueAt(i, 6).toString();
            String tl = tblinvoice.getValueAt(i, 7).toString();
            String pa = tblinvoice.getValueAt(i, 8).toString();
            String bn = tblinvoice.getValueAt(i, 9).toString();
            String pt = tblinvoice.getValueAt(i, 10).toString();
            String hi = tblinvoice.getValueAt(i, 11).toString();
            String rn = tblinvoice.getValueAt(i, 12).toString();
            String hn = tblinvoice.getValueAt(i, 13).toString();
            String is = tblinvoice.getValueAt(i, 14).toString();
            String ui = tblinvoice.getValueAt(i, 15).toString();

            table.addCell("" + i);
            table.addCell(id);
            table.addCell(cn);
            table.addCell(sd);
            table.addCell(st);
            table.addCell(ti);
            table.addCell(tp);
            table.addCell(dt);
            table.addCell(tl);
            table.addCell(pa);
            table.addCell(bn);
            table.addCell(pt);
            table.addCell(hi);
            table.addCell(rn);
            table.addCell(hn);
            table.addCell(is);
            table.addCell(ui);

        }
        dc.newPage();
        table.setComplete(true);
        table.setSplitLate(false);
        dc.add(table);
        dc.close();`

I expect to hava multiple rows of pdf document(A4 sized). but I get I get a nullPointerException message. and pdf saves but could not open!

0

There are 0 answers