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

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

Introduction

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

Prototype

public void setHorizontalAlignment(final int horizontalAlignment) 

Source Link

Document

Sets the horizontal alignment of the table relative to the page.

Usage

From source file:fxml.test.PDFService.java

public PdfPTable createFooter1() {

    //String[] names = new String[]{"Md. Eamin Rahman", "Md. Mujibur Rahman", "Md Masum", "Md. Saiful Islam", "Husne Ara Chowdhury", "Sabir Ismail"};
    PdfPTable table = new PdfPTable(5);
    table.setHorizontalAlignment(Element.ALIGN_LEFT);

    try {/* w w w . ja  va  2 s  .  co m*/
        table.setTotalWidth(new float[] { 161f, 161f, 133f, 167f, 161f });
        table.setLockedWidth(true);

    } catch (DocumentException ex) {
        Logger.getLogger(PDFService.class.getName()).log(Level.SEVERE, null, ex);
    }
    //table.setWidthPercentage(100);
    PdfPCell chairmanSIgnature = new PdfPCell(new Paragraph("Signature of the Chairman:", font9));
    chairmanSIgnature.setBorder(Rectangle.NO_BORDER);
    chairmanSIgnature.setPaddingLeft(0f);
    chairmanSIgnature.setPaddingTop(5);
    table.addCell(chairmanSIgnature);

    PdfPCell underLine = new PdfPCell(new Paragraph("_______________________"));
    underLine.setBorder(Rectangle.NO_BORDER);
    table.addCell(underLine);

    PdfPCell blankColumn = new PdfPCell(new Paragraph(" "));
    blankColumn.setBorder(Rectangle.NO_BORDER);
    table.addCell(blankColumn);

    Paragraph p = new Paragraph("Signature of The Controller of Examinations:", font9);
    p.setLeading(0, 1.3f);
    PdfPCell controllerSignature = new PdfPCell();
    controllerSignature.addElement(p);
    controllerSignature.setBorder(Rectangle.NO_BORDER);
    table.addCell(controllerSignature);
    table.addCell(underLine);

    PdfPCell cell1 = new PdfPCell(new Paragraph(inputs.get(5).trim(), font9));
    cell1.setPaddingTop(0f);
    cell1.setBorder(Rectangle.NO_BORDER);

    PdfPCell cell2 = new PdfPCell(new Paragraph(inputs.get(6).trim(), font9));
    cell2.setPaddingTop(0f);
    cell2.setBorder(Rectangle.NO_BORDER);

    PdfPCell nameColumn = new PdfPCell(new Paragraph("Name :", font9));
    nameColumn.setBorder(Rectangle.NO_BORDER);
    nameColumn.setPaddingLeft(0f);
    nameColumn.setPaddingTop(0f);

    PdfPCell nameColumn2 = new PdfPCell(new Paragraph("Name :", font9));
    nameColumn2.setBorder(Rectangle.NO_BORDER);
    nameColumn2.setPaddingTop(0f);

    table.addCell(nameColumn);
    table.addCell(cell1);
    table.addCell(blankColumn);
    table.addCell(nameColumn2);
    table.addCell(cell2);

    table.setSpacingAfter(23.5f);
    return table;
}

From source file:fxml.test.PDFService.java

public PdfPTable createFooter2() {

    PdfPTable table = new PdfPTable(8);
    table.setHorizontalAlignment(Element.ALIGN_LEFT);

    try {/*from  ww w .  j av a 2 s .c o m*/
        table.setTotalWidth(new float[] { 192f, 144f, 5f, 144f, 5f, 144f, 5f, 144f });
        table.setLockedWidth(true);

    } catch (DocumentException ex) {
        Logger.getLogger(PDFService.class.getName()).log(Level.SEVERE, null, ex);
    }

    PdfPCell underLine = new PdfPCell(new Paragraph("_____________________"));
    underLine.setBorder(Rectangle.NO_BORDER);

    PdfPCell blankColumn = new PdfPCell(new Paragraph(" "));
    blankColumn.setBorder(Rectangle.NO_BORDER);

    PdfPCell blankColumn2 = new PdfPCell(new Paragraph(" "));
    blankColumn2.setBorder(Rectangle.BOTTOM);

    PdfPCell nameColumn = new PdfPCell(new Paragraph("Name :", font9));
    nameColumn.setBorder(Rectangle.NO_BORDER);
    nameColumn.setPaddingLeft(0f);

    PdfPCell cell3 = new PdfPCell(new Paragraph(inputs.get(7).trim(), font9));
    cell3.setPaddingRight(2);
    cell3.setBorder(Rectangle.TOP);

    PdfPCell cell4 = new PdfPCell(new Paragraph(inputs.get(8).trim(), font9));
    cell4.setBorder(Rectangle.TOP);

    PdfPCell cell5 = new PdfPCell(new Paragraph(inputs.get(9).trim(), font9));
    cell5.setBorder(Rectangle.TOP);

    PdfPCell cell6 = new PdfPCell(new Paragraph(inputs.get(10).trim(), font9));
    cell6.setBorder(Rectangle.TOP);

    PdfPCell memberSignature = new PdfPCell(new Paragraph("Signature of the Members:", font9));
    memberSignature.setPaddingLeft(0f);
    memberSignature.setBorder(Rectangle.NO_BORDER);

    table.addCell(memberSignature);
    table.addCell(blankColumn);
    table.addCell(blankColumn);
    table.addCell(blankColumn);
    table.addCell(blankColumn);
    table.addCell(blankColumn);
    table.addCell(blankColumn);
    table.addCell(blankColumn);

    table.addCell(nameColumn);
    table.addCell(cell3);
    table.addCell(blankColumn);
    table.addCell(cell4);
    table.addCell(blankColumn);
    table.addCell(cell5);
    table.addCell(blankColumn);
    table.addCell(cell6);

    PdfPCell tabulatorSignature = new PdfPCell(new Paragraph("Signature of the Tabulators:", font9));
    tabulatorSignature.setPaddingLeft(0f);
    tabulatorSignature.setPaddingTop(13f);
    tabulatorSignature.setBorder(Rectangle.NO_BORDER);

    table.addCell(tabulatorSignature);
    table.addCell(blankColumn2);
    table.addCell(blankColumn);
    table.addCell(blankColumn2);
    table.addCell(blankColumn);
    table.addCell(blankColumn2);
    table.addCell(blankColumn);
    table.addCell(blankColumn2);

    return table;

}

From source file:fxml.test.PDFService.java

public PdfPTable createTableHeader(int start) {

    PdfPTable table = null;

    try {//from   w  w w  . j a  v a 2 s.c o m

        int colCount = 1;
        List<Float> columnsList = new ArrayList<>();

        columnsList.add(57.5f);
        columnsList.add(159.4f);

        for (int i = start; i < list.size(); i++) {

            if (list.get(i) instanceof Course) {

                columnsList.add(44f);

            } else if (list.get(i).equals("Total Credit")) {

                columnsList.add(30f);

            } else if (list.get(i).equals("Total GPA")) {

                columnsList.add(30f);

            } else if (list.get(i).equals("Letter Grade")) {

                columnsList.add(30f);

            } else if (list.get(i).equals("Cumulative")) {

                columnsList.add(88f);

            } else {

                columnsList.add(66f);

            }

            if (colCount == 12) {
                break;
            }
            colCount++;

        }

        float[] columns = new float[columnsList.size()];

        for (int i = 0; i < columnsList.size(); i++) {
            columns[i] = columnsList.get(i);
        }

        table = new PdfPTable(columns.length);

        System.err.println("table size :" + table.getNumberOfColumns());

        table.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.setTotalWidth(columns);
        table.setLockedWidth(true);

    } catch (DocumentException ex) {
        Logger.getLogger(PDFService.class.getName()).log(Level.SEVERE, null, ex);
    }

    PdfPCell regCell = getCellForString("Reg No.", 0, true, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER, font9,
            false);
    regCell.setPaddingTop(0f);
    table.addCell(regCell);

    PdfPCell nameCell = getNameCell();
    nameCell.setPaddingBottom(2f);
    table.addCell(nameCell);

    int colCount = 1;

    for (int i = start; i < list.size(); i++) {

        if (list.get(i) instanceof Course) {

            Course course = (Course) list.get(i);

            PdfPCell cell3 = new PdfPCell(createCourseInfo(course));
            cell3.setPaddingTop(1f);
            cell3.setPaddingBottom(2f);
            //cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell3);

        } else if (list.get(i).equals("Total Credit") || list.get(i).equals("Total GPA")
                || list.get(i).equals("Letter Grade")) {

            String str = (String) list.get(i);
            String s[] = str.split(" ");

            PdfPTable totalCredit = new PdfPTable(1);
            totalCredit.setSpacingBefore(12.5f);
            totalCredit.setWidthPercentage(100);
            totalCredit.getDefaultCell().setBorder(Rectangle.NO_BORDER);

            PdfPCell cell1 = getCellForString(s[0], 0, false, 0, Element.ALIGN_CENTER,
                    new Font(Font.FontFamily.TIMES_ROMAN, 10f), false);
            PdfPCell cell2 = getCellForString(s[1], 0, false, 0, Element.ALIGN_CENTER,
                    new Font(Font.FontFamily.TIMES_ROMAN, 10f), false);

            totalCredit.addCell(cell1);
            totalCredit.addCell(cell2);

            PdfPCell grade = new PdfPCell(totalCredit);
            table.addCell(grade);

        } else if (list.get(i).equals("Cumulative")) {

            PdfPTable cumulative = new PdfPTable(1);
            // cumulative.setPaddingTop(count);
            cumulative.addCell(getCellForString("Cumulative", 0, false, Element.ALIGN_MIDDLE,
                    Element.ALIGN_CENTER, font10, true));

            PdfPTable creditGpaGrade = new PdfPTable(3);

            creditGpaGrade.setTotalWidth(88f);
            creditGpaGrade.setLockedWidth(true);
            creditGpaGrade.addCell(getCellForString("Credit", 0, false, Element.ALIGN_MIDDLE,
                    Element.ALIGN_CENTER, font10, true));
            creditGpaGrade.addCell(getCellForString("GPA", 0, false, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,
                    font10, true));
            creditGpaGrade.addCell(getCellForString("Grade", 0, false, Element.ALIGN_MIDDLE,
                    Element.ALIGN_CENTER, font10, true));

            PdfPTable p = new PdfPTable(1);
            PdfPCell cell1 = new PdfPCell(cumulative);
            cell1.setPaddingBottom(3f);
            cell1.setBorder(Rectangle.NO_BORDER);
            PdfPCell cell2 = new PdfPCell(creditGpaGrade);
            cell2.setBorder(Rectangle.NO_BORDER);
            p.addCell(cell1);
            p.addCell(cell2);
            table.addCell(p);

        } else {

            table.addCell(getCellForString((String) list.get(i), 0, true, Element.ALIGN_MIDDLE,
                    Element.ALIGN_CENTER, font9, true));

        }

        if (colCount == 12) {
            break;
        }
        colCount++;
    }

    return table;

}

From source file:generators.InvoiceGenerator.java

/**
 * Generate Invoice pdf file//from  w  ww . j a  va 2  s  . c o  m
 *
 * @param order the order
 * @throws DocumentException the document exception
 * @throws IOException       the io exception
 */
public void generate(Order order) throws DocumentException, IOException {
    Date invoiceDate = order.getDate();
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM YYYY");
    //        User guest = order.getGuest();
    User guest = new UserDAO().get(order.getUserId());
    guest.setOrder(order);
    System.out.println("invoice generator orderID: " + order.getId());
    Document document = new Document();
    Font defaultFont = new Font(Font.FontFamily.TIMES_ROMAN, 12);
    System.out.println(System.getProperty("user.dir"));
    PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream(System.getProperty("user.dir") + "/src/main/java/nl/ipsen3/invoice/"
                    + new SimpleDateFormat("dd-MM-yyyy").format(invoiceDate) + " - " + order.getId() + ".pdf"));
    document.setMargins(30, 30, 30, 65);
    writer.setPageEvent(new InvoiceEventListener());
    document.open();
    Paragraph header = new Paragraph("Lionsclub Oegstgeest/Warmond",
            new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD));
    header.setAlignment(Element.ALIGN_CENTER);
    document.add(header);
    AddressDAO addressDAO = new AddressDAO();
    Address userAddress = addressDAO.get(guest.getAddressId());
    Paragraph address = new Paragraph(guest.getFirstName() + " " + guest.getPrefixLastName() + " "
            + guest.getLastName() + "\n" + userAddress.getStreet() + " " + userAddress.getHouseNumber() + "\n"
            + userAddress.getZipCode() + " " + userAddress.getCity(), defaultFont);
    address.setSpacingBefore(35);
    address.setSpacingAfter(25);
    address.setLeading(15);
    document.add(address);

    Paragraph invoiceDetails = new Paragraph("Factuurdatum: " + sdf.format(invoiceDate) + "\n"
            + "FactuurNummer: " + order.getId() + " \n" + "Debiteurennummer: " + guest.getId(), defaultFont);

    invoiceDetails.setSpacingAfter(15);
    invoiceDetails.setLeading(15);
    document.add(invoiceDetails);

    Paragraph subject = new Paragraph("Betreft: Onderwerp factuur", defaultFont);
    subject.setSpacingAfter(30);
    document.add(subject);

    PdfPTable orderTable = new PdfPTable(10);
    PdfPCell wineCell = new PdfPCell(new Paragraph("Wijn", defaultFont));
    wineCell.setColspan(5);
    wineCell.setBorder(Rectangle.BOTTOM);
    orderTable.getDefaultCell().setPaddingBottom(10);
    orderTable.getDefaultCell().setBorder(Rectangle.BOTTOM);
    ;

    orderTable.addCell(new Paragraph("Code", defaultFont));
    orderTable.addCell(new Paragraph("Aantal", defaultFont));
    orderTable.addCell(wineCell);
    orderTable.addCell(new Paragraph("Jaar", defaultFont));
    orderTable.addCell(new Paragraph("Per Fles", defaultFont));
    orderTable.addCell(new Paragraph("Bedrag", defaultFont));

    orderTable.getDefaultCell().setPaddingBottom(0);
    orderTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
    wineCell.setBorder(Rectangle.NO_BORDER);

    NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.GERMANY);

    for (WineOrder wineOrder : order.getWineOrders()) {
        orderTable.addCell(new Paragraph("" + wineOrder.getWine().getId(), defaultFont));
        orderTable.addCell(new Paragraph("" + wineOrder.getAmount(), defaultFont));
        wineCell.setPhrase(new Phrase(wineOrder.getWine().getName(), defaultFont));
        orderTable.addCell(wineCell);
        orderTable.addCell(new Paragraph("" + wineOrder.getWine().getYear(), defaultFont));
        orderTable.addCell(new Paragraph(
                " " + numberFormat.format(wineOrder.getWine().getPrice()).replace(" ", ""), defaultFont));
        orderTable.addCell(new Paragraph(" " + numberFormat
                .format(wineOrder.getAmount() * wineOrder.getWine().getPrice()).replace(" ", ""),
                defaultFont));

        orderTable.completeRow();
    }

    orderTable.addCell(" ");
    orderTable.completeRow();
    Font totalFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
    PdfPCell totalAmount = new PdfPCell(
            new Paragraph(" " + numberFormat.format(order.getTotalAmount()).replace(" ", ""), totalFont));
    totalAmount.setBorder(Rectangle.TOP);
    totalAmount.setPaddingTop(10);
    PdfPCell totalCell = new PdfPCell(new Paragraph("Totaal", totalFont));
    totalCell.setPaddingTop(10);
    totalCell.setBorder(Rectangle.NO_BORDER);
    orderTable.addCell(totalCell);
    PdfPCell fillerCell = new PdfPCell(new Paragraph(""));
    fillerCell.setColspan(8);
    fillerCell.setBorder(Rectangle.NO_BORDER);
    orderTable.addCell(fillerCell);
    orderTable.addCell(totalAmount);

    orderTable.setSpacingBefore(15);
    orderTable.setSpacingAfter(30);
    orderTable.setWidthPercentage(95);
    orderTable.setHorizontalAlignment(Element.ALIGN_CENTER);
    document.add(orderTable);

    Paragraph retrievalDetails = new Paragraph(
            "Wij verzoeken u vriendelijk het totaalbedrag binnen 7 dagen na factuurdatum over te maken op bankrekening <bankAccountNr> t.n.v <bankAccountName> onder vermelding van het factuurnummer",
            defaultFont);
    retrievalDetails.setLeading(15);
    retrievalDetails.setSpacingAfter(20);
    document.add(retrievalDetails);

    //        document.add(new Paragraph("U kunt uw wijnen ophalen op " +  sdf.format(invoiceDate) , defaultFont));
    //        document.add(new Paragraph("Adres:", defaultFont));
    //
    //        PdfPTable addressTable = new PdfPTable(1);
    //        addressTable.setSpacingBefore(5);
    //        addressTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
    //        addressTable.getDefaultCell().setPaddingLeft(35);
    //        MerchantService merchantService = new MerchantService();
    //        Merchant merchant = merchantService.find(merchantService.all().get(0).getId());
    //        addressTable.addCell(new Paragraph(merchant.getName(), defaultFont));
    //        addressTable.addCell(new Paragraph(merchant.getAddress().getStreet()
    //                + " " + merchant.getAddress().getHouseNumber(), defaultFont));
    //        addressTable.addCell(new Paragraph(merchant.getAddress().getZipCode() + " " +
    //                merchant.getAddress().getCity(), defaultFont));
    //        addressTable.setHorizontalAlignment(Element.ALIGN_LEFT);
    //        document.add(addressTable);

    document.close();
    System.out.println(
            "Succesfully generated IPSEN2.invoice: " + order.getId() + " on Date: " + sdf.format(invoiceDate));
}

From source file:ics4u.ics4u_final_project.Recipe.java

License:Open Source License

private void addData(Document doc) {
    //create the format for the % Daily Value
    DecimalFormat onePer = new DecimalFormat("#,##0%");
    try {/*from  ww w . j a  va  2 s.co m*/
        //add the title of the recipe
        doc.add(new Paragraph(title, TITLE_FONT));
        //add each of the ingredients
        for (int i = 0; i < ingredients.size(); i++) {
            doc.add(new Paragraph(ingredients.get(i).getFormattedName(), LABEL_NORMAL));
        }
        //add the instructions
        Paragraph p = new Paragraph();
        if (!instructions.equals("")) {
            p = new Paragraph(new Phrase("Instructions:\n", LABEL_FONT));
            doc.add(p);
            p = new Paragraph();
            p.add(new Phrase(instructions + "\n", LABEL_NORMAL));
        } else {
            p.add(new Phrase("\n"));
        }
        //create the nutrient value table
        PdfPTable label = new PdfPTable(10);
        label.setWidthPercentage(30);
        //Top
        PdfPCell c = new PdfPCell(new Phrase("Nutrition Facts", LABEL_FONT));
        c.setColspan(10);
        c.setBorderWidthBottom(0);
        label.addCell(c);
        c = new PdfPCell(new Phrase("Per " + servingName, LABEL_BOLD));
        c.setColspan(10);
        c.setBorderWidthTop(0);
        label.addCell(c);
        //Amount
        PdfPCell c2a = new PdfPCell(new Phrase("Amount", LABEL_SMALL));
        c2a.setColspan(3);
        c2a.setBorderWidthRight(0);
        PdfPCell c2b = new PdfPCell(new Phrase("% Daily Value", LABEL_SMALL));
        c2b.setColspan(10);
        c2b.setHorizontalAlignment(Element.ALIGN_RIGHT);
        c2b.setBorderWidthLeft(0);
        label.addCell(c2a);
        label.addCell(c2b);
        //Calories
        c = new PdfPCell(new Phrase("Calories " + (int) (nutrients[208] / servings), LABEL_BOLD));
        c.setColspan(10);
        c.setBorderWidthTop(2);
        label.addCell(c);
        //Fat
        PdfPCell ca = new PdfPCell(new Phrase("Fat " + (int) (nutrients[204] / servings) + "g", LABEL_BOLD));
        ca.setColspan(6);
        ca.setBorderWidthRight(0);
        PdfPCell cb = new PdfPCell(new Phrase(onePer.format((nutrients[204] / servings) / 60.0), LABEL_BOLD));
        cb.setColspan(10);
        cb.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cb.setBorderWidthLeft(0);
        label.addCell(ca);
        label.addCell(cb);
        //fat 2
        ca = new PdfPCell(new Phrase("Saturated " + ((int) (nutrients[606] / servings)) + "g\n\t+ Trans "
                + ((int) (nutrients[605] / servings)) + "g", LABEL_NORMAL));
        ca.setColspan(6);
        ca.setIndent(16);
        ca.setBorderWidthRight(0);
        cb = new PdfPCell(new Phrase(onePer.format((nutrients[606] / servings) / 20.0), LABEL_BOLD));
        cb.setColspan(10);
        cb.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cb.setBorderWidthLeft(0);
        label.addCell(ca);
        label.addCell(cb);
        //Cholesterol
        c = new PdfPCell(new Phrase("Cholesterol " + ((int) (nutrients[601] / servings)) + "mg", LABEL_BOLD));
        c.setColspan(10);
        label.addCell(c);
        //Sodium
        ca = new PdfPCell(new Phrase("Sodium " + (int) (nutrients[307] / servings) + "mg", LABEL_BOLD));
        ca.setColspan(6);
        ca.setBorderWidthRight(0);
        cb = new PdfPCell(new Phrase(onePer.format((nutrients[307] / servings) / 2400.0), LABEL_BOLD));
        cb.setColspan(10);
        cb.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cb.setBorderWidthLeft(0);
        label.addCell(ca);
        label.addCell(cb);
        //Carbs
        ca = new PdfPCell(new Phrase("Carbohydrate " + (int) (nutrients[205] / servings) + "g", LABEL_BOLD));
        ca.setColspan(7);
        ca.setBorderWidthRight(0);
        cb = new PdfPCell(new Phrase(onePer.format((nutrients[205] / servings) / 300.0), LABEL_BOLD));
        cb.setColspan(10);
        cb.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cb.setBorderWidthLeft(0);
        label.addCell(ca);
        label.addCell(cb);
        //Fibre
        ca = new PdfPCell(new Phrase("Fibre " + (int) (nutrients[291] / servings) + "g", LABEL_NORMAL));
        ca.setColspan(6);
        ca.setIndent(16);
        ca.setBorderWidthRight(0);
        cb = new PdfPCell(new Phrase(onePer.format((nutrients[291] / servings) / 25.0), LABEL_BOLD));
        cb.setColspan(10);
        cb.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cb.setBorderWidthLeft(0);
        label.addCell(ca);
        label.addCell(cb);
        //Sugar
        ca = new PdfPCell(new Phrase("Sugars " + (int) (nutrients[269] / servings) + "g", LABEL_NORMAL));
        ca.setColspan(10);
        ca.setIndent(16);
        label.addCell(ca);
        //Protein
        ca = new PdfPCell(new Phrase("Protein " + (int) (nutrients[203] / servings) + "g", LABEL_BOLD));
        ca.setColspan(10);
        label.addCell(ca);
        //Vit A
        ca = new PdfPCell(new Phrase("Vitamin A", LABEL_NORMAL));
        ca.setColspan(6);
        ca.setBorderWidthRight(0);
        ca.setBorderWidthTop(2);
        cb = new PdfPCell(cb = new PdfPCell(new Phrase(
                onePer.format(
                        ((nutrients[319] + (nutrients[321] / 12) + (nutrients[834] / 24)) / servings) / 1000),
                LABEL_NORMAL)));
        cb.setColspan(10);
        cb.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cb.setBorderWidthLeft(0);
        cb.setBorderWidthTop(2);
        label.addCell(ca);
        label.addCell(cb);
        //Vit C
        ca = new PdfPCell(new Phrase("Vitamin C", LABEL_NORMAL));
        ca.setColspan(6);
        ca.setBorderWidthRight(0);
        cb = new PdfPCell(new Phrase(onePer.format((nutrients[401] / servings) / 60.0), LABEL_NORMAL));
        cb.setColspan(10);
        cb.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cb.setBorderWidthLeft(0);
        label.addCell(ca);
        label.addCell(cb);
        //Calcium
        ca = new PdfPCell(new Phrase("Calcium", LABEL_NORMAL));
        ca.setColspan(6);
        ca.setBorderWidthRight(0);
        cb = new PdfPCell(new Phrase(onePer.format((nutrients[301] / servings) / 1100.0), LABEL_NORMAL));
        cb.setColspan(10);
        cb.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cb.setBorderWidthLeft(0);
        label.addCell(ca);
        label.addCell(cb);
        //Iron
        ca = new PdfPCell(new Phrase("Iron", LABEL_NORMAL));
        ca.setColspan(6);
        ca.setBorderWidthRight(0);
        cb = new PdfPCell(new Phrase(onePer.format((nutrients[303] / servings) / 14.0), LABEL_NORMAL));
        cb.setColspan(10);
        cb.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cb.setBorderWidthLeft(0);
        label.addCell(ca);
        label.addCell(cb);
        //Add to the table
        label.setHorizontalAlignment(Element.ALIGN_LEFT);
        p.add(label);
        //add to the document
        doc.add(p);

    } catch (DocumentException ex) {
        System.out.println("Error: " + ex.toString());
    }
}

From source file:Integral.PDF.java

/**
 * Crea una objeto de tipo tabla/*from   w ww. j  a  v  a  2 s.  c  o  m*/
 * @param columnas numero de columnas
 * @param tamanio Tamao de las columnas
 * @param porcentaje proporcion de la tabla deacuerdo a la hoja.
 * @param alineacion alineacion de la tabla respecto a la hoja
 * @return 
 */
public PdfPTable crearTabla(int columnas, float tamanio[], int porcentaje, int alineacion) {
    try {
        PdfPTable table = new PdfPTable(columnas);
        table.setWidths(tamanio);
        table.setWidthPercentage(porcentaje);
        table.setHorizontalAlignment(alineacion);
        return table;
    } catch (DocumentException e) {
        System.out.println(e);
        return null;
    }
}

From source file:javaresturentdesktopclient.BillingPage.java

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    // TODO add your handling code here:

    try {//www . j  a  v  a 2 s .c  o  m
        Document doc = new Document(PageSize.A4.rotate());
        String Datee = Date;
        PdfWriter.getInstance(doc,
                new FileOutputStream("d:/BillingTable_" + transactionId + "_" + Datee + ".pdf"));

        PdfPTable pdfTable = new PdfPTable(jTableMealOrder.getColumnCount());
        //adding table headers
        for (int i = 0; i < jTableMealOrder.getColumnCount(); i++) {
            pdfTable.addCell(jTableMealOrder.getColumnName(i));
        }
        //extracting data from the JTable and inserting it to PdfPTable
        for (int rows = 0; rows < jTableMealOrder.getRowCount(); rows++) {
            for (int cols = 0; cols < jTableMealOrder.getColumnCount(); cols++) {
                pdfTable.addCell(jTableMealOrder.getModel().getValueAt(rows, cols).toString());

            }
        }

        doc.open();

        Paragraph preface = new Paragraph("Biliing Id :  " + transactionId
                + "                                                                                                                                  DATE : "
                + Date + "  TIME :" + Time + "\n\n\n\n");
        preface.setAlignment(Element.ALIGN_LEFT);
        doc.add(preface);
        pdfTable.setHorizontalAlignment(Element.ALIGN_CENTER);
        doc.add(pdfTable);

        Paragraph preface1 = new Paragraph("\n\n\n\n\tTotal:" + Total + "\n\n");
        preface1.setAlignment(Element.ALIGN_BASELINE);
        doc.add(preface1);
        doc.add(new Paragraph());

        doc.add(new Paragraph("\n\n\n\nTHANK YOU"));

        doc.close();
        System.out.println("done");
        JOptionPane.showMessageDialog(null, "Printed", "Information", JOptionPane.INFORMATION_MESSAGE);
    } catch (DocumentException ex) {
        Logger.getLogger(BillingPage.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(BillingPage.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:jdbreport.model.io.pdf.itext5.PdfWriter.java

License:Apache License

private PdfPTable createPdfTable(int columnCount, float[] widths, Rectangle pageSize) throws DocumentException {
    PdfPTable table;
    table = new PdfPTable(columnCount);
    table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
    table.setTotalWidth(widths);/*  www  . j av a2 s .  c  o  m*/
    table.setWidthPercentage(widths, pageSize);
    return table;
}

From source file:Login.ventas.fproyectos.java

/**
 * Creates new form cliente/*from   w  w  w. j  av  a 2 s  .c om*/
 * @param user
 */
public fproyectos(Login user) {
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    @SuppressWarnings("MismatchedReadAndWriteOfArray")
    String[] fecha = { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre",
            "Octubre", "Noviembre", "Diciembre" };
    initComponents();
    Calendar rightNow = Calendar.getInstance();
    int ccyy = rightNow.get(Calendar.YEAR);
    int month = rightNow.get(Calendar.MONTH);
    setSize(d.width, d.height - 95);
    this.usuario = user;
    add(f);
    f.setLocation(jPanel1.getX(), jPanel1.getY());
    add(f3);
    f3.setLocation(jPanel1.getX(), jPanel1.getY());
    add(f4);
    f4.setLocation(jPanel1.getX(), jPanel1.getY());
    add(f5);
    f5.setLocation(jPanel1.getX(), jPanel1.getY());
    add(f2);
    f2.setLocation(jPanel1.getX(), jPanel1.getY());

    f2.getjButton1().addMouseListener(new java.awt.event.MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            try {
                ServiceHb helper = new ServiceHb();
                helper.iniciarTransaccion();
                Fcomisiones fc = helper.getFcomisiones(idcliente, idinstalacion);
                if (helper.getFcomisiones(idcliente, idinstalacion) == null) {
                    fc = new Fcomisiones();
                    Calendar d = Calendar.getInstance();
                    fc.setFecha(new java.sql.Date(d.getTime().getTime()));
                    fc.setClientes(
                            (Clientes) helper.obtenerObjeto(Clientes.class, Integer.parseInt(idcliente)));
                    fc.setInstalacion((Instalacion) helper.obtenerObjeto(Instalacion.class,
                            Integer.parseInt(idinstalacion)));
                    fc.setRentabilidad(f2.getjLabel20().getText());
                    fc.setValor1(f2.getjLabel11().getText().substring(2, f2.getjLabel11().getText().length()));
                    fc.setValor2(f2.getjLabel2().getText().substring(3, f2.getjLabel2().getText().length()));
                    fc.setDiferido(f2.getjLabel4().getText());
                    fc.setDias(numeroinstala);
                    helper.crearObjeto(fc);
                    helper.confirmarTransaccion();
                    helper.cerrarSesion();
                    JOptionPane.showMessageDialog(null,
                            "Comision prepara con fecha " + Funcion.DateFormatSql(d.getTime()));
                } else {
                    Calendar d = Calendar.getInstance();
                    fc.setFecha(new java.sql.Date(d.getTime().getTime()));
                    fc.setRentabilidad(f2.getjLabel20().getText());
                    fc.setValor1(f2.getjLabel11().getText().substring(2, f2.getjLabel11().getText().length()));
                    fc.setValor2(f2.getjLabel2().getText().substring(3, f2.getjLabel2().getText().length()));
                    fc.setDiferido(f2.getjLabel4().getText());
                    fc.setDias(numeroinstala);
                    helper.actualizarObjeto(fc);
                    helper.confirmarTransaccion();
                    helper.cerrarSesion();
                    JOptionPane.showMessageDialog(null,
                            "Actualizado / Comision prepara con fecha " + Funcion.DateFormatSql(d.getTime()));
                }
            } catch (Exception io) {
                System.out.println(io);
            }
        }
    });
    f2.getjButton2().addMouseListener(new java.awt.event.MouseAdapter() {
        @Override
        @SuppressWarnings("UseSpecificCatch")
        public void mouseClicked(MouseEvent e) {
            float ancho = 0;
            try {
                Document documento = new Document(PageSize.A4, 0, 0, 0, 0);
                ancho = documento.getPageSize().getWidth() - 100;
                FileOutputStream ficheroPdf;
                PdfWriter writer = null;
                PdfWriter writer2 = null;
                String direccion = "";
                Calendar now2 = Calendar.getInstance();
                try {
                    JFileChooser chooser = new JFileChooser();
                    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    //Mostrar la ventana para abrir archivo y recoger la respuesta
                    //En el parmetro del showOpenDialog se indica la ventana
                    //  al que estar asociado. Con el valor this se asocia a la
                    //  ventana que la abre.
                    int respuesta = chooser.showOpenDialog(null);
                    String cadena = "";
                    if (respuesta == JFileChooser.APPROVE_OPTION) {
                        direccion = chooser.getSelectedFile().getAbsolutePath();
                    }
                    Calendar d = Calendar.getInstance();
                    ficheroPdf = new FileOutputStream(direccion + "/" + idcliente + idinstalacion + ".pdf");
                    writer = PdfWriter.getInstance(documento, ficheroPdf);
                } catch (IOException ex) {
                    System.out.println(ex.toString());
                }
                ServiceHb helper = null;
                try {
                    List<Productos> lt = null;
                    helper = new ServiceHb();
                    helper.iniciarTransaccion();
                    PdfPTable tabla;
                    PdfPCell casilla;
                    PdfPTable salto = null;
                    PdfPCell celda;
                    Font fontpersonalizado = FontFactory.getFont("ARIAL", 7, Font.BOLD);
                    Font fontpersonalizado2 = FontFactory.getFont("ARIAL", 7, Font.NORMAL);
                    documento.open();
                    PdfContentByte canvas = writer.getDirectContent();
                    fondos(documento, canvas);
                    //Materiales
                    double total = 0;
                    double total2 = 0;
                    double total3 = 0;
                    double total4 = 0;
                    double total5 = 0;
                    tabla = new PdfPTable(6);
                    tabla.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                    tabla.setTotalWidth(ancho);
                    tabla.setLockedWidth(true);
                    celda = new PdfPCell(new Paragraph("ADQUISICION DE MATERIALES"));
                    celda.setColspan(6);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda.setBorder(PdfPCell.NO_BORDER);
                    tabla.addCell(celda);
                    float pagina = documento.getPageSize().getHeight() - 140;
                    final float max = pagina;
                    pagina = pagina - tabla.getRow(0).calculateHeights();
                    if (ltart != null) {
                        Object nuevo2[] = { "FECHA", "DETALLE", "CANT.", "PRECIO", "SUBTOTAL", "TOTAL DIARIO" };
                        for (Object obj : nuevo2) {
                            celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado)));
                            celda.setBorderColor(new Color(195, 195, 195));
                            celda.setColspan(1);
                            celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                            tabla.addCell(celda);
                        }
                        pagina = pagina - tabla.getRow(1).calculateHeights();
                        int tam = 0;
                        double diario = 0;
                        int turno = 0;
                        for (int i = 0; i < ltart.size(); i++) {
                            turno = 0;
                            fact = helper.getFacturafecha(ltart.get(i).getIdfactura());
                            String fecha = "";
                            if (fact != null) {
                                fecha = Funcion.DateFormatSql(fact.getFecha());
                            }
                            if ((i + 1) == ltart.size()) {
                                diario = diario + Double.parseDouble(ltart.get(i).getTotal());
                                turno = 1;
                            } else {
                                fact2 = helper.getFacturafecha(ltart.get(i + 1).getIdfactura());
                                String fecha2 = "";
                                if (fact2 != null) {
                                    fecha2 = Funcion.DateFormatSql(fact2.getFecha());
                                    if (fecha.equalsIgnoreCase(fecha2)) {
                                        diario = diario + Double.parseDouble(ltart.get(i).getTotal());
                                    } else {
                                        diario = diario + Double.parseDouble(ltart.get(i).getTotal());
                                        turno = 1;
                                    }
                                }
                            }
                            String uni = "$ " + df.format(Double.parseDouble(ltart.get(i).getUnitario()))
                                    .replace(",", ".");
                            String tot = "$ "
                                    + df.format(Double.parseDouble(ltart.get(i).getTotal())).replace(",", ".");
                            total = total + Double.parseDouble(ltart.get(i).getTotal());
                            if (turno == 0) {
                                Object nuevo[] = { fecha, ltart.get(i).getDescripcion(),
                                        ltart.get(i).getCantidad(), uni, tot, "" };
                                for (Object obj : nuevo) {

                                    celda = new PdfPCell(
                                            new Paragraph((String) obj, new Font(fontpersonalizado2)));
                                    if (((String) obj).equalsIgnoreCase("")) {
                                        celda.setBorder(Rectangle.RIGHT);
                                    }
                                    celda.setBorderColor(new Color(195, 195, 195));
                                    celda.setColspan(1);
                                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                                    tabla.addCell(celda);
                                }
                            } else {
                                Object nuevo[] = { fecha, ltart.get(i).getDescripcion(),
                                        ltart.get(i).getCantidad(), uni, tot,
                                        "$ " + df.format(diario).replace(",", ".") };
                                int va = 0;
                                for (Object obj : nuevo) {
                                    va++;
                                    celda = new PdfPCell(
                                            new Paragraph((String) obj, new Font(fontpersonalizado2)));
                                    if (va == 5) {
                                        celda.setBorder(Rectangle.BOTTOM);
                                    }
                                    celda.setColspan(1);
                                    celda.setBorderColor(new Color(195, 195, 195));
                                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                                    tabla.addCell(celda);
                                }
                                diario = 0;
                            }
                            pagina = pagina - tabla.getRow(tam).getMaxHeights();
                            tam++;
                            if (pagina < 11) {
                                salto = new PdfPTable(1);
                                casilla = new PdfPCell(new Phrase("", new Font(fontpersonalizado)));
                                casilla.setMinimumHeight(90);
                                casilla.setBorder(PdfPCell.NO_BORDER);
                                salto.addCell(casilla);
                                documento.add(salto);

                                documento.add(tabla);
                                documento.newPage();
                                fondos(documento, canvas);
                                tabla = new PdfPTable(6);
                                tabla.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                                tabla.setTotalWidth(ancho);
                                tabla.setLockedWidth(true);
                                pagina = max;
                                tam = 0;
                            }

                        }
                    }
                    salto = new PdfPTable(1);
                    casilla = new PdfPCell(new Phrase("", new Font(fontpersonalizado)));
                    casilla.setMinimumHeight(90);
                    casilla.setBorder(PdfPCell.NO_BORDER);
                    salto.addCell(casilla);
                    documento.add(salto);

                    celda = new PdfPCell(new Paragraph("Total", new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    celda = new PdfPCell(
                            new Paragraph("Adquisicin de Materiales", new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(4);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    celda = new PdfPCell(new Paragraph("$ " + df.format(total).replace(",", "."),
                            new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    documento.add(tabla);
                    documento.newPage();
                    fondos(documento, canvas);
                    /*Parte 2*/

                    tabla = new PdfPTable(4);
                    tabla.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                    tabla.setTotalWidth(ancho);
                    tabla.setLockedWidth(true);
                    tabla.setWidths(new int[] { 1, 3, 1, 1 });
                    celda = new PdfPCell(new Paragraph("MANO DE OBRA TECNICOS"));
                    celda.setColspan(4);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda.setBorder(PdfPCell.NO_BORDER);
                    tabla.addCell(celda);
                    pagina = documento.getPageSize().getHeight() - 140;
                    pagina = pagina - tabla.getRow(0).calculateHeights();

                    if (list != null) {
                        Object nuevo2[] = { "FECHA", "DETALLE", "CANT.", "SUBTOTAL" };
                        for (Object obj : nuevo2) {
                            celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado)));
                            celda.setBorderColor(new Color(195, 195, 195));
                            celda.setColspan(1);
                            celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                            tabla.addCell(celda);
                        }
                        pagina = pagina - tabla.getRow(1).calculateHeights();
                        int tam = 0;
                        double diario = 0;
                        int turno = 0;
                        for (Ctepagar list1 : list) {
                            if (list1.getDetalle().contains("ci:")) {
                                String fecha = list1.getFecha();
                                String desc = list1.getDetalle();
                                String[] cadena = desc.split(" ");
                                String result = "";
                                int contador = 0;
                                for (String n : cadena) {
                                    if (n.contains("ci:")) {
                                        result = result + ", ";
                                        contador++;
                                    } else {
                                        result = result + n + " ";
                                    }
                                }
                                result = result.substring(0, result.length() - 2);
                                String uni = "$ "
                                        + df.format(Double.parseDouble(list1.getValor())).replace(",", ".");
                                String tot = "$ "
                                        + df.format(Double.parseDouble(list1.getValor())).replace(",", ".");
                                total2 = total2 + Double.parseDouble(list1.getValor());
                                Object nuevo[] = { fecha, result, "" + contador, tot };
                                for (Object obj : nuevo) {
                                    celda = new PdfPCell(
                                            new Paragraph((String) obj, new Font(fontpersonalizado2)));
                                    celda.setBorderColor(new Color(195, 195, 195));
                                    celda.setColspan(1);
                                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                                    tabla.addCell(celda);
                                }
                                pagina = pagina - tabla.getRow(tam).getMaxHeights();
                                tam++;
                                if (pagina < 90) {
                                    salto = new PdfPTable(1);
                                    casilla = new PdfPCell(new Phrase("", new Font(fontpersonalizado2)));
                                    casilla.setMinimumHeight(90);
                                    casilla.setBorder(PdfPCell.NO_BORDER);
                                    salto.addCell(casilla);
                                    documento.add(salto);

                                    documento.add(tabla);
                                    documento.newPage();
                                    fondos(documento, canvas);
                                    tabla = new PdfPTable(4);
                                    tabla.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                                    tabla.setTotalWidth(ancho);
                                    tabla.setLockedWidth(true);
                                    tabla.setWidths(new int[] { 1, 3, 1, 1 });
                                    pagina = max;
                                    tam = 0;
                                }
                            }
                        }
                    }
                    salto = new PdfPTable(1);
                    casilla = new PdfPCell(new Phrase("", new Font(fontpersonalizado)));
                    casilla.setMinimumHeight(90);
                    casilla.setBorder(PdfPCell.NO_BORDER);
                    salto.addCell(casilla);
                    documento.add(salto);

                    celda = new PdfPCell(new Paragraph("Total", new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    celda = new PdfPCell(new Paragraph("Mano de Obra Tcnicos", new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(2);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    celda = new PdfPCell(new Paragraph("$ " + df.format(total2).replace(",", "."),
                            new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    documento.add(tabla);

                    documento.newPage();
                    fondos(documento, canvas);

                    tabla = new PdfPTable(3);
                    tabla.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                    tabla.setTotalWidth(ancho);
                    tabla.setLockedWidth(true);
                    tabla.setWidths(new int[] { 1, 3, 1 });
                    celda = new PdfPCell(new Paragraph("TRANSPORTE"));
                    celda.setColspan(3);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda.setBorder(PdfPCell.NO_BORDER);
                    tabla.addCell(celda);
                    pagina = documento.getPageSize().getHeight() - 140;
                    pagina = pagina - tabla.getRow(0).calculateHeights();

                    if (list != null) {
                        Object nuevo2[] = { "FECHA", "DETALLE", "SUBTOTAL" };
                        for (Object obj : nuevo2) {
                            celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado)));
                            celda.setBorderColor(new Color(195, 195, 195));
                            celda.setColspan(1);
                            celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                            tabla.addCell(celda);
                        }
                        pagina = pagina - tabla.getRow(1).calculateHeights();
                        int tam = 0;
                        for (Ctepagar list1 : list) {
                            if (list1.getDetalle().contains("Transporte")) {
                                String fecha = list1.getFecha();
                                String desc = list1.getDetalle();
                                String result = "";
                                String uni = "$ "
                                        + df.format(Double.parseDouble(list1.getValor())).replace(",", ".");
                                String tot = "$ "
                                        + df.format(Double.parseDouble(list1.getValor())).replace(",", ".");
                                total3 = total3 + Double.parseDouble(list1.getValor());
                                Object nuevo[] = { fecha, desc, tot };
                                for (Object obj : nuevo) {
                                    celda = new PdfPCell(
                                            new Paragraph((String) obj, new Font(fontpersonalizado2)));
                                    celda.setBorderColor(new Color(195, 195, 195));
                                    celda.setColspan(1);
                                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                                    tabla.addCell(celda);
                                }
                                pagina = pagina - tabla.getRow(tam).getMaxHeights();
                                tam++;
                                if (pagina < 50) {
                                    salto = new PdfPTable(1);
                                    casilla = new PdfPCell(new Phrase("", new Font(fontpersonalizado2)));
                                    casilla.setMinimumHeight(90);
                                    casilla.setBorder(PdfPCell.NO_BORDER);
                                    salto.addCell(casilla);
                                    documento.add(salto);

                                    documento.add(tabla);
                                    documento.newPage();
                                    fondos(documento, canvas);
                                    tabla = new PdfPTable(3);
                                    tabla.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                                    tabla.setTotalWidth(ancho);
                                    tabla.setLockedWidth(true);
                                    tabla.setWidths(new int[] { 1, 3, 1 });
                                    pagina = max;
                                    tam = 0;
                                }
                            }
                        }
                    }
                    salto = new PdfPTable(1);
                    casilla = new PdfPCell(new Phrase("", new Font(fontpersonalizado)));
                    casilla.setMinimumHeight(90);
                    casilla.setBorder(PdfPCell.NO_BORDER);
                    salto.addCell(casilla);
                    documento.add(salto);

                    celda = new PdfPCell(new Paragraph("Total", new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    celda = new PdfPCell(new Paragraph("Transporte", new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    celda = new PdfPCell(new Paragraph("$ " + df.format(total3).replace(",", "."),
                            new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    documento.add(tabla);

                    documento.newPage();
                    fondos(documento, canvas);

                    tabla = new PdfPTable(3);
                    tabla.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                    tabla.setTotalWidth(ancho);
                    tabla.setLockedWidth(true);
                    tabla.setWidths(new int[] { 1, 3, 1 });
                    celda = new PdfPCell(new Paragraph("COMBUSTIBLE"));
                    celda.setColspan(3);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda.setBorder(PdfPCell.NO_BORDER);
                    tabla.addCell(celda);
                    pagina = documento.getPageSize().getHeight() - 140;
                    pagina = pagina - tabla.getRow(0).calculateHeights();

                    if (list != null) {
                        Object nuevo2[] = { "FECHA", "DETALLE", "SUBTOTAL" };
                        for (Object obj : nuevo2) {
                            celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado)));
                            celda.setBorderColor(new Color(195, 195, 195));
                            celda.setColspan(1);
                            celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                            tabla.addCell(celda);
                        }
                        pagina = pagina - tabla.getRow(1).calculateHeights();
                        int tam = 0;
                        for (Ctepagar list1 : list) {
                            if (list1.getDetalle().contains("Combustible")) {
                                String fecha = list1.getFecha();
                                String desc = list1.getDetalle();
                                String result = "";
                                String uni = "$ "
                                        + df.format(Double.parseDouble(list1.getValor())).replace(",", ".");
                                String tot = "$ "
                                        + df.format(Double.parseDouble(list1.getValor())).replace(",", ".");
                                total4 = total4 + Double.parseDouble(list1.getValor());
                                Object nuevo[] = { fecha, desc, tot };
                                for (Object obj : nuevo) {
                                    celda = new PdfPCell(
                                            new Paragraph((String) obj, new Font(fontpersonalizado2)));
                                    celda.setBorderColor(new Color(195, 195, 195));
                                    celda.setColspan(1);
                                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                                    tabla.addCell(celda);
                                }
                                pagina = pagina - tabla.getRow(tam).getMaxHeights();
                                tam++;
                                if (pagina < 50) {
                                    salto = new PdfPTable(1);
                                    casilla = new PdfPCell(new Phrase("", new Font(fontpersonalizado)));
                                    casilla.setMinimumHeight(90);
                                    casilla.setBorder(PdfPCell.NO_BORDER);
                                    salto.addCell(casilla);
                                    documento.add(salto);

                                    documento.add(tabla);
                                    documento.newPage();
                                    fondos(documento, canvas);
                                    tabla = new PdfPTable(3);
                                    tabla.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                                    tabla.setTotalWidth(ancho);
                                    tabla.setLockedWidth(true);
                                    tabla.setWidths(new int[] { 1, 3, 1 });
                                    pagina = max;
                                    tam = 0;
                                }
                            }
                        }
                    }
                    salto = new PdfPTable(1);
                    casilla = new PdfPCell(new Phrase("", new Font(fontpersonalizado)));
                    casilla.setMinimumHeight(90);
                    casilla.setBorder(PdfPCell.NO_BORDER);
                    salto.addCell(casilla);
                    documento.add(salto);

                    celda = new PdfPCell(new Paragraph("Total", new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    celda = new PdfPCell(new Paragraph("Combustible", new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    celda = new PdfPCell(new Paragraph("$ " + df.format(total4).replace(",", "."),
                            new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    documento.add(tabla);

                    documento.newPage();
                    fondos(documento, canvas);

                    //Extras

                    tabla = new PdfPTable(3);
                    tabla.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                    tabla.setTotalWidth(ancho);
                    tabla.setLockedWidth(true);
                    tabla.setWidths(new int[] { 1, 3, 1 });
                    celda = new PdfPCell(new Paragraph("EXTRAS"));
                    celda.setColspan(3);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda.setBorder(PdfPCell.NO_BORDER);
                    tabla.addCell(celda);
                    pagina = documento.getPageSize().getHeight() - 140;
                    pagina = pagina - tabla.getRow(0).calculateHeights();

                    if (list != null) {
                        Object nuevo2[] = { "FECHA", "DETALLE", "SUBTOTAL" };
                        for (Object obj : nuevo2) {
                            celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado)));
                            celda.setBorderColor(new Color(195, 195, 195));
                            celda.setColspan(1);
                            celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                            tabla.addCell(celda);
                        }
                        pagina = pagina - tabla.getRow(1).calculateHeights();
                        int tam = 0;
                        for (Ctepagar list1 : list) {
                            if (!list1.getDetalle().contains("Combustible")
                                    && !list1.getDetalle().contains("ci:")
                                    && !list1.getDetalle().contains("Transporte")) {
                                String fecha = list1.getFecha();
                                String desc = list1.getDetalle();
                                String result = "";
                                String uni = "$ "
                                        + df.format(Double.parseDouble(list1.getValor())).replace(",", ".");
                                String tot = "$ "
                                        + df.format(Double.parseDouble(list1.getValor())).replace(",", ".");
                                total5 = total5 + Double.parseDouble(list1.getValor());
                                Object nuevo[] = { fecha, desc, tot };
                                for (Object obj : nuevo) {
                                    celda = new PdfPCell(
                                            new Paragraph((String) obj, new Font(fontpersonalizado2)));
                                    celda.setBorderColor(new Color(195, 195, 195));
                                    celda.setColspan(1);
                                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                                    tabla.addCell(celda);
                                }
                                pagina = pagina - tabla.getRow(tam).getMaxHeights();
                                tam++;
                                if (pagina < 50) {
                                    salto = new PdfPTable(1);
                                    casilla = new PdfPCell(new Phrase("", new Font(fontpersonalizado)));
                                    casilla.setMinimumHeight(90);
                                    casilla.setBorder(PdfPCell.NO_BORDER);
                                    salto.addCell(casilla);
                                    documento.add(salto);

                                    documento.add(tabla);
                                    documento.newPage();
                                    fondos(documento, canvas);
                                    tabla = new PdfPTable(3);
                                    tabla.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                                    tabla.setTotalWidth(ancho);
                                    tabla.setLockedWidth(true);
                                    tabla.setWidths(new int[] { 1, 3, 1 });
                                    pagina = max;
                                    tam = 0;
                                }
                            }
                        }
                    }
                    salto = new PdfPTable(1);
                    casilla = new PdfPCell(new Phrase("", new Font(fontpersonalizado)));
                    casilla.setMinimumHeight(90);
                    casilla.setBorder(PdfPCell.NO_BORDER);
                    salto.addCell(casilla);
                    documento.add(salto);

                    celda = new PdfPCell(new Paragraph("Total", new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    celda = new PdfPCell(new Paragraph("Extra", new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    celda = new PdfPCell(new Paragraph("$ " + df.format(total5).replace(",", "."),
                            new Font(fontpersonalizado2)));
                    celda.setBorderColor(new Color(195, 195, 195));
                    celda.setColspan(1);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);

                    documento.add(tabla);

                    documento.newPage();
                    fondos(documento, canvas);

                    //Final Vendedor

                    tabla = new PdfPTable(3);
                    tabla.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                    tabla.setTotalWidth(ancho);
                    tabla.setLockedWidth(true);
                    tabla.setWidths(new int[] { 1, 3, 1 });
                    celda = new PdfPCell(new Paragraph(nombrecliente));
                    celda.setColspan(3);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda.setBorder(PdfPCell.NO_BORDER);
                    tabla.addCell(celda);

                    celda = new PdfPCell(new Paragraph("Venta"));
                    celda.setColspan(3);
                    celda.setHorizontalAlignment(Element.ALIGN_LEFT);
                    celda.setBorder(PdfPCell.NO_BORDER);
                    tabla.addCell(celda);

                    pagina = documento.getPageSize().getHeight() - 140;
                    pagina = pagina - tabla.getRow(0).calculateHeights();

                    Object nuevo2[] = { "FECHA", "DETALLE", "TOTAL" };
                    for (Object obj : nuevo2) {
                        celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado)));
                        celda.setBorderColor(new Color(195, 195, 195));
                        celda.setColspan(1);
                        celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                        tabla.addCell(celda);
                    }
                    Object nuevo3[] = { "", nombreinstalacion, totalinstalacion };
                    for (Object obj : nuevo3) {
                        celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado2)));
                        celda.setBorderColor(new Color(195, 195, 195));
                        celda.setColspan(1);
                        celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                        tabla.addCell(celda);
                    }
                    celda = new PdfPCell(new Paragraph("INVERSION", new Font(fontpersonalizado)));
                    celda.setBorder(PdfPCell.NO_BORDER);
                    celda.setColspan(3);
                    celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tabla.addCell(celda);
                    Object nuevo4[] = { "Uso de Materiales", "", "$ " + df.format(total).replace(",", ".") };
                    for (Object obj : nuevo4) {
                        celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado2)));
                        celda.setBorder(PdfPCell.NO_BORDER);
                        celda.setColspan(1);
                        celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                        tabla.addCell(celda);
                    }
                    Object nuevo5[] = { "Mano de obra tcnicos", "",
                            "$ " + df.format(total2).replace(",", ".") };
                    for (Object obj : nuevo5) {
                        celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado2)));
                        celda.setBorder(PdfPCell.NO_BORDER);
                        celda.setColspan(1);
                        celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                        tabla.addCell(celda);
                    }
                    Object nuevo6[] = { "Transporte", "", "$ " + df.format(total3).replace(",", ".") };
                    for (Object obj : nuevo6) {
                        celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado2)));
                        celda.setBorder(PdfPCell.NO_BORDER);
                        celda.setColspan(1);
                        celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                        tabla.addCell(celda);
                    }
                    Object nuevo7[] = { "Combustible", "", "$ " + df.format(total4).replace(",", ".") };
                    for (Object obj : nuevo7) {
                        celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado2)));
                        celda.setBorder(PdfPCell.NO_BORDER);
                        celda.setColspan(1);
                        celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                        tabla.addCell(celda);
                    }
                    Object nuevo71[] = { "Extras", "", "$ " + df.format(total5).replace(",", ".") };
                    for (Object obj : nuevo71) {
                        celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado2)));
                        celda.setBorder(PdfPCell.NO_BORDER);
                        celda.setColspan(1);
                        celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                        tabla.addCell(celda);
                    }
                    Object nuevo8[] = { "Total Inversin", "",
                            "$ " + df.format(total + total2 + total3 + total4).replace(",", ".") };
                    for (Object obj : nuevo8) {
                        celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado2)));
                        celda.setBorder(PdfPCell.NO_BORDER);
                        celda.setColspan(1);
                        celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                        tabla.addCell(celda);
                    }
                    celda = new PdfPCell(new Paragraph(""));
                    celda.setColspan(3);
                    celda.setHorizontalAlignment(Element.ALIGN_LEFT);
                    celda.setBorder(PdfPCell.NO_BORDER);
                    tabla.addCell(celda);

                    Object nuevo9[] = { "Utilidad del proyecto", "", "$ "
                            + df.format(proyectoval - total - total2 - total3 - total4).replace(",", ".") };
                    for (Object obj : nuevo9) {
                        celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado)));
                        celda.setColspan(1);
                        celda.setBorder(PdfPCell.NO_BORDER);
                        celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                        tabla.addCell(celda);
                    }
                    Object nuevo10[] = { "Rentabilidad ", "", df.format(porcent).replace(",", ".") + " %" };
                    for (Object obj : nuevo10) {
                        celda = new PdfPCell(new Paragraph((String) obj, new Font(fontpersonalizado)));
                        celda.setBorder(PdfPCell.NO_BORDER);
                        celda.setColspan(1);
                        celda.setHorizontalAlignment(Element.ALIGN_CENTER);
                        tabla.addCell(celda);
                    }

                    salto = new PdfPTable(1);
                    casilla = new PdfPCell(new Phrase("", new Font(fontpersonalizado)));
                    casilla.setMinimumHeight(90);
                    casilla.setBorder(PdfPCell.NO_BORDER);
                    salto.addCell(casilla);
                    documento.add(salto);

                    documento.add(tabla);

                    /*Fin Parte 3*/
                    documento.close();
                    helper.cerrarSesion();
                } catch (Exception ex) {
                    documento.close();
                }
                JOptionPane.showMessageDialog(null, "PDF Generado");
            } catch (Exception io) {
                System.out.println(io);
            }
        }
    });
    f.getjTable1().addMouseListener(new java.awt.event.MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            int row = f.getjTable1().getSelectedRow();
            int col = f.getjTable1().getSelectedColumn();
            if (col == 4) {
                f2.setVisible(true);
                f.setVisible(false);
                idcliente = f.getjTable1().getValueAt(row, 5).toString();
                idinstalacion = f.getjTable1().getValueAt(row, 6).toString();
                f2.getjLabel15().setText(f.getjTable1().getValueAt(row, 0).toString());
                f2.getjLabel8().setText(f.getjTable1().getValueAt(row, 2).toString());
                nombrecliente = f.getjTable1().getValueAt(row, 0).toString();
                nombreinstalacion = f.getjTable1().getValueAt(row, 2).toString();
                updateTablas up = new updateTablas(1);
                up.start();
            }
        }
    });
    f5.getjComboBox1().addActionListener(new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            DefaultTableModel temp2 = (DefaultTableModel) f5.getjTable2().getModel();
            for (int i = temp2.getRowCount() - 1; i >= 0; i--) {
                temp2.removeRow(i);
            }
            f5.setComi(null);
            f5.setNinstalacion("");
            f5.setNcliente("");
            f5.setList(null);
            f5.setId("");
            updateTablas up = new updateTablas(4);
            up.start();
        }
    });
    f3.getjTable1().addMouseListener(new java.awt.event.MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            int row = f3.getjTable1().getSelectedRow();
            int col = f3.getjTable1().getSelectedColumn();
            if (col == 6) {
                visible(f4);
                fcomisiones = f3.getjTable1().getValueAt(row, 6).toString();
                try {
                    ServiceHb helper = new ServiceHb();
                    helper.iniciarTransaccion();
                    Fcomisiones ft = (Fcomisiones) helper.obtenerObjeto(Fcomisiones.class,
                            Integer.parseInt(fcomisiones));
                    numeroinstala = ft.getDias();
                    f4.setIdcliente("" + ft.getClientes().getId());
                    f4.setIdinstalacion("" + ft.getInstalacion().getId());
                    f4.getDiasint().setText(numeroinstala);
                    helper.cerrarSesion();
                } catch (Exception io) {

                }
                f4.getjLabel1().setText(fcomisiones);
                f4.getC1().setText(f3.getjTable1().getValueAt(row, 0).toString());
                f4.getC2().setText(f3.getjTable1().getValueAt(row, 1).toString());
                double campo1 = Double
                        .parseDouble(f3.getjTable1().getValueAt(row, 3).toString().replace("$ ", ""));
                double campo2 = Double
                        .parseDouble(f3.getjTable1().getValueAt(row, 4).toString().replace("-$ ", ""));
                f4.getC3().setText(f3.getjTable1().getValueAt(row, 3).toString());
                f4.getC4().setText(f3.getjTable1().getValueAt(row, 4).toString());
                f4.getC5().setText(f3.getjTable1().getValueAt(row, 5).toString());
                f4.getC6().setText("$ " + df.format((campo1 - campo2)).replace(",", "."));

                updateTablas up = new updateTablas(3);
                up.start();
            }
        }
    });

    setVisible(false);
}

From source file:Measurment_Result_Record.MeasurmentSetup.java

/**
 *  write the given table datas to the given document
 * @param doc  document to write the result tables on
 * @param table  the result table //from  w  w w.j a va2  s  . c  om
 * @param m_title   the measurment title
 */
public static void addResultTablesTotheDocument(Document doc, JTable table, String m_title)
        throws DocumentException {
    //add the measurment title and table
    Paragraph measurmenttitle = new Paragraph(m_title + "\n ");
    measurmenttitle.getFont().setStyle(com.itextpdf.text.Font.BOLD);
    doc.add(measurmenttitle);

    int colmWidth = table.getColumnCount();
    int rowCount = table.getModel().getRowCount();
    PdfPTable resultTable = new PdfPTable(colmWidth);
    resultTable.setHorizontalAlignment(Element.ALIGN_LEFT);

    //add the table headers
    for (int i = 0; i < colmWidth; i++) {
        String headertext = table.getModel().getColumnName(i);
        PdfPCell cell = new PdfPCell(new Paragraph(headertext));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        resultTable.addCell(cell);
    }

    //add the table body

    for (int r = 0; r < rowCount; r++) {
        for (int c = 0; c < colmWidth; c++) {
            String data = table.getValueAt(r, c).toString();
            resultTable.addCell(data);
        }
    }

    resultTable.setSpacingAfter(10);
    //add the table to the document
    doc.add(resultTable);

}