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

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

Introduction

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

Prototype

public void setWidthPercentage(final float widthPercentage) 

Source Link

Document

Sets the width percentage that the table will occupy in the page.

Usage

From source file:Functions.pdf_Export.java

private void createProductTable(Document doc, Integer soHoaDon) {
    try {/* w  ww  .  ja va 2 s  . co m*/
        PdfPTable table = new PdfPTable(new float[] { 10, 35, 15, 20, 20 });
        table.setWidthPercentage(100);
        String[] colName = { "No.", "Product Name", "Quantity", "Price (VND)", "Amount" };
        // table header
        for (int i = 0; i < colName.length; i++) {
            PdfPCell cell = new PdfPCell(new Phrase(colName[i], subFont));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
        }

        List<ChiTietHoaDon> listCTHD = new ChiTietHoaDonDAO().getListChiTietHoaDon(soHoaDon);
        Integer productAmount = listCTHD.size();
        for (Integer i = 0; i < productAmount; i++) {
            String[] rowValue = getRowValue(i + 1, listCTHD);
            for (int j = 0; j < rowValue.length; j++) {
                PdfPCell cell = new PdfPCell(new Phrase(rowValue[j], smallFont));
                if (j == 1) {
                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                } else {
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                }
                table.addCell(cell);
            }
        }

        // add some blank rows
        for (Integer i = productAmount + 1; i < 11; i++) {
            for (int j = 0; j < 5; j++) {
                PdfPCell cell;
                if (j == 0) {
                    cell = new PdfPCell(new Phrase(i.toString(), smallFont));
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                } else {
                    cell = new PdfPCell(new Phrase(" "));
                }
                table.addCell(cell);
            }
        }

        doc.add(table);
        Paragraph p = new Paragraph();
        addEmptyLine(p, 1);
        doc.add(p);
    } catch (DocumentException ex) {
        Logger.getLogger(pdf_Export.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:fxml.test.PDFService.java

public PdfPTable createCourseInfo(Course course) {

    PdfPTable courseInfo = new PdfPTable(1);
    courseInfo.setWidthPercentage(100);

    courseInfo.addCell(getCellForString(course.getSemester(), Element.ALIGN_MIDDLE, false, 0,
            Element.ALIGN_CENTER, font7, false));

    PdfPCell cell2 = getCellForString(course.getCourseCode(), Element.ALIGN_MIDDLE, false, 0,
            Element.ALIGN_CENTER, font9, true);
    cell2.setPaddingBottom(2.5f);/*from  w w w .  ja  v  a 2  s. c o  m*/
    cell2.setPaddingTop(3.4f);
    courseInfo.addCell(cell2);
    courseInfo.addCell(getCellForString(String.format("%.02f", course.getCredit()), 0, false,
            Element.ALIGN_MIDDLE, Element.ALIGN_CENTER, font9, false));

    return courseInfo;

}

From source file:fxml.test.PDFService.java

public PdfPTable createTableHeader(int start) {

    PdfPTable table = null;/*from w  ww.ja  v  a2s .  c om*/

    try {

        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/*w w  w.  j a  v a 2 s .  co  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:gestionsessions.FenSession.java

private void jTableGestionSessionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableGestionSessionMouseClicked
    Document document = new Document();
    String req;/*from   w w  w. j  a v  a  2s  . c  om*/
    int nbLignes = 0;
    // Creation d'une PdfPTable avec 5 colonnes
    final PdfPTable table = new PdfPTable(5);
    try {
        PdfWriter.getInstance(document,
                new FileOutputStream(System.getProperty("user.dir") + "//listeEmargementSession.pdf"));
        document.open();
        //en tte du pdf
        Image logo = Image.getInstance("logo.png");
        logo.scaleAbsolute(120f, 67f);
        document.add(logo);
        //on indique la marge
        document.add(new Phrase("\n\n\nMarge dgage par la session: "
                + jTableGestionSession.getValueAt(jTableGestionSession.getSelectedRow(), 9).toString()
                + ""));
        document.add(new Phrase("\n\nListe d'margement de la session "));
        document.add(new Phrase(
                jTableGestionSession.getValueAt(jTableGestionSession.getSelectedRow(), 0).toString()));
        document.add(new Phrase(
                " / " + jTableGestionSession.getValueAt(jTableGestionSession.getSelectedRow(), 1).toString()));
        document.add(new Phrase(
                " / " + jTableGestionSession.getValueAt(jTableGestionSession.getSelectedRow(), 2).toString()));
        document.add(new Phrase(" / "
                + jTableGestionSession.getValueAt(jTableGestionSession.getSelectedRow(), 3).toString() + ":"));
        // Ajout d'un espace entre la PdfPTable et l'lment prcdent.
        table.setSpacingBefore(15f);
        //requte pour les personnes inscrites
        stmt1 = GestionBdd.connexionBdd(GestionBdd.TYPE_MYSQL, "formarmor", "localhost", "root", "");
        req = "Select c.* from client c, inscription i where c.id = i.client_id and i.session_formation_id = "
                + jTableGestionSession.getValueAt(jTableGestionSession.getSelectedRow(), 0).toString();
        ResultSet rs1 = GestionBdd.envoiRequeteLMD(stmt1, req);
        // Ajout d'une entte pour PdfPTable
        table.addCell("Matricule");
        table.addCell("Nom");
        table.addCell("Rue");
        table.addCell("Code Postal");
        table.addCell("Ville");
        // Ajout d'un espace entre la PdfPTable et l'lment prcdent.
        table.setSpacingBefore(15f);
        table.setWidthPercentage(100);
        table.setSpacingBefore(0f);
        table.setSpacingAfter(0f);
        //change taille colonnes
        table.setWidths(new int[] { 5, 10, 14, 6, 8 });
        while (rs1.next()) {
            // Ajout d'objets String  la PdfPTable
            table.addCell(rs1.getString(1));
            table.addCell(rs1.getString(3));
            table.addCell(rs1.getString(5));
            table.addCell(rs1.getString(6));
            table.addCell(rs1.getString(7));
            // Ajout d'un espace entre la PdfPTable et l'lment prcdent.
            table.setSpacingBefore(15f);
            nbLignes++;
        }
        //cas ou il n'y a personne d'inscrit
        if (nbLignes == 0) {
            JOptionPane.showMessageDialog(null, "Il n'y a personne d'inscrits");
            document.add(new Phrase("\n\nIl n'y a personne d'inscrits"));
        } else {
            document.add(table);
            //ouvre le fichier listeEmargementSession.pdf avec le logiciel de lecture pdf par dfaut 
            Runtime r = Runtime.getRuntime();
            r.exec(new String[] { "cmd.exe", "/C",
                    System.getProperty("user.dir") + "//listeEmargementSession.pdf" });
        }
        document.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException ex) {
        Logger.getLogger(FenSession.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(FenSession.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:gov.nih.nci.firebird.service.registration.AbstractPdfWriterGenerator.java

License:Open Source License

PdfPTable createTable(int numberOfColumns) {
    PdfPTable table = new PdfPTable(numberOfColumns);
    table.setWidthPercentage(ONE_HUNDRED_PERCENT);
    table.setSplitRows(false);/*from   ww  w .  j  a va 2s  . c  o m*/
    table.setSpacingAfter(TABLE_SPACING);
    table.setKeepTogether(true);
    return table;
}

From source file:gov.utah.dts.det.ccl.actions.reports.generators.ExpiredLicensesReport.java

private static PdfPTable getDocumentTable(Person specialist) throws DocumentException {
    PdfPTable table = new PdfPTable(1);
    // format the tables
    int headerwidths[] = { 100 }; // percentage
    table.setWidths(headerwidths); // percentage
    table.setWidthPercentage(100);
    table.getDefaultCell().setLeading(0, 0);
    table.getDefaultCell().setPadding(0);
    table.getDefaultCell().setBorderWidthLeft(0);
    table.getDefaultCell().setBorderWidthRight(0);
    table.getDefaultCell().setBorderWidthTop(0);
    table.getDefaultCell().setPaddingBottom(4);
    table.getDefaultCell().setBorderWidthBottom(1.0f);
    table.getDefaultCell().setVerticalAlignment(PdfPTable.ALIGN_TOP);
    table.getDefaultCell().setHorizontalAlignment(PdfPTable.ALIGN_CENTER);

    table.addCell(getHeaderTable(specialist));
    table.setHeaderRows(1);//from  w ww .j  av a  2s . c o  m

    return table;

}

From source file:gov.utah.dts.det.ccl.actions.reports.generators.ExpiredLicensesReport.java

private static PdfPTable getDetailTable() throws DocumentException {
    PdfPTable table = new PdfPTable(5);
    // format the table
    int headerwidths[] = { 41, 17, 8, 24, 10 }; // percentage
    table.setWidths(headerwidths); // percentage
    table.setWidthPercentage(100);
    table.getDefaultCell().setLeading(fixedLeading, 0);
    table.getDefaultCell().setPadding(0);
    table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
    table.getDefaultCell().setVerticalAlignment(PdfPTable.ALIGN_BOTTOM);
    table.getDefaultCell().setHorizontalAlignment(PdfPTable.ALIGN_LEFT);

    return table;
}

From source file:gov.utah.dts.det.ccl.actions.reports.generators.ExpiredLicensesReport.java

private static PdfPTable getFacilityDetailsTable() throws DocumentException {
    PdfPTable table = new PdfPTable(3);
    // format the table
    int headerwidths[] = { 33, 33, 33 }; // percentage
    table.setWidths(headerwidths); // percentage
    table.setWidthPercentage(100);
    table.getDefaultCell().setLeading(fixedLeading, 0);
    table.getDefaultCell().setPadding(0);
    table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
    table.getDefaultCell().setVerticalAlignment(PdfPTable.ALIGN_BOTTOM);
    table.getDefaultCell().setHorizontalAlignment(PdfPTable.ALIGN_LEFT);

    return table;
}

From source file:gov.utah.dts.det.ccl.actions.reports.generators.FacilityLicenseDetailReport.java

private static PdfPTable getDocumentTable(Person specialist, Date endDate) throws DocumentException {
    PdfPTable table = new PdfPTable(1);
    // format the tables
    int headerwidths[] = { 100 }; // percentage
    table.setWidths(headerwidths); // percentage
    table.setWidthPercentage(100);
    table.getDefaultCell().setLeading(0, 0);
    table.getDefaultCell().setPadding(0);
    table.getDefaultCell().setBorderWidthLeft(0);
    table.getDefaultCell().setBorderWidthRight(0);
    table.getDefaultCell().setBorderWidthTop(0);
    table.getDefaultCell().setPaddingBottom(4);
    table.getDefaultCell().setBorderWidthBottom(1.0f);
    table.getDefaultCell().setVerticalAlignment(PdfPTable.ALIGN_TOP);
    table.getDefaultCell().setHorizontalAlignment(PdfPTable.ALIGN_CENTER);

    table.addCell(getHeaderTable(specialist, endDate));
    table.setHeaderRows(1);/*ww  w.  j  a v a 2 s .  c  om*/

    return table;
}