Example usage for com.lowagie.text.pdf PdfPTable getDefaultCell

List of usage examples for com.lowagie.text.pdf PdfPTable getDefaultCell

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfPTable getDefaultCell.

Prototype

public PdfPCell getDefaultCell() 

Source Link

Document

Gets the default PdfPCell that will be used as reference for all the addCell methods except addCell(PdfPCell).

Usage

From source file:permit.InvoicePdf.java

License:Open Source License

/**
 * handles the letter header//from   w  w  w .jav a2 s  .com
 */
PdfPTable getHeader() {
    //
    String str = "";
    String spacer = "   ";
    PdfPTable headTable = null;
    try {
        //
        // for http url use
        //
        Image image = Image.getInstance(url + "js/images/city_logo3.jpg");
        Font fnt = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL);
        Font fntb = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
        float[] widths = { 25f, 40f, 35f }; // percentages
        headTable = new PdfPTable(widths);
        headTable.setWidthPercentage(100);
        headTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        headTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        image.scalePercent(15f);
        PdfPCell cell = new PdfPCell(image);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        headTable.addCell(cell);
        //
        float[] width = { 33f };
        PdfPTable midTable = new PdfPTable(width);
        midTable.setWidthPercentage(33);
        midTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        midTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        Phrase phrase = new Phrase();
        Chunk ch = new Chunk("City of Bloomington ", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        midTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("Planning and Transportation Department ", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        midTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("bloomington.in.gov", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        midTable.addCell(cell);
        //
        headTable.addCell(midTable);
        //
        PdfPTable rightTable = new PdfPTable(width);
        rightTable.setWidthPercentage(33);
        rightTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        rightTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        phrase = new Phrase();
        ch = new Chunk("401 N Morton St Suite 130 ", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("PO Box 100 \nBloomington, IN 47404", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("\n Phone: (812) 349-3423\nFax (812) 349-3520\nEmail: planning@bloomington.in.gov",
                fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        headTable.addCell(rightTable);
    } catch (Exception ex) {
        logger.error(ex);
    }
    return headTable;
}

From source file:permit.InvoicePdf.java

License:Open Source License

void writePages(HttpServletResponse res, Invoice invoice) {
    ////from   w  w w  .j a  va  2  s.  com
    // paper size legal (A4) 8.5 x 11
    // page 1-inch = 72 points
    //
    String fileName = "row_invoice_" + invoice.getInvoice_num() + ".pdf";
    Rectangle pageSize = new Rectangle(612, 792); // 8.5" X 11"
    Document document = new Document(pageSize, 36, 36, 18, 18);
    ServletOutputStream out = null;
    Font fnt = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL);
    Font fntb = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
    Font fnts = new Font(Font.TIMES_ROMAN, 8, Font.NORMAL);
    Font fntbs = new Font(Font.TIMES_ROMAN, 8, Font.BOLD);
    String spacer = "   ";
    PdfPTable header = getHeader();
    Company company = invoice.getCompany();
    Contact contact = null;
    if (invoice.hasContact()) {
        contact = invoice.getContact();
    }
    List<Page> pages = invoice.getPages();

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        String str = "";
        document.open();
        document.add(header);
        //
        // title
        float[] width = { 100f }; // one cell
        PdfPTable table = new PdfPTable(width);
        table.setWidthPercentage(100.0f);
        PdfPCell cell = new PdfPCell(new Phrase("INVOICE", fntb));
        //         
        cell.setBorder(Rectangle.BOTTOM);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        document.add(table);
        //
        // we need these later
        Paragraph pp = new Paragraph();
        Chunk ch = new Chunk(" ", fntb);
        Phrase phrase = new Phrase();
        //
        float[] widths = { 35f, 30f, 35f }; // percentages
        table = new PdfPTable(widths);
        table.setWidthPercentage(100.0f);
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        //
        // first row
        float[] widthOne = { 100f };
        PdfPTable leftTable = new PdfPTable(widthOne);
        leftTable.setWidthPercentage(35.0f);
        leftTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        //
        if (company != null) {
            ch = new Chunk("Company\n", fntb);
            phrase = new Phrase();
            phrase.add(ch);
            cell = new PdfPCell(phrase);
            cell.setBorder(Rectangle.NO_BORDER);
            leftTable.addCell(cell);
            phrase = new Phrase();
            ch = new Chunk(company.getName() + "\n", fnt);
            phrase.add(ch);
            cell = new PdfPCell(phrase);
            cell.setBorder(Rectangle.NO_BORDER);
            leftTable.addCell(cell);
        }
        if (contact != null) {
            phrase = new Phrase();
            ch = new Chunk(contact.getFullName(), fnt);
            phrase.add(ch);
            cell = new PdfPCell(phrase);
            cell.setBorder(Rectangle.NO_BORDER);
            leftTable.addCell(cell);
            phrase = new Phrase();
            ch = new Chunk(contact.getAddress(), fnt);
            phrase.add(ch);
            cell = new PdfPCell(phrase);
            cell.setBorder(Rectangle.NO_BORDER);
            leftTable.addCell(cell);
            ch = new Chunk(contact.getCityStateZip(), fnt);
            phrase = new Phrase();
            phrase.add(ch);
            cell = new PdfPCell(phrase);
            cell.setBorder(Rectangle.NO_BORDER);
            leftTable.addCell(cell);
        }
        table.addCell(leftTable);
        //
        // middle cell
        //
        cell = new PdfPCell(new Phrase(spacer, fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        //
        float[] widths2 = { 50f, 50f }; // percentages
        PdfPTable rightTable = new PdfPTable(widths2);
        rightTable.setWidthPercentage(35.0f);
        rightTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        //
        ch = new Chunk("Invoice No.", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk(invoice.getInvoice_num(), fnt);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk("Status", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk(invoice.getStatus(), fnt);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk("Invoice Date", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk(invoice.getDate(), fnt);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk("From ", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        ch = new Chunk(invoice.getStart_date(), fnt);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk("To ", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        ch = new Chunk(invoice.getEnd_date(), fnt);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        table.addCell(rightTable);
        //
        //
        document.add(table);
        //
        phrase = new Phrase(new Chunk(spacer, fnt));
        document.add(phrase);
        //
        int jj = 0;
        if (pages != null) {
            for (Page page : pages) {
                jj++;
                // float[] widthOne = {100f};
                PdfPTable borderTable = new PdfPTable(widthOne);
                borderTable.setWidthPercentage(100.0f);
                borderTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                float[] widthTwo = { 50f, 50f };
                PdfPTable titleTable = new PdfPTable(widthTwo);
                titleTable.setWidthPercentage(75.0f);
                titleTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                phrase = new Phrase("Invoice No. ", fntb);
                ch = new Chunk(invoice.getInvoice_num(), fnt);
                phrase.add(ch);
                cell = new PdfPCell(phrase);
                cell.setHorizontalAlignment(Rectangle.ALIGN_LEFT);
                cell.setBorder(Rectangle.NO_BORDER);
                titleTable.addCell(cell);
                //
                phrase = new Phrase(page.getPage_num(), fnt);
                cell = new PdfPCell(phrase);
                cell.setHorizontalAlignment(Rectangle.ALIGN_RIGHT);
                cell.setBorder(Rectangle.NO_BORDER);
                titleTable.addCell(cell);
                //
                borderTable.addCell(titleTable);
                float[] width4 = { 25f, 40f, 25f, 10f };
                PdfPTable contTable = new PdfPTable(width4);
                cell = new PdfPCell(new Phrase("Excavation Permit Number", fntb));
                contTable.addCell(cell);
                cell = new PdfPCell(new Phrase("Project", fntb));
                contTable.addCell(cell);
                cell = new PdfPCell(new Phrase("Date Issued", fntb));
                contTable.addCell(cell);
                cell = new PdfPCell(new Phrase("Permit Fee", fntb));
                contTable.addCell(cell);
                List<Permit> permits = page.getPermits();
                if (permits != null) {
                    for (Permit permit : permits) {
                        cell = new PdfPCell(new Phrase(permit.getPermit_num(), fnt));
                        contTable.addCell(cell);
                        phrase = new Phrase(permit.getProject() + "\n", fnt);
                        List<Excavation> cuts = permit.getExcavations();
                        if (cuts != null) {
                            for (Excavation one : cuts) {
                                ch = new Chunk(one.getAddress() + " (" + one.getCut_type() + ")", fnt);
                                phrase.add(ch);
                            }
                        }
                        cell = new PdfPCell(phrase);
                        contTable.addCell(cell);
                        cell = new PdfPCell(new Phrase(permit.getDate(), fnt));
                        contTable.addCell(cell);
                        cell = new PdfPCell(new Phrase("$" + permit.getFee(), fnt));
                        cell.setHorizontalAlignment(Rectangle.ALIGN_RIGHT);
                        contTable.addCell(cell);
                        cell = new PdfPCell(new Phrase(spacer, fnt));
                        //
                        // space line
                        cell.setColspan(4);
                        contTable.addCell(cell);
                    }
                }
                if (page.getNeededLines() > 0) { // first page 
                    for (int j = 0; j < page.getNeededLines(); j++) {
                        cell = new PdfPCell(new Phrase(spacer, fnt));
                        contTable.addCell(cell);
                        contTable.addCell(cell);
                        contTable.addCell(cell);
                        contTable.addCell(cell);
                    }
                }
                if (jj == pages.size()) {
                    cell = new PdfPCell(new Phrase("Total Invoice Amount\n" + invoice.getTotal(), fntb));
                    cell.setHorizontalAlignment(Rectangle.ALIGN_RIGHT);
                    cell.setColspan(4);
                    contTable.addCell(cell);
                }
                borderTable.addCell(contTable);
                cell = new PdfPCell(new Phrase(
                        "Payment due upon receipt. Please Make check payable to 'City of Bloomington'. Thank You.",
                        fnt));

                cell.setHorizontalAlignment(Rectangle.ALIGN_CENTER);
                cell.setBorder(Rectangle.NO_BORDER);
                borderTable.addCell(cell);
                borderTable.addCell(titleTable); // invoice and date
                document.add(borderTable);
                if (jj < pages.size()) {
                    document.newPage();
                }
            }
        }
        //
        document.close();
        writer.close();
        res.setHeader("Expires", "0");
        res.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        //
        // if you want for users to download, uncomment the following line
        //
        // res.setHeader("Content-Disposition","attachment; filename="+fileName);         
        res.setHeader("Pragma", "public");
        //
        // setting the content type
        res.setContentType("application/pdf");
        //
        // the contentlength is needed for MSIE!!!
        res.setContentLength(baos.size());
        //
        out = res.getOutputStream();
        if (out != null) {
            baos.writeTo(out);
        }
    } catch (Exception ex) {
        logger.error(ex);
    }

}

From source file:permit.PermitPdf.java

License:Open Source License

/**
 * handles the letter header//from  w  ww .  ja  v a2s  . c o m
 */
PdfPTable getHeader() {
    //
    String str = "";
    String spacer = "   ";
    //
    PdfPTable headTable = null;
    try {
        //
        // for http url use
        //
        Image image = Image.getInstance(url + "js/images/city_logo3.jpg");

        Font fnt = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL);
        Font fntb = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
        float[] widths = { 25f, 40f, 35f }; // percentages
        headTable = new PdfPTable(widths);
        headTable.setWidthPercentage(100);
        headTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        headTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        // image.setWidthPercentage(33.0f);
        image.scalePercent(15f);
        PdfPCell cell = new PdfPCell(image);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        headTable.addCell(cell);
        //
        float[] width = { 33f };
        PdfPTable midTable = new PdfPTable(width);
        midTable.setWidthPercentage(33);
        midTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        midTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        Phrase phrase = new Phrase();
        Chunk ch = new Chunk("City of Bloomington ", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        midTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("Planning and Transportation Department ", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        midTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("bloomington.in.gov", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        midTable.addCell(cell);
        //
        headTable.addCell(midTable);
        //
        PdfPTable rightTable = new PdfPTable(width);
        rightTable.setWidthPercentage(33);
        rightTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        rightTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        phrase = new Phrase();
        ch = new Chunk("401 N Morton St Suite 130 ", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("PO Box 100 \nBloomington, IN 47404", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("\n Phone: (812) 349-3423\nFax (812) 349-3520\nEmail: planning@bloomington.in.gov",
                fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        headTable.addCell(rightTable);
    } catch (Exception ex) {
        logger.error(ex);
    }
    return headTable;
}

From source file:permit.PermitPdf.java

License:Open Source License

void writePage(HttpServletResponse res, Permit permit) {
    ////from   w w  w.  j  ava  2  s . co  m
    // paper size legal (A4) 8.5 x 11
    // page 1-inch = 72 points
    //
    String fileName = "row_permit_" + permit.getId() + ".pdf";
    Rectangle pageSize = new Rectangle(612, 792); // 8.5" X 11"
    Document document = new Document(pageSize, 36, 36, 18, 18);
    ServletOutputStream out = null;
    Font fnt = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL);
    Font fntb = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
    Font fnts = new Font(Font.TIMES_ROMAN, 8, Font.NORMAL);
    Font fntbs = new Font(Font.TIMES_ROMAN, 8, Font.BOLD);
    String spacer = "   ";
    PdfPTable header = getHeader();
    Bond bond = permit.getBond();
    Invoice invoice = permit.getInvoice();
    if (bond == null)
        bond = new Bond();
    if (invoice == null)
        invoice = new Invoice();
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        String str = "";
        document.open();
        document.add(header);
        //
        // title
        float[] width = { 100f }; // one cell
        PdfPTable table = new PdfPTable(width);
        table.setWidthPercentage(100.0f);
        PdfPCell cell = new PdfPCell(new Phrase("Right Of Way Excavation Permit", fntb));
        //         
        cell.setBorder(Rectangle.BOTTOM);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        document.add(table);
        //
        // we need these later
        Paragraph pp = new Paragraph();
        Chunk ch = new Chunk(" ", fntb);
        Phrase phrase = new Phrase();
        //
        float[] widths = { 14f, 20f, 17f, 18f, 13f, 20f }; // percentages
        table = new PdfPTable(widths);
        table.setWidthPercentage(100.0f);
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        //
        // first row
        cell = new PdfPCell(new Phrase("Company", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getCompany().getName(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Status", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getStatus(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Permit", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getPermit_num(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        //
        // 2nd row
        //
        cell = new PdfPCell(new Phrase("Responsible", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getContact().getFullName(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Inspector", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getReviewer().getFullName(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Date Issued", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getDate(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        //
        // 3rd row
        cell = new PdfPCell(new Phrase("Project", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getProject(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Permit Fee", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("$" + permit.getFee(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Start Date", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getStart_date(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        //
        // 4th row
        cell = new PdfPCell(new Phrase("Bond Amount", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("$" + bond.getAmount(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Expiration Date", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(bond.getExpire_date(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Invoice", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(invoice.getStatus(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        document.add(table);
        //
        phrase = new Phrase(new Chunk(spacer, fnt));
        document.add(phrase);
        //
        float[] widths2 = { 25f, 15f, 15f, 25f, 10f, 10f };
        table = new PdfPTable(widths2);
        table.setWidthPercentage(100.0f);
        cell = new PdfPCell(new Phrase("Address", fntb));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Cut Type", fntb));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Utility", fntb));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Description", fntb));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Width", fntb));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Length", fntb));
        table.addCell(cell);
        List<Excavation> list = permit.getExcavations();
        if (list != null && list.size() > 0) {
            for (Excavation one : list) {
                cell = new PdfPCell(new Phrase(one.getAddress().getAddress(), fnt));
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(one.getCut_type(), fnt));
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(one.getUtility_type().getName(), fnt));
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(one.getCut_description(), fnt));
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(one.getWidth(), fnt));
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(one.getLength(), fnt));
                table.addCell(cell);
            }
        }
        document.add(table);
        //
        pp = new Paragraph();
        pp.setIndentationLeft(12);
        pp.setAlignment(Element.ALIGN_LEFT);
        pp.setLeading(0f, 1f);
        ch = new Chunk("\nSpecial Provisions\n", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        ch = new Chunk(permit.getNotes() + "\n", fnt);
        phrase.add(ch);
        pp.add(phrase);
        document.add(pp);
        //
        pp = new Paragraph();
        pp.setIndentationLeft(12);
        pp.setAlignment(Element.ALIGN_LEFT);
        ch = new Chunk("Standards Conditions of Approval\n", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        pp.add(phrase);
        document.add(pp);
        //
        pp = new Paragraph();
        pp.setIndentationLeft(12);
        pp.setAlignment(Element.ALIGN_LEFT);
        pp.setLeading(0f, 1f);
        ch = new Chunk("1 - " + conditions[0] + "\n", fntbs);
        phrase = new Phrase();
        phrase.add(ch);
        pp.add(phrase);
        document.add(pp);
        //
        int jj = 1;
        for (int j = 1; j < conditions.length; j++) {
            jj = j + 1;
            pp = new Paragraph();
            pp.setLeading(0f, 1f);
            pp.setIndentationLeft(12);
            pp.setAlignment(Element.ALIGN_LEFT);
            ch = new Chunk(jj + " - " + conditions[j] + "\n", fnts);
            phrase = new Phrase();
            phrase.add(ch);
            pp.add(phrase);
            document.add(pp);
        }

        pp = new Paragraph();
        pp.setIndentationLeft(20);
        pp.setAlignment(Element.ALIGN_RIGHT);
        ch = new Chunk("\n\n___________________\n", fnt);
        phrase = new Phrase();
        phrase.add(ch);
        pp.add(phrase);
        ch = new Chunk(permit.getReviewer().getFullName(), fnt);
        phrase = new Phrase();
        phrase.add(ch);
        pp.add(phrase);
        document.add(pp);
        document.close();
        writer.close();
        res.setHeader("Expires", "0");
        res.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        res.setHeader("Pragma", "public");
        //
        // setting the content type
        res.setContentType("application/pdf");
        //
        // the contentlength is needed for MSIE!!!
        res.setContentLength(baos.size());
        //
        out = res.getOutputStream();
        if (out != null) {
            baos.writeTo(out);
        }
    } catch (Exception ex) {
        logger.error(ex);
    }

}

From source file:permit.ReceiptPdf.java

License:Open Source License

void writePages(HttpServletResponse res, Receipt receipt) {
    ////from  ww  w .ja va  2 s .c o  m
    // paper size legal (A4) 8.5 x 11
    // page 1-inch = 72 points
    //
    Invoice invoice = receipt.getInvoice();
    String fileName = "row_receipt_" + receipt.getId() + ".pdf";
    Rectangle pageSize = new Rectangle(612, 792); // 8.5" X 11"
    Document document = new Document(pageSize, 36, 36, 18, 18);
    ServletOutputStream out = null;
    Font fnt = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL);
    Font fntb = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
    Font fnts = new Font(Font.TIMES_ROMAN, 8, Font.NORMAL);
    Font fntbs = new Font(Font.TIMES_ROMAN, 8, Font.BOLD);
    String spacer = "   ";
    PdfPTable header = getHeader();
    Company company = invoice.getCompany();
    Contact contact = null;
    if (invoice.hasContact()) {
        contact = invoice.getContact();
    }
    List<Page> pages = invoice.getPages();
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        String str = "";
        document.open();
        document.add(header);
        //
        // title
        float[] width = { 100f }; // one cell
        PdfPTable table = new PdfPTable(width);
        table.setWidthPercentage(100.0f);
        PdfPCell cell = new PdfPCell(new Phrase("RECEIPT", fntb));
        //         
        cell.setBorder(Rectangle.BOTTOM);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        document.add(table);
        //
        // we need these later
        Paragraph pp = new Paragraph();
        Chunk ch = new Chunk(" ", fntb);
        Phrase phrase = new Phrase();
        //
        float[] widths = { 35f, 30f, 35f }; // percentages
        table = new PdfPTable(widths);
        table.setWidthPercentage(100.0f);
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        //
        // first row
        float[] widthOne = { 100f };
        PdfPTable leftTable = new PdfPTable(widthOne);
        leftTable.setWidthPercentage(35.0f);
        leftTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        //
        if (company != null) {
            ch = new Chunk("Company\n", fntb);
            phrase = new Phrase();
            phrase.add(ch);
            cell = new PdfPCell(phrase);
            cell.setBorder(Rectangle.NO_BORDER);
            leftTable.addCell(cell);
            phrase = new Phrase();
            ch = new Chunk(company.getName() + "\n", fnt);
            phrase.add(ch);
            cell = new PdfPCell(phrase);
            cell.setBorder(Rectangle.NO_BORDER);
            leftTable.addCell(cell);
        }
        if (contact != null) {
            phrase = new Phrase();
            ch = new Chunk(contact.getFullName(), fnt);
            phrase.add(ch);
            cell = new PdfPCell(phrase);
            cell.setBorder(Rectangle.NO_BORDER);
            leftTable.addCell(cell);
            phrase = new Phrase();
            ch = new Chunk(contact.getAddress(), fnt);
            phrase.add(ch);
            cell = new PdfPCell(phrase);
            cell.setBorder(Rectangle.NO_BORDER);
            leftTable.addCell(cell);
            ch = new Chunk(contact.getCityStateZip(), fnt);
            phrase = new Phrase();
            phrase.add(ch);
            cell = new PdfPCell(phrase);
            cell.setBorder(Rectangle.NO_BORDER);
            leftTable.addCell(cell);
        }
        table.addCell(leftTable);
        //
        // middle cell
        //
        cell = new PdfPCell(new Phrase(spacer, fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        //
        float[] widths2 = { 50f, 50f }; // percentages
        PdfPTable rightTable = new PdfPTable(widths2);
        rightTable.setWidthPercentage(35.0f);
        rightTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        //
        ch = new Chunk("Receipt No.", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk(receipt.getId(), fnt);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk("Invoice No.", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk(invoice.getInvoice_num(), fnt);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk("Receipt Date", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk(receipt.getDate(), fnt);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk("Amount Paid $", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("$" + receipt.getAmount_paid(), fnt);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        ch = new Chunk("Payment Method ", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk(receipt.getPayment_type(), fnt);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        table.addCell(rightTable);
        //
        document.add(table);
        //
        phrase = new Phrase(new Chunk(spacer, fnt));
        document.add(phrase);
        //
        int jj = 0;
        if (pages != null) {
            for (Page page : pages) {
                jj++;
                PdfPTable borderTable = new PdfPTable(widthOne);
                borderTable.setWidthPercentage(100.0f);
                borderTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                float[] widthTwo = { 50f, 50f };
                PdfPTable titleTable = new PdfPTable(widthTwo);
                titleTable.setWidthPercentage(75.0f);
                titleTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                phrase = new Phrase("Receipt Number ", fntb);
                ch = new Chunk(receipt.getId(), fnt);
                phrase.add(ch);
                cell = new PdfPCell(phrase);
                cell.setHorizontalAlignment(Rectangle.ALIGN_LEFT);
                cell.setBorder(Rectangle.NO_BORDER);
                titleTable.addCell(cell);
                //
                phrase = new Phrase(page.getPage_num(), fnt);
                cell = new PdfPCell(phrase);
                cell.setHorizontalAlignment(Rectangle.ALIGN_RIGHT);
                cell.setBorder(Rectangle.NO_BORDER);
                titleTable.addCell(cell);
                //
                borderTable.addCell(titleTable);
                float[] width4 = { 25f, 40f, 25f, 10f };
                PdfPTable contTable = new PdfPTable(width4);
                cell = new PdfPCell(new Phrase("Excavation Permit Number", fntb));
                contTable.addCell(cell);
                cell = new PdfPCell(new Phrase("Project", fntb));
                contTable.addCell(cell);
                cell = new PdfPCell(new Phrase("Date Issued", fntb));
                contTable.addCell(cell);
                cell = new PdfPCell(new Phrase("Permit Fee", fntb));
                contTable.addCell(cell);
                List<Permit> permits = page.getPermits();
                if (permits != null) {
                    for (Permit permit : permits) {
                        cell = new PdfPCell(new Phrase(permit.getPermit_num(), fnt));
                        contTable.addCell(cell);
                        phrase = new Phrase(permit.getProject() + "\n", fnt);
                        List<Excavation> cuts = permit.getExcavations();
                        if (cuts != null) {
                            for (Excavation one : cuts) {
                                ch = new Chunk(one.getAddress() + " (" + one.getCut_type() + ")", fnt);
                                phrase.add(ch);
                            }
                        }
                        cell = new PdfPCell(phrase);
                        contTable.addCell(cell);
                        cell = new PdfPCell(new Phrase(permit.getDate(), fnt));
                        contTable.addCell(cell);
                        cell = new PdfPCell(new Phrase("$" + permit.getFee(), fnt));
                        cell.setHorizontalAlignment(Rectangle.ALIGN_RIGHT);
                        contTable.addCell(cell);
                        cell = new PdfPCell(new Phrase(spacer, fnt));
                        //
                        // space line
                        //
                        cell.setColspan(4);
                        contTable.addCell(cell);
                    }
                }
                if (page.getNeededLines() > 0) { // first page 
                    for (int j = 0; j < page.getNeededLines(); j++) {
                        cell = new PdfPCell(new Phrase(spacer, fnt));
                        contTable.addCell(cell);
                        contTable.addCell(cell);
                        contTable.addCell(cell);
                        contTable.addCell(cell);
                    }
                }
                if (jj == pages.size()) {
                    cell = new PdfPCell(new Phrase("Total Invoice Amount\n" + invoice.getTotal(), fntb));
                    cell.setHorizontalAlignment(Rectangle.ALIGN_RIGHT);
                    cell.setColspan(4);
                    contTable.addCell(cell);
                }
                borderTable.addCell(contTable);
                cell = new PdfPCell(new Phrase("Thank You.", fnt));

                cell.setHorizontalAlignment(Rectangle.ALIGN_CENTER);
                cell.setBorder(Rectangle.NO_BORDER);
                borderTable.addCell(cell);
                borderTable.addCell(titleTable); // receipt and date
                document.add(borderTable);
                if (jj < pages.size()) {
                    document.newPage();
                }
            }
        }
        //
        document.close();
        writer.close();
        res.setHeader("Expires", "0");
        res.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        // res.setHeader("Content-Disposition","attachment; filename="+fileName);         
        res.setHeader("Pragma", "public");
        //
        // setting the content type
        res.setContentType("application/pdf");
        //
        // the contentlength is needed for MSIE!!!
        res.setContentLength(baos.size());
        //
        out = res.getOutputStream();
        if (out != null) {
            baos.writeTo(out);
        }
    } catch (Exception ex) {
        logger.error(ex);
    }

}

From source file:questions.forms.FormWithTooltips.java

private static PdfPTable createTable(PdfWriter writer, PdfFormField parent)
        throws IOException, DocumentException {
    PdfPTable table = new PdfPTable(2);
    PdfPCell cell;/*from www. j a  v  a  2  s .  co m*/
    TextField field;
    table.getDefaultCell().setPadding(5f);

    table.addCell("Your name:");
    cell = new PdfPCell();
    field = new TextField(writer, new Rectangle(0, 0), "name");
    field.setFontSize(12);
    cell.setCellEvent(new FormWithTooltips(parent, field.getTextField(), 1, "Your name"));
    table.addCell(cell);

    table.addCell("Your home address:");
    cell = new PdfPCell();
    field = new TextField(writer, new Rectangle(0, 0), "address");
    field.setFontSize(12);
    cell.setCellEvent(new FormWithTooltips(parent, field.getTextField(), 1, "Street and number"));
    table.addCell(cell);

    table.addCell("Postal code:");
    cell = new PdfPCell();
    field = new TextField(writer, new Rectangle(0, 0), "postal_code");
    field.setFontSize(12);
    cell.setCellEvent(new FormWithTooltips(parent, field.getTextField(), 1, "Postal code"));
    table.addCell(cell);

    table.addCell("City:");
    cell = new PdfPCell();
    field = new TextField(writer, new Rectangle(0, 0), "city");
    field.setFontSize(12);
    cell.setCellEvent(new FormWithTooltips(parent, field.getTextField(), 1, "City"));
    table.addCell(cell);

    table.addCell("Country:");
    cell = new PdfPCell();
    field = new TextField(writer, new Rectangle(0, 0), "country");
    field.setFontSize(12);
    cell.setCellEvent(new FormWithTooltips(parent, field.getTextField(), 1, "Country"));
    table.addCell(cell);

    table.addCell("Your email address:");
    cell = new PdfPCell();
    field = new TextField(writer, new Rectangle(0, 0), "email");
    field.setFontSize(12);
    cell.setCellEvent(new FormWithTooltips(parent, field.getTextField(), 1, "mail address"));
    table.addCell(cell);
    return table;
}

From source file:questions.tables.TableHeaderAlternateBackground.java

public static void main(String[] args) {
    // step 1: creation of a document-object
    Document document = new Document(PageSize.A4.rotate());
    try {/*from w w  w  .  j a  va 2  s.c  om*/
        // step 2:
        // we create a writer
        PdfWriter.getInstance(
                // that listens to the document
                document,
                // and directs a PDF-stream to a file
                new FileOutputStream(RESULT));
        // step 3: we open the document
        document.open();
        // step 4: we add a table to the document
        PdfPTable datatable = new PdfPTable(10);
        datatable.setTableEvent(new AlternateBackground());
        int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 };
        datatable.setWidths(headerwidths);
        datatable.setWidthPercentage(100);
        datatable.getDefaultCell().setPadding(5);

        // The header starts with a cell that spans 10 columns
        PdfPCell cell = new PdfPCell(new Phrase("Administration - System Users Report",
                FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD)));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBorderWidth(2);
        cell.setColspan(10);
        cell.setBackgroundColor(Color.YELLOW);
        cell.setUseDescender(true);
        datatable.addCell(cell);
        // We need 4 cells with rowspan 2
        datatable.getDefaultCell().setBorderWidth(2);
        datatable.getDefaultCell().setBackgroundColor(Color.YELLOW);
        datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        datatable.addCell("User Id");
        datatable.addCell("Name\nAddress");
        datatable.addCell("Company");
        datatable.addCell("Department");
        datatable.getDefaultCell().setBackgroundColor(null);
        // we use a nested table to fake this
        PdfPTable permissions = new PdfPTable(6);
        permissions.getDefaultCell().setBackgroundColor(Color.YELLOW);
        permissions.getDefaultCell().setBorderWidth(2);
        permissions.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        permissions.getDefaultCell().setColspan(6);
        permissions.addCell("Permissions");
        permissions.getDefaultCell().setColspan(1);
        permissions.addCell("Admin");
        permissions.addCell("Data");
        permissions.addCell("Expl");
        permissions.addCell("Prod");
        permissions.addCell("Proj");
        permissions.addCell("Online");
        PdfPCell permission = new PdfPCell(permissions);
        permission.setColspan(6);
        datatable.addCell(permission);
        // this is the end of the table header
        // as far as PdfPTable is concerned there are 2 rows in the header
        datatable.setHeaderRows(2);

        // we add the data to the table
        datatable.getDefaultCell().setBorderWidth(1);
        for (int i = 1; i < 50; i++) {
            datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
            datatable.addCell("myUserId");
            datatable.addCell("Person " + i);
            datatable.addCell("No Name Company");
            datatable.addCell("D" + i);
            datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            for (int j = 0; j < 6; j++)
                datatable.addCell(Math.random() > .5 ? "Yes" : "No");
        }
        document.add(datatable);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

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

From source file:s2s.luna.reports.Stampa_documento.java

License:GNU General Public License

@SuppressWarnings("CallToThreadDumpStack")
private boolean addPDF(String titolo, byte[] fileContent, Document document, PdfWriter writer) {
    try {//from w  w  w .  jav a  2s . c  o  m
        // Inizializzo il contenitore del pdf da importare.
        PdfContentByte cb = writer.getDirectContent();

        // Inizializzo la variabile dove appogger (una alla volta)
        // le pagine del pdf da importare.
        PdfImportedPage page;

        // Apro in lettura il pdf da importare.
        PdfReader pdfReader = new PdfReader(fileContent);

        // Ne determino il numero di pagine totali.
        int pdfPageNumber = pdfReader.getNumberOfPages();

        // Inzializzo il contatore delle pagine.
        int pageOfCurrentReaderPDF = 1;

        // Per ogni pagina...
        while (pageOfCurrentReaderPDF <= pdfPageNumber) {
            // Creo una nuova pagina vuota sul pdf di destinazione.
            document.newPage();
            // Estraggo la pagina dal pdf da importare.
            page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
            // Coverto la pagina in un immagine.
            Image pageImg = Image.getInstance(page);
            // Aggiungo la pagina estratta al pdf di destinazione, ruotandola se necessario.
            PdfPTable table = new PdfPTable(1);
            PdfPCell defaultCell = table.getDefaultCell();
            defaultCell.setRotation(-pdfReader.getPageRotation(pageOfCurrentReaderPDF));
            defaultCell.setBorder(Rectangle.NO_BORDER);
            table.addCell(pageImg);
            document.add(table);
            // Incremento il contatore delle pagine
            pageOfCurrentReaderPDF++;
        }
        return true;
    } catch (Exception ex) {
        // Eccezione silenziosa.
        // Gestisce il caso in cui si verifica un errore non previsto
        ex.printStackTrace();
        return false;
    }
}

From source file:se.idega.idegaweb.commune.school.business.StudentAddressLabelsWriter.java

License:Open Source License

/**
 * Adds a student address to the specified document.
 *//*  w ww.  ja v a 2s  .  co m*/
protected void addAddress(PdfWriter writer, IWResourceBundle iwrb, MailReceiver student, int studentCount) {

    // User student = member.getStudent();
    // Address address = userBusiness.getUsersMainAddress(student);
    // PostalCode postalCode = address != null ? address.getPostalCode() : null;

    // String name = student.getFirstName() + " " + student.getLastName();
    // String streetAddress = address != null ? address.getStreetAddress() : "";
    // String postalAddress = "";
    // if (address != null && postalCode != null) {
    // String zip = postalCode.getPostalCode();
    // if (zip.length() > 4) {
    // zip = zip.substring(0, 3) + " " + zip.substring(3, 5);
    // }
    // postalAddress = zip + " " + postalCode.getName();
    // }

    PdfPTable table = new PdfPTable(1);
    table.setTotalWidth(ADDRESS_TABLE_WIDTH);
    table.getDefaultCell().setPadding(3);
    PdfPCell cell;

    cell = new PdfPCell(
            new Phrase(iwrb.getLocalizedString(KEY_TO_CUSTODIAN_FOR, "To custodian for") + ":", this.font));
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell(new Phrase(student.getStudentName(), this.font));
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell(new Phrase(student.getStreetAddress(), this.font));
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell(new Phrase(student.getPostalAddress(), this.font));
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    int row = (studentCount / NR_OF_COLUMNS) % NR_OF_ROWS;
    int column = studentCount % NR_OF_COLUMNS;
    table.writeSelectedRows(0, -1, LEFT_MARGIN + column * ADDRESS_TABLE_WIDTH,
            TOP_START - row * ADDRESS_TABLE_HEIGHT, writer.getDirectContent());
}

From source file:sms.ReportForms.java

public void utext(String query, String Name, String dob, String house, String formclass, String kcpe,
        String imgurl, String kcpegrade, String id) {
    methods nn = new methods();
    String u = unig();/*from w w  w  .ja va  2s  .c om*/
    checkPreviousResults(u, kcpe, kcpegrade, id);
    ArrayList<ExamDbDataHolder> users = selectExamResults(u, query);
    if (users.size() < 2) {
        Form1Exams n = new Form1Exams();
        String[] Subjects = n.findSubjectid();
        String[] Subjectsnames = n.findSubjectname();

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File(","));
        chooser.setDialogTitle("Save at");
        chooser.setApproveButtonText("save");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {

                Document pdfp = new Document();
                PdfWriter w = PdfWriter.getInstance(pdfp,
                        new FileOutputStream(new File(chooser.getSelectedFile(), "" + sid.getText() + ".pdf")));
                pdfp.open();
                PdfContentByte canvas = w.getDirectContentUnder();
                Image imgb = Image.getInstance("C:\\Users\\kimani kogi\\Pictures\\icons\\logo.png");
                imgb.setAbsolutePosition(2, 2);
                imgb.scaleAbsoluteHeight(PageSize.A4.getHeight() / 4);
                imgb.scaleAbsoluteWidth(PageSize.A4.getWidth() / 4);
                canvas.saveState();
                PdfGState state = new PdfGState();
                state.setFillOpacity(0.6f);
                canvas.setGState(state);
                canvas.addImage(imgb);
                canvas.restoreState();

                PdfPTable tbl1 = new PdfPTable(2);
                //  tbl.setWidthPercentage(100);

                tbl1.setTotalWidth(575);
                tbl1.setLockedWidth(true);

                tbl1.setWidths(new int[] { 1, 4 });
                if (imgurl.equals("image")) {
                    tbl1.addCell("no image ");

                    String img = imgurl;
                } else {
                    tbl1.addCell(createImageCell(imgurl));

                }

                tbl1.addCell(createTextCell(schooldetails));

                PdfPTable tbl = new PdfPTable(8);
                //  tbl.setWidthPercentage(100);
                tbl.setTotalWidth(575);
                tbl.setLockedWidth(true);
                tbl.setSpacingBefore(8);
                tbl.setSpacingAfter(6);
                tbl.getDefaultCell().setBorderWidthTop(2);
                tbl.getDefaultCell().setBorderWidthLeft(0);
                tbl.getDefaultCell().setBorderWidthRight(0);
                tbl.setWidths(new int[] { 1, 1, 1, 2, 1, 1, 1, 1 });
                tbl.addCell("Adm No:");
                tbl.addCell(sid.getText());
                tbl.addCell("Name:");
                tbl.addCell(Name);
                tbl.addCell("Form:");
                tbl.addCell(formclass);
                tbl.addCell("Kcpe:");
                tbl.addCell(kcpe);

                tbl.addCell("House:");
                tbl.addCell(house);
                tbl.addCell("Term:");
                tbl.addCell("Second term");
                tbl.addCell("Year:");
                tbl.addCell("2017");
                tbl.addCell("DOB:");
                tbl.addCell(dob);

                PdfPTable tbl2 = new PdfPTable(5);
                //  tbl.setWidthPercentage(100);
                tbl2.setTotalWidth(575);
                tbl2.setLockedWidth(true);
                //  tbl2.getDefaultCell().setFixedHeight(35f);
                tbl2.setWidths(new int[] { 2, 1, 1, 2, 2 });

                tbl2.addCell(creatTextCellHeader("Subjects"));
                tbl2.addCell(creatTextCellHeader("Exams"));
                tbl2.addCell(creatTextCellHeader("Grade"));
                tbl2.addCell(creatTextCellHeader("Ratings"));
                tbl2.addCell(creatTextCellHeader("Remarks"));
                // String []  Subjectsnames=n.findSubjectname();
                for (int i = 0; i < users.size(); i++) {
                    for (int a = 0; a < Subjectsnames.length; a++) {
                        tbl2.addCell(Subjectsnames[a]);
                        String re = null;
                        if (Subjects[a].equals("s1")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getMathematics());
                            re = ((ExamDbDataHolder) users.get(i)).getMathematics();
                            //  JOptionPane.showMessageDialog(null,((ExamDbDataHolder)users.get(i)).getMathematics());
                            // String maths = ((ExamDbDataHolder)users.get(i)).getMathematics();

                        } else if (Subjects[a].equals("s2")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getEnglish());
                            re = ((ExamDbDataHolder) users.get(i)).getEnglish();
                            //row[c] = ((ExamDbDataHolder)users.get(i)).getEnglish();
                            // c++;
                        } else if (Subjects[a].equals("s3")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getKiswahili());
                            re = ((ExamDbDataHolder) users.get(i)).getKiswahili();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getKiswahili();
                            // c++;
                        }

                        else if (Subjects[a].equals("s4")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getPhysics());
                            re = ((ExamDbDataHolder) users.get(i)).getPhysics();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getPhysics();
                            //  c++;
                        } else if (Subjects[a].equals("s5")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getChemistry());
                            re = ((ExamDbDataHolder) users.get(i)).getChemistry();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getChemistry();
                            // c++;
                        } else if (Subjects[a].equals("s6")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getBiology());
                            re = ((ExamDbDataHolder) users.get(i)).getBiology();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getBiology();
                            //  c++;
                        } else if (Subjects[a].equals("s7")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getHistory());
                            re = ((ExamDbDataHolder) users.get(i)).getHistory();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getHistory();
                            //  c++;
                        } else if (Subjects[a].equals("s8")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getGeography());
                            re = ((ExamDbDataHolder) users.get(i)).getGeography();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getGeography();
                            // c++;
                        } else if (Subjects[a].equals("s9")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getCre());
                            re = ((ExamDbDataHolder) users.get(i)).getCre();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getCre();
                            //  c++;
                        } else if (Subjects[a].equals("s10")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getIre());
                            re = ((ExamDbDataHolder) users.get(i)).getIre();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getIre();
                            //  c++;
                        } else if (Subjects[a].equals("s11")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getHre());
                            re = ((ExamDbDataHolder) users.get(i)).getHre();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getHre();
                            //  c++;
                        } else if (Subjects[a].equals("s12")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getAgriculture());
                            re = ((ExamDbDataHolder) users.get(i)).getAgriculture();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getAgriculture();
                            //  c++;
                        } else if (Subjects[a].equals("s13")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getHomescience());
                            re = ((ExamDbDataHolder) users.get(i)).getHomescience();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getHomescience();
                            //  c++;
                        } else if (Subjects[a].equals("s14")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getArtdesign());
                            re = ((ExamDbDataHolder) users.get(i)).getArtdesign();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getArtdesign();
                            //  c++;
                        } else if (Subjects[a].equals("s15")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getComputer());
                            re = ((ExamDbDataHolder) users.get(i)).getComputer();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getComputer();
                            // c++;

                        } else if (Subjects[a].equals("s16")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getBuilding());
                            re = ((ExamDbDataHolder) users.get(i)).getBuilding();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getBuilding();
                            //  c++;
                        } else if (Subjects[a].equals("s17")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getWoodwork());
                            re = ((ExamDbDataHolder) users.get(i)).getWoodwork();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getWoodwork();
                            // c++;
                        } else if (Subjects[a].equals("s18")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getMetalwork());
                            re = ((ExamDbDataHolder) users.get(i)).getMetalwork();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getMetalwork();
                            // c++;
                        } else if (Subjects[a].equals("s19")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getMusic());
                            re = ((ExamDbDataHolder) users.get(i)).getMusic();
                            //   row[c] = ((ExamDbDataHolder)users.get(i)).getMusic();
                            //  c++;
                        } else if (Subjects[a].equals("s20")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getFrench());
                            re = ((ExamDbDataHolder) users.get(i)).getFrench();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getFrench();
                            // c++;
                        } else if (Subjects[a].equals("s21")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getGerman());
                            re = ((ExamDbDataHolder) users.get(i)).getGerman();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getGerman();
                            // c++;
                        } else if (Subjects[a].equals("s22")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getArabic());
                            re = ((ExamDbDataHolder) users.get(i)).getArabic();
                            // row[c] = ((ExamDbDataHolder)users.get(i)).getArabic();
                            // c++;
                        } else if (Subjects[a].equals("s23")) {
                            tbl2.addCell(((ExamDbDataHolder) users.get(i)).getBusiness());
                            re = ((ExamDbDataHolder) users.get(i)).getBusiness();
                            //  row[c] = ((ExamDbDataHolder)users.get(i)).getBusiness();
                            // c++;
                        }
                        String[] gr = nn.checkGrade(re, Subjects[a]);
                        tbl2.addCell(gr[0]);
                        tbl2.addCell(gr[1]);
                        tbl2.addCell("");
                    }
                }

                //add for all other subjects

                PdfPTable tbl3 = new PdfPTable(4);
                //  tbl.setWidthPercentage(100);
                tbl3.setTotalWidth(575);
                tbl3.setLockedWidth(true);
                tbl3.setSpacingBefore(8);
                tbl3.setSpacingAfter(6);
                //  tbl3.getDefaultCell().setBorderWidthTop(0);
                tbl3.getDefaultCell().setBorder(0);
                //  tbl3.getDefaultCell().setBorderWidthLeft(0);
                //  tbl3.getDefaultCell().setBorderWidthRight(0);
                tbl3.setWidths(new int[] { 1, 1, 1, 1 });
                tbl3.addCell(creatTextCellHeader("CURRENT MARKS:"));
                tbl3.addCell(creatTextCellHeader(String.valueOf(((ExamDbDataHolder) users.get(0)).getTotal())));
                float g = Float.valueOf(((ExamDbDataHolder) users.get(0)).getTotal());
                //  tbl.addCell(String.format("%.1f", g));

                String gr = nn.checkGrade(yearid, String.format("%.1f", g / getYear()));

                tbl3.addCell(creatTextCellHeader("PREVIOUS MARKS:"));
                tbl3.addCell(creatTextCellHeader("459"));
                tbl3.addCell(creatTextCellHeader("AVERAGE MARKS:"));
                tbl3.addCell(creatTextCellHeader(String.format("%.1f", g / getYear())));
                tbl3.addCell(creatTextCellHeader("PREVIOUS AVERAGE:"));
                tbl3.addCell(creatTextCellHeader("50"));
                tbl3.addCell(creatTextCellHeader("MEAN GRADE:"));
                tbl3.addCell(creatTextCellHeader(gr));
                tbl3.addCell(creatTextCellHeader("PREVIOUS MEAN GRADE:"));
                tbl3.addCell(creatTextCellHeader("B"));
                tbl3.addCell(creatTextCellHeader("POS"));
                tbl3.addCell(creatTextCellHeader("3 "));
                tbl3.addCell(creatTextCellHeader("OUT OF"));
                tbl3.addCell(creatTextCellHeader("79"));

                PdfPTable tbl4 = new PdfPTable(2);
                //  tbl.setWidthPercentage(100);
                tbl4.setHorizontalAlignment(0);
                //tbl4.HorizontalAlignment=Element.ALIGN_LEFT;
                tbl4.setTotalWidth(575 / 2);
                tbl4.setLockedWidth(true);
                tbl4.setSpacingBefore(8);
                tbl4.setSpacingAfter(6);
                //  tbl3.getDefaultCell().setBorderWidthTop(0);
                tbl4.getDefaultCell().setBorder(0);
                //  tbl3.getDefaultCell().setBorderWidthLeft(0);
                //  tbl3.getDefaultCell().setBorderWidthRight(0);
                tbl4.setWidths(new int[] { 2, 1 });
                PdfPCell cell = new PdfPCell(new Paragraph("REMARKS"));
                cell.setColspan(2);

                tbl4.addCell(cell);
                tbl4.addCell(creatTextCellHeader(
                        "CLASS TEACHERS........................................................."
                                + ":............................\n\n\n"));
                tbl4.addCell(creatTextCellChart("chart"));
                tbl4.addCell(creatTextCellHeader(
                        "SIGNATURE-------------------------------------------------------------"
                                + "-------------------------------\n\n\n"));
                tbl4.addCell(creatTextCellHeader(
                        "-------------\n\n\n-----------------------------------------------------------"
                                + "-----------------------------"));

                pdfp.add(tbl1);

                pdfp.add(tbl);

                pdfp.add(tbl2);
                pdfp.add(tbl3);
                pdfp.add(tbl4);

                //        Paragraph p=new Paragraph();
                //        p.setAlignment(Element.ALIGN_CENTER);
                //        p.setFont(FontFactory.getFont(FontFactory.TIMES_BOLD,18,Font.BOLD));
                //         Paragraph po=new Paragraph();
                //        po.setAlignment(Element.ALIGN_CENTER);
                //        po.setFont(FontFactory.getFont(FontFactory.TIMES_BOLD,16,Font.BOLD));
                //        
                //        Paragraph pd=new Paragraph();
                //          pd.setAlignment(Element.ALIGN_CENTER);
                //         pd.setFont(FontFactory.getFont(FontFactory.TIMES_BOLD,14,Font.BOLD));
                //        p.add("ITHANGA SECONDARY SCHOOL");
                //        po.add("PO.BOX 238  ITHANGA THIKA");
                //         pd.add(new Date().toString());
                //        pdfp.add(p);
                //        pdfp.add(po);
                //        pdfp.add(pd);
                //         
                //        
                //      
                //        pdfp.add(new Paragraph("\n.................................................................."
                //                + ".................................................................................\n"));
                //         String []names=  n.findSubjectname();
                //        PdfPTable tbl=new PdfPTable(names.length+6);
                //        tbl.setWidths(new float[]{1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1});
                //       // tbl.setWidthPercentage(100);
                //        tbl.setTotalWidth(575);
                //        tbl.setLockedWidth(true);
                //         PdfPTable tbl1=new PdfPTable(names.length+6);
                //        tbl1.setWidths(new float[]{1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1});
                //       // tbl.setWidthPercentage(100);
                //        tbl1.setTotalWidth(575);
                //        tbl1.setLockedWidth(true);
                //     PdfPCell cell=new PdfPCell (new Paragraph("RESULTS"));
                //     cell.setColspan((names.length+4)*2);
                //     cell.setBackgroundColor(Color.CYAN);
                //        tbl1.addCell(cell);
                //        tbl1.addCell("Pos");
                //          tbl1.addCell("id");
                //          tbl1.addCell("Name");
                //          int a;

                pdfp.close();
            } catch (Exception j) {
                j.printStackTrace();
            }

            // Image img=new Image.getInstance("j.png");

        }
    }
}