Example usage for com.itextpdf.text.pdf PdfPTable setHeaderRows

List of usage examples for com.itextpdf.text.pdf PdfPTable setHeaderRows

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfPTable setHeaderRows.

Prototype

public void setHeaderRows(int headerRows) 

Source Link

Document

Sets the number of the top rows that constitute the header.

Usage

From source file:biblioteca.reportes.PdfCreator.java

License:Open Source License

/**
 * Dado un resultSet obtenido de una consulta, esta funcin convierte los datos
 * internos de este en una tabla que puede ser insertada en un Document de iText
 * esta PdfPTable tiene en su primera linea los nombres de las columnas del ResultSet
 * @param entrada ResultSet El resultado de una consulta a la base de datos
 * @return una PdfPTable para insertar en un documento de iText
 * @see com.itextpdf.text.pdf.PdfPTable;
 * @see java.sql.ResultSet;/*from  w ww .j  ava2  s.co  m*/
 */
static public PdfPTable resultSetToTable(ResultSet entrada) {
    PdfPTable salida = null;
    try {
        ResultSetMetaData rsmd = entrada.getMetaData();

        salida = new PdfPTable(rsmd.getColumnCount());
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            salida.addCell(rsmd.getColumnName(i));
        }
        salida.setHeaderRows(1);
        while (entrada.next()) {
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                salida.addCell(entrada.getString(i));
            }
        }
    } catch (java.sql.SQLException sqlex) {
        System.err.println(sqlex);
    }
    return salida;
}

From source file:biblioteca.reportes.PdfCreator.java

License:Open Source License

static public PdfPTable plainArrayListToPdfPTable(ArrayList<String> entrada, int columns) {
    PdfPTable salida = new PdfPTable(columns);
    for (int i = 0; i < columns; i++) {
        salida.addCell(entrada.get(i));//  w  w  w.j  av a 2  s . c  o m
    }
    salida.setHeaderRows(1);
    for (int i = columns; i < entrada.size(); i++) {
        salida.addCell(entrada.get(i));
    }
    return salida;
}

From source file:biblioteca.reportes.PdfCreator.java

License:Open Source License

/**
 * Esta funcin convierte un arrayList en una PdfPTable y, agrega una columna
 * de estadisticas a esta. Funcionar si el arrayList tiene en su ultima columna
 * numeros/*from  w  w w  .  j  av  a2 s . c  om*/
 * @param entrada Matriz con los datos estadisticos de la base de datos
 * @param total La suma de todos los datos de la ultima columna
 * @param rows El numero de columnas que se mostrarn en la PdfPTable
 * @return PdfPTable Contiene los datos del ArrayList ms los porcentajes.
 */
static public PdfPTable arrayListToStatisticTable(ArrayList<ArrayList<String>> entrada, int total, int rows) {
    PdfPTable salida = null;

    salida = new PdfPTable(entrada.get(0).size() + 1);
    for (int i = 0; i < entrada.get(0).size(); i++) {
        salida.addCell(entrada.get(0).get(i));
    }
    salida.addCell("Porcentaje");
    salida.setHeaderRows(1);
    int counter = 0;
    for (int j = 1; j < entrada.size() && counter < rows; j++) {
        for (int i = 0; i < entrada.get(j).size(); i++) {
            salida.addCell(entrada.get(j).get(i));
            if (i == entrada.get(j).size() - 1) {
                int porcentaje = (int) (Integer.parseInt(entrada.get(j).get(i)) * 100) / total;
                salida.addCell(porcentaje + "%");
            }
        }
        counter++;
    }
    if (entrada.size() - 1 > rows) {
        int otros = 0, otrospor;
        for (int i = counter; i < entrada.size(); i++) {
            otros += Integer.parseInt(entrada.get(i).get(entrada.get(i).size() - 1));
        }
        otrospor = otros * 100;
        otrospor /= total;
        salida.addCell("-");
        salida.addCell("Otros");
        salida.addCell(otros + "");
        salida.addCell(otrospor + "%");
    }
    return salida;
}

From source file:bl.pdf.PDFFile.java

private PdfPTable getTable(EntityTableModel tModel) {
    String[] headers = tModel.getColumnNames();

    PdfPTable table = new PdfPTable(headers.length);
    table.setHeaderRows(1);
    table.setWidthPercentage(100);// w ww. j  a v  a  2  s.  c om
    for (String header : headers) {
        PdfPCell c1 = new PdfPCell(new Phrase(header));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
    }

    ArrayList<DBEntity> entries = tModel.getEntries();
    if (entries.isEmpty()) {
        PdfPCell c = new PdfPCell(new Phrase("Keine Eintrge vorhanden"));
        c.setHorizontalAlignment(Element.ALIGN_CENTER);
        c.setColspan(headers.length);
        table.addCell(c);
        return table;
    }
    for (DBEntity entry : entries) {
        for (String header : headers) {
            Method method;

            Object a;
            try {
                method = entry.getClass().getMethod("get" + header, new Class<?>[0]);
                a = method.invoke(entry, new Object[0]);
                table.addCell(String.valueOf(a));
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                table.addCell(new String(""));
            } catch (SecurityException e) {
                e.printStackTrace();
                table.addCell(new String(""));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                table.addCell(new String(""));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                table.addCell(new String(""));
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                table.addCell(new String(""));
            }
        }
    }
    return table;
}

From source file:bl.pdf.PDFFile.java

private PdfPTable getRechnungszeileTable(Rechnung r) throws DALException {
    String[] headers;//from   w  w  w . jav a  2 s .c  om
    if (r instanceof Eingangsrechnung) {
        String[] h = { "Rechnungszeile", "Kommentar", "Steuersatz", "Betrag", "ohne Steuer" };
        headers = h;
    } else {
        String[] h = { "Rechnungszeile", "Kommentar", "AngebotID", "Steuersatz", "Betrag", "ohne Steuer" };
        headers = h;
    }
    PdfPTable table = new PdfPTable(headers.length);
    table.setHeaderRows(1);
    table.setWidthPercentage(100);
    for (String header : headers) {
        PdfPCell c1 = new PdfPCell(new Phrase(header));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
    }

    ArrayList<Rechnungszeile> rechnungszeilen = BL.getRechnungszeileListe(r.getRechnungID());
    if (rechnungszeilen.isEmpty()) {
        PdfPCell c = new PdfPCell(new Phrase("Keine Rechnungszeilen vorhanden"));
        c.setHorizontalAlignment(Element.ALIGN_CENTER);
        if (r instanceof Ausgangsrechnung) {
            c.setColspan(6);
        } else {
            c.setColspan(5);
        }
        table.addCell(c);
        return table;
    }
    double summe = 0;
    double summeOhne = 0;
    for (Rechnungszeile rz : rechnungszeilen) {
        table.addCell(String.valueOf(rz.getRechnungszeileID()));
        table.addCell(String.valueOf(rz.getKommentar()));
        if (r instanceof Ausgangsrechnung) {
            table.addCell(String.valueOf(rz.getAngebotID()));
        }
        table.addCell(String.valueOf(rz.getSteuersatz()));
        table.addCell(String.valueOf(rz.getBetrag()));
        double betrag = rz.getBetrag();
        double steuersatz = rz.getSteuersatz();
        double betragOhne = betrag - (betrag / 100 * steuersatz);
        table.addCell(String.valueOf(betragOhne));
        summe += rz.getBetrag();
        summeOhne += betragOhne;
    }

    PdfPCell c = new PdfPCell(new Phrase("Summe"));
    c.setHorizontalAlignment(Element.ALIGN_RIGHT);
    if (r instanceof Ausgangsrechnung) {
        c.setColspan(4);
    } else {
        c.setColspan(3);
    }
    table.addCell(c);
    table.addCell(String.valueOf(summe));
    table.addCell(String.valueOf(summeOhne));

    return table;

}

From source file:bl.pdf.PDFFile.java

private void createTable(Section subCatPart) throws BadElementException {
    PdfPTable table = new PdfPTable(3);

    // t.setBorderColor(BaseColor.GRAY);
    // t.setPadding(4);
    // t.setSpacing(4);
    // t.setBorderWidth(1);

    PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);/*from   w ww .  ja v  a2 s .  c om*/

    c1 = new PdfPCell(new Phrase("Table Header 2"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Table Header 3"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);
    table.setHeaderRows(1);

    table.addCell("1.0");
    table.addCell("1.1");
    table.addCell("1.2");
    table.addCell("2.1");
    table.addCell("2.2");
    table.addCell("2.3");

    subCatPart.add(table);

}

From source file:bookshopautomationsoftware.GenerateReceipt.java

private static void createTable(Section subCatPart) throws BadElementException {
    PdfPTable table = new PdfPTable(4);

    // t.setBorderColor(BaseColor.GRAY);
    // t.setPadding(4);
    // t.setSpacing(4);
    // t.setBorderWidth(1);
    addEmptyLine(preface, 2);/*from w  ww .j  av a  2 s .c  om*/
    PdfPCell c1 = new PdfPCell(new Phrase("slno"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("isbn"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("quantity"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("price"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    table.setHeaderRows(1);

    try {
        Connection con = DriverManager.getConnection(DB_URL1, USER1, PASS1);
        Statement stmnt = con.createStatement();
        String sql = "SELECT slno,book,quantity FROM cart";
        Connection con1 = DriverManager.getConnection(DB_URL1, USER1, PASS1);
        Statement stmnt1 = con.createStatement();
        int count = 0;
        ResultSet rs = stmnt.executeQuery(sql);
        ResultSet rs1;
        float total = 0;
        while (rs.next()) {
            String sql1 = "SELECT isbn,price FROM booktable WHERE isbn = " + rs.getLong("book");
            rs1 = stmnt1.executeQuery(sql1);
            rs1.next();
            count++;
            table.addCell("" + rs.getInt("slno"));
            table.addCell("" + rs.getLong("book"));
            table.addCell("" + rs.getInt("quantity"));
            table.addCell("" + rs1.getFloat("price"));
            total += (rs1.getFloat("price")) * (rs.getInt("quantity"));

            //  model.addRow(new Object[]{(""+rs.getInt("slno")),""+rs.getLong("book"),""+rs.getInt("quantity")});
        }
        table.addCell("");
        table.addCell("");
        table.addCell("");
        table.addCell("" + total);
        if (count == 0) {
            // dispose();

            return;
        }

    } catch (Exception e) {
        System.out.println(" " + e + " is caught");
    }
    /*table.addCell("1.0");
    table.addCell("1.1");
    table.addCell("1.2");
    table.addCell("2.1");
    table.addCell("2.2");
    table.addCell("2.3");*/

    subCatPart.add(table);

}

From source file:bouttime.report.award.AwardReport.java

License:Open Source License

/**
 * Generate an award report.//w w w  .  j a v  a  2s  . c o m
 * @param dao Dao object to use to retrieve data.
 * @param session Session to generate the report for.
 * @param group Group to generate report for.  This takes precedence, so
 * if not null, then the report will be generated for this group.
 * @return True if the report was generated.
 */
private static boolean doReport(Dao dao, String session, Group group) {
    if (!dao.isOpen()) {
        return false;
    }

    // step 1: creation of a document-object
    Document document = new Document();

    try {

        // step 2: creation of the writer
        FileOutputStream fos = createOutputFile();
        if (fos == null) {
            return false;
        }
        PdfWriter.getInstance(document, fos);

        // step 3: we open the document
        document.open();

        // step 4: create and add content

        // create and add the header
        Paragraph p1 = new Paragraph(new Paragraph(
                String.format("%s    %s %s, %s", dao.getName(), dao.getMonth(), dao.getDay(), dao.getYear()),
                FontFactory.getFont(FontFactory.HELVETICA, 10)));
        document.add(p1);

        Paragraph p2 = new Paragraph(
                new Paragraph("Award Report", FontFactory.getFont(FontFactory.HELVETICA, 14)));
        p2.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(p2);

        Font headerFont = new Font(Font.FontFamily.TIMES_ROMAN, 12);
        Font detailFont = new Font(Font.FontFamily.TIMES_ROMAN, 10);
        PdfPCell headerCell = new PdfPCell();
        headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        headerCell.setPadding(3);
        headerCell.setBorderWidth(2);

        List<Group> groups;
        if (group != null) {
            groups = new ArrayList<Group>();
            groups.add(group);
        } else if (session != null) {
            groups = dao.getGroupsBySession(session);
        } else {
            groups = dao.getAllGroups();
        }

        for (Group g : groups) {

            // create and add the table
            PdfPTable datatable = new PdfPTable(4);
            int colWidths[] = { 30, 30, 30, 10 }; // percentage
            datatable.setWidths(colWidths);
            datatable.setWidthPercentage(100);
            datatable.getDefaultCell().setPadding(3);
            datatable.getDefaultCell().setBorderWidth(2);
            datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

            // The header has the group name
            headerCell.setPhrase(new Phrase(g.toString(), headerFont));
            headerCell.setColspan(4);
            datatable.addCell(headerCell);

            datatable.setHeaderRows(1); // this is the end of the table header

            datatable.getDefaultCell().setBorderWidth(1);
            datatable.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);

            List<Wrestler> wList = getSortedAwardList(g);

            int i = 0;
            for (Wrestler w : wList) {
                if ((i++ % 2) == 0) {
                    datatable.getDefaultCell().setGrayFill(0.9f);
                } else {
                    datatable.getDefaultCell().setGrayFill(1);
                }

                datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                datatable.addCell(new Phrase(w.getFirstName(), detailFont));
                datatable.addCell(new Phrase(w.getLastName(), detailFont));
                datatable.addCell(new Phrase(w.getTeamName(), detailFont));
                datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                Integer place = w.getPlace();
                String placeStr = (place == null) ? "" : place.toString();
                datatable.addCell(new Phrase(placeStr, detailFont));
            }

            datatable.setSpacingBefore(5f);
            datatable.setSpacingAfter(15f);
            document.add(datatable);
        }

    } catch (DocumentException de) {
        logger.error("Document Exception", de);
        return false;
    }

    // step 5: we close the document
    document.close();

    return true;
}

From source file:bouttime.report.boutsequence.BoutSequenceReport.java

License:Open Source License

private static PdfPTable addBoutSequences(List<Wrestler> wList, String session) throws DocumentException {
    // create and add the table
    Font headerFont = new Font(Font.FontFamily.TIMES_ROMAN, 12);
    Font detailFont = new Font(Font.FontFamily.TIMES_ROMAN, 10);
    PdfPCell headerCell = new PdfPCell();
    headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    headerCell.setPadding(3);/*w  w w .j  a  va 2  s  .  com*/
    headerCell.setBorderWidth(2);
    PdfPTable datatable = new PdfPTable(3);
    int colWidths[] = { 5, 25, 70 }; // percentage
    datatable.setWidths(colWidths);
    datatable.setWidthPercentage(100);
    datatable.getDefaultCell().setPadding(3);
    datatable.getDefaultCell().setBorderWidth(2);
    datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

    headerCell.setPhrase(new Phrase("Mat", headerFont));
    datatable.addCell(headerCell);

    headerCell.setPhrase(new Phrase("Name", headerFont));
    datatable.addCell(headerCell);

    headerCell.setPhrase(new Phrase("Bout Sequence", headerFont));
    datatable.addCell(headerCell);

    datatable.setHeaderRows(1); // this is the end of the table header

    datatable.getDefaultCell().setBorderWidth(1);
    datatable.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);

    Collections.sort(wList, new WrestlerMatNameSort());

    int i = 0;
    for (Wrestler w : wList) {
        if (w.getGroup() == null) {
            logger.debug(String.format("Wrestler [%s] is not in a group, skipping.", w.getShortName()));
            continue;
        }

        if ((session != null) && !session.equalsIgnoreCase(w.getGroup().getSession())) {
            logger.debug(String.format("Wrestler [%s] is in a group but not in session %s, skipping.",
                    w.getShortName(), session));
            continue;
        }

        if ((i++ % 2) == 0) {
            datatable.getDefaultCell().setGrayFill(0.9f);
        } else {
            datatable.getDefaultCell().setGrayFill(1);
        }

        List<Bout> bList = BoutSequence.calculate(w);
        datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        datatable.addCell(new Phrase(w.getGroup().getMat(), detailFont));
        datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        datatable.addCell(new Phrase(String.format("%s %s", w.getFirstName(), w.getLastName()), detailFont));
        datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        String boutSequenceString = getBoutSequenceString(bList);
        datatable.addCell(new Phrase(boutSequenceString, detailFont));
    }

    datatable.setSpacingBefore(5f);
    datatable.setSpacingAfter(15f);
    return (i > 0 ? datatable : null);
}

From source file:bouttime.report.matkey.MatKeyReport.java

License:Open Source License

/**
 * Generate the Mat Key report.// w w w.  j  av a2 s . com
 * This is a color-coded report that maps the groups to a mat.
 * @param dao Dao object to use to retrieve data.
 * @return True if the report was generated.
 */
public static boolean doReport(Dao dao) {
    if (!dao.isOpen()) {
        logger.warn("Cannot create report : DAO not open");
        return false;
    }

    String matValues = dao.getMatValues();
    if ((matValues == null) || matValues.isEmpty()) {
        JFrame mainFrame = BoutTimeApp.getApplication().getMainFrame();
        JOptionPane.showMessageDialog(mainFrame,
                "Mat values are not configured." + "\nSet the mat values in 'Edit -> Configuration'",
                "Mat Key Report error", JOptionPane.WARNING_MESSAGE);
        logger.warn("Cannot create report : mat values not configured");
        return false;
    }

    // step 1: creation of a document-object
    Document document = new Document();

    try {

        // step 2: creation of the writer
        FileOutputStream fos = createOutputFile();
        if (fos == null) {
            return false;
        }
        PdfWriter.getInstance(document, fos);

        // step 3: we open the document
        document.open();

        // step 4: create and add content

        // create and add the header
        Paragraph p1 = new Paragraph(new Paragraph(
                String.format("%s    %s %s, %s", dao.getName(), dao.getMonth(), dao.getDay(), dao.getYear()),
                FontFactory.getFont(FontFactory.HELVETICA, 10)));
        document.add(p1);

        Paragraph p2 = new Paragraph(
                new Paragraph("Mat Key Report", FontFactory.getFont(FontFactory.HELVETICA, 14)));
        p2.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(p2);

        int cols = 4; // Class, Div, Weight, Mat

        // create and add the table
        PdfPTable datatable = new PdfPTable(cols);
        //int colWidths[] = { 55, 15, 15, 15 };   // percentage
        //datatable.setWidths(colWidths);
        datatable.getDefaultCell().setPadding(3);
        datatable.getDefaultCell().setBorderWidth(2);
        datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

        datatable.addCell("Class");
        datatable.addCell("Div");
        datatable.addCell("Weight");
        datatable.addCell("Mat");
        datatable.setHeaderRows(1); // this is the end of the table header

        datatable.getDefaultCell().setBorderWidth(1);

        // Prepare the list of groups
        List<Group> groups = dao.getAllGroups();
        Collections.sort(groups, new GroupClassDivWtSort());

        // Prepare the list of mat values
        String[] mats = matValues.split(",");
        List<String> matList = new ArrayList<String>();
        for (String m : mats) {
            matList.add(m.trim());
        }

        for (Group g : groups) {
            String mat = g.getMat();
            int idx = matList.indexOf(mat);
            datatable.getDefaultCell().setBackgroundColor(colorMap.get(idx));

            datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
            datatable.addCell(g.getClassification());
            datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            datatable.addCell(g.getAgeDivision());
            datatable.addCell(g.getWeightClass());
            datatable.addCell(g.getMat());
        }

        datatable.setSpacingBefore(15f); // space between title and table
        document.add(datatable);

    } catch (DocumentException de) {
        logger.error("Document Exception", de);
        return false;
    }

    // step 5: we close the document
    document.close();

    return true;
}