How to create new page based on the condition using itextpdf and java

491 views Asked by At

I am generating pdf using itextpdf library and created table with different fields. In which Accode is one field it represents the account code of each employee.I am getting the Accode by using type,type1 fields from database and assigning the value for it.

Here type = M(Manager),type1=G(General), So I concatenated these two fields MN and assigning the Accode for employee based on it.

For Ex: Employee name: Rajesh has Accode: WA1555

Now i want to generate separate table in pdf based on the Accode.

Ex: The Employees having the Accode WA1555 in one table, and employees having other Accode like WD1589 in one table.

I need to check the Accode for the purpose of getting separate table for each Accode.

table.addCell(createRowCell(getAccode.get(0)));

At present i am getting all in one table.This is my code

public Element createMedicalOrgTable() {
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    // get current date time with Date()
    Date date = new Date();
    PdfPTable table = new PdfPTable(8);
    int orderTotal, total = 0;
    // table.setWidthPercentage(100);

        table.setWidths(new int[] { 10, 10, 15, 60, 15, 18, 15, 18 });
        table.setTotalWidth(550);
        PdfPCell cell = new PdfPCell();
        table.setLockedWidth(true);
        table.getDefaultCell().setFixedHeight(22);

        StringBuilder addr;
        String name, addr1, addr2, addr3;

        Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD,
                BaseColor.BLACK);
        Font font1 = new Font(FontFamily.HELVETICA, 8, Font.NORMAL,
                BaseColor.BLACK);
        Paragraph para = new Paragraph(
                "The Company\nMedical Payment Voucher-Cum-Bill",
                font);
        para.setLeading(0, 1);
        para.setAlignment(Element.ALIGN_CENTER);
        cell.setColspan(7);
        cell.setMinimumHeight(50);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.addElement(para);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        Paragraph para1 = new Paragraph("Orginal\nFor\nPayment", font1);
        para1.setAlignment(Element.ALIGN_CENTER);
        cell = new PdfPCell();
        cell.getBorder();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(1);
        cell.addElement(para1);
        table.addCell(cell);

        String reportSql = "select rawdata.type1,rawdata.type,rawdata.brno,medtest.MCODE,medtest.NAME,medtest.ADDRESS1,medtest.ADDRESS2,medtest.ADDRESS3,rawdata.pamount,rawdata.tax,rawdata.pamount1 from rawdata inner join medtest on medtest.MCODE = rawdata.mcode";

        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(reportSql);

        cell = new PdfPCell(new Paragraph("Tr Code", font1));
        cell.setColspan(4);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("Division Code ", font1));
        cell.setColspan(4);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("Voucher Date "
                + dateFormat.format(date), font1));
        cell.setColspan(4);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("Voucher Number ", font1));
        cell.setColspan(4);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("For ", font1));
        cell.setColspan(4);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("By Cheque", font1));
        cell.setColspan(4);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        table.addCell(createLabelCell("S.NO."));
        table.addCell(createLabelCell("BR NO"));
        table.addCell(createLabelCell("MED CODE"));
        table.addCell(createLabelCell("PAYEE"));
        table.addCell(createLabelCell("A/C CODE"));
        table.addCell(createLabelCell("AMOUNT CLIAMED"));
        table.addCell(createLabelCell("TAX(CR)"));
        table.addCell(createLabelCell("NET PAYMENT\n(AFTER TAX)"));

        int i = 0;
        while (rs.next()) {
            addr = new StringBuilder();
            String data = rs.getString("type") + rs.getString("type1");
            ArrayList<String> getAccode = getData(data);
            table.addCell(createRowCell(String.valueOf(++i)));
            table.addCell(createRowCell(rs.getString("brno")));
            table.addCell(createRowCell(rs.getString("mcode")));
            name = rs.getString("NAME");
            addr1 = rs.getString("ADDRESS1");
            addr2 = rs.getString("ADDRESS2");
            addr3 = rs.getString("ADDRESS3");

            String nameAddr = addr.append(name).append(",").append(addr1)
                    .append(",").append(addr2).append(",").append(addr3)
                    .toString();
            table.addCell(createRowCell(nameAddr));
            table.addCell(createRowCell(getAccode.get(0)));
            table.addCell(createRowCell(rs.getString("pamount")));
            table.addCell(createRowCell(rs.getString("tax")));
            table.addCell(createRowCell(rs.getString("pamount1")));
            orderTotal = rs.getInt("pamount1");
            total = total + orderTotal;         
        }
        Paragraph para2 = new Paragraph("Total Amount Rs." + total, font1);
        para2.setAlignment(Element.ALIGN_RIGHT);
        cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setColspan(8);
        cell.addElement(para2);
        table.addCell(cell);

        Paragraph para3 = new Paragraph(
                "Cheque Favouring As Per Name Indicated Above", font1);
        para3.setAlignment(Element.ALIGN_LEFT);
        cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setColspan(8);
        cell.addElement(para3);
        table.addCell(cell);

        Paragraph para4 = new Paragraph("Pay Rs." + total, font1);
        para4.setAlignment(Element.ALIGN_LEFT);
        cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setColspan(8);
        cell.addElement(para4);
        table.addCell(cell);

        Paragraph para5 = new Paragraph("(Rupees "
                + convertNumToWord(total) + " only)", font1);
        para5.setAlignment(Element.ALIGN_LEFT);
        cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setColspan(8);
        cell.addElement(para5);
        table.addCell(cell);

        table.setSpacingAfter(30.0f);

        cell = new PdfPCell(new Phrase("Asstt.", font1));
        cell.setColspan(2);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Dm(A/CS)", font1));
        cell.setColspan(2);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Dy.Finance Manager", font1));
        cell.setColspan(4);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
    return table;
}
0

There are 0 answers