1

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;
}
user3459021
  • 21
  • 1
  • 5
  • 1
    What is the question? I have looked at your code and I have read what you write about it, but I have no idea what you're asking. What is Accode? Can't you explain the requirement in simple words and then phrase a question that tells us what goes wrong. – Bruno Lowagie Jun 10 '15 at 12:53
  • @BrunoLowagie I has updated my question,once check it – user3459021 Jun 10 '15 at 14:02
  • 1
    You create one big table in a method which returns it as a result. As that table is not added to the document yet, its layout and its positioning on pages is not yet determined. If the table logically contains information which should be handled separately, e.g. to add a page break in-between, split that method into multiple methods each of which creates a table containing logically connected content. You may call `document.newPage()` between adding the tables to the document. – mkl Jun 10 '15 at 14:17
  • @mkl I need separate tables in each page – user3459021 Jun 10 '15 at 14:42
  • 1
    @user3459021 If you need separate tables in each page, then why don't you create separate tables for each page? What is keeping you from doing the obvious? – Bruno Lowagie Jun 10 '15 at 14:49
  • @BrunoLowagie I am not getting the way how to compare the Accode and get the particular records.So i created single table with entire employees – user3459021 Jun 10 '15 at 14:52
  • 1
    That is a non-question. How is this related to iText? Just create your query so that you get the employees for each account code. Or, if you must retrieve the employees all at once, sort them before you create your tables. – Bruno Lowagie Jun 10 '15 at 15:09

0 Answers0