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

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

Introduction

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

Prototype

public PdfPTable(PdfPTable table) 

Source Link

Document

Constructs a copy of a PdfPTable.

Usage

From source file:com.logiware.accounting.reports.ArDisputeReportCreator.java

private void writeContent() throws Exception {
    List<ReportBean> disputeList = new ArReportsDAO().getDisputeList(arReportsForm);
    contentTable = new PdfPTable(9);
    contentTable.setWidthPercentage(100);
    contentTable.setWidths(new float[] { 10, 22, 8, 12, 10, 10, 8, 10, 10 });
    contentTable// www .jav a 2 s.  c  om
            .addCell(createHeaderCell("Customer Number", blackBoldFont8, Element.ALIGN_LEFT, Rectangle.BOTTOM));
    contentTable
            .addCell(createHeaderCell("Customer Name", blackBoldFont8, Element.ALIGN_LEFT, Rectangle.BOTTOM));
    contentTable
            .addCell(createHeaderCell("Invoice Date", blackBoldFont8, Element.ALIGN_LEFT, Rectangle.BOTTOM));
    contentTable.addCell(createHeaderCell("Invoice/BL", blackBoldFont8, Element.ALIGN_LEFT, Rectangle.BOTTOM));
    contentTable
            .addCell(createHeaderCell("Invoice Amount", blackBoldFont8, Element.ALIGN_LEFT, Rectangle.BOTTOM));
    contentTable
            .addCell(createHeaderCell("Invoice Balance", blackBoldFont8, Element.ALIGN_LEFT, Rectangle.BOTTOM));
    contentTable
            .addCell(createHeaderCell("Disputed Date", blackBoldFont8, Element.ALIGN_LEFT, Rectangle.BOTTOM));
    contentTable.addCell(createHeaderCell("Disputed By", blackBoldFont8, Element.ALIGN_LEFT, Rectangle.BOTTOM));
    contentTable.addCell(createHeaderCell("Disputed To", blackBoldFont8, Element.ALIGN_LEFT, Rectangle.BOTTOM));
    for (ReportBean dispute : disputeList) {
        contentTable.addCell(createTextCell(dispute.getCustomerNumber(), blackNormalFont7, Rectangle.BOTTOM));
        contentTable.addCell(createTextCell(dispute.getCustomerName(), blackNormalFont7, Rectangle.BOTTOM));
        contentTable.addCell(createTextCell(dispute.getInvoiceDate(), blackNormalFont7, Rectangle.BOTTOM));
        contentTable.addCell(createTextCell(dispute.getInvoiceOrBl(), blackNormalFont7, Rectangle.BOTTOM));
        contentTable.addCell(createTextCell(dispute.getInvoiceAmount(), blackNormalFont7, Rectangle.BOTTOM));
        contentTable.addCell(createTextCell(dispute.getBalance(), blackNormalFont7, Rectangle.BOTTOM));
        contentTable.addCell(createTextCell(dispute.getDisputedDate(), blackNormalFont7, Rectangle.BOTTOM));
        contentTable.addCell(createTextCell(dispute.getDisputedUser(), blackNormalFont7, Rectangle.BOTTOM));
        contentTable.addCell(createTextCell(dispute.getDisputeSentUser(), blackNormalFont7, Rectangle.BOTTOM));
    }
    document.add(contentTable);
}

From source file:com.nokia.s60tools.swmtanalyser.wizards.ReportCreationJob.java

License:Open Source License

/**
 * Returns a table for issues of given priority type from the selected issues.
 * @param allTreeItems tree/*  w  ww. j  av  a2  s  .  c om*/
 * @param p priority
 * @return
 */
private PdfPTable getTableForIssues(Tree allTreeItems, AnalyserConstants.Priority p) {

    float[] relativeWidth = { 60, 25, 15 };//100% total
    PdfPTable table = new PdfPTable(relativeWidth);
    table.setWidthPercentage(100);

    PdfPCell cell = new PdfPCell(new Paragraph("Item name"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setBackgroundColor(colorTableHeaderBackGrd);
    cell.setPadding(cellPaddingTableHeader);
    table.addCell(cell);

    cell = new PdfPCell(new Paragraph("Event"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setBackgroundColor(colorTableHeaderBackGrd);
    cell.setPadding(cellPaddingTableHeader);
    table.addCell(cell);

    cell = new PdfPCell(new Paragraph("Delta"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setBackgroundColor(colorTableHeaderBackGrd);
    cell.setPadding(cellPaddingTableHeader);
    table.addCell(cell);

    for (TreeItem parent : allTreeItems.getItems()) {
        for (TreeItem child : parent.getItems()) {
            if (child.getText(4).toLowerCase().equals(p.toString().toLowerCase())) {

                cell = new PdfPCell(new Paragraph(child.getText(1), fontNormalSmallTables));
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setPadding(cellPaddingSmall);
                table.addCell(cell);

                cell = new PdfPCell(new Paragraph(child.getText(2), fontNormalSmallTables));
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setPadding(cellPaddingSmall);
                table.addCell(cell);

                cell = new PdfPCell(new Paragraph(child.getText(3), fontNormalSmallTables));
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setPadding(cellPaddingSmall);
                table.addCell(cell);
            }
        }
    }
    if (table.getRows().size() > 1)
        return table;

    return table;
}

From source file:com.nokia.s60tools.swmtanalyser.wizards.ReportCreationJob.java

License:Open Source License

/**
 * Returns a table for the overview issues of all types.
 * @param all//  w ww .j a  va2  s  .  com
 * @return
 */
private PdfPTable getTableForOverallIssues(Tree all) {
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);

    PdfPCell cell = new PdfPCell(new Paragraph("Severity"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setBackgroundColor(colorTableHeaderBackGrd);
    cell.setPadding(cellPaddingTableHeader);
    table.addCell(cell);

    cell = new PdfPCell(new Paragraph("Count"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setBackgroundColor(colorTableHeaderBackGrd);
    cell.setPadding(cellPaddingTableHeader);
    table.addCell(cell);

    int critical = 0;
    int high = 0;
    int normal = 0;

    for (TreeItem parent : all.getItems()) {
        for (TreeItem child : parent.getItems()) {
            if (child.getText(4).toLowerCase()
                    .equals(AnalyserConstants.Priority.CRITICAL.toString().toLowerCase()))
                critical++;
            else if (child.getText(4).toLowerCase()
                    .equals(AnalyserConstants.Priority.HIGH.toString().toLowerCase()))
                high++;
            else if (child.getText(4).toLowerCase()
                    .equals(AnalyserConstants.Priority.NORMAL.toString().toLowerCase()))
                normal++;
        }
    }

    table.addCell(new PdfPCell(new Paragraph("CRITICAL", fontNormalSmallTables)));
    table.addCell(new PdfPCell(new Paragraph(critical + "", fontNormalSmallTables)));
    table.addCell(new PdfPCell(new Paragraph("HIGH", fontNormalSmallTables)));
    table.addCell(new PdfPCell(new Paragraph(high + "", fontNormalSmallTables)));
    table.addCell(new PdfPCell(new Paragraph("NORMAL", fontNormalSmallTables)));
    table.addCell(new PdfPCell(new Paragraph(normal + "", fontNormalSmallTables)));

    return table;
}

From source file:com.nokia.s60tools.swmtanalyser.wizards.ReportCreationJob.java

License:Open Source License

/**
 * Returns a table for the selected issues.
 * @param issuesTree/* w  w  w  .  j  ava2  s  . co m*/
 * @return
 */
private PdfPTable getTableForTheSelectedIssues(Tree issuesTree) {

    float[] relativeWidth = { 50, 22, 14, 14 };//100% total
    PdfPTable table = new PdfPTable(relativeWidth);
    table.setWidthPercentage(100);

    PdfPCell cell = new PdfPCell(new Paragraph("Item name"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setBackgroundColor(colorTableHeaderBackGrd);
    cell.setPadding(cellPaddingTableHeader);
    table.addCell(cell);

    cell = new PdfPCell(new Paragraph("Event"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setBackgroundColor(colorTableHeaderBackGrd);
    cell.setPadding(cellPaddingTableHeader);
    table.addCell(cell);

    cell = new PdfPCell(new Paragraph("Delta"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setBackgroundColor(colorTableHeaderBackGrd);
    cell.setPadding(cellPaddingTableHeader);
    table.addCell(cell);

    cell = new PdfPCell(new Paragraph("Severity"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setBackgroundColor(colorTableHeaderBackGrd);
    cell.setPadding(cellPaddingTableHeader);
    table.addCell(cell);

    for (TreeItem item : issuesTree.getItems()) {
        ArrayList<TreeItem> selected = new ArrayList<TreeItem>();
        for (TreeItem child : item.getItems())
            if (child.getChecked())
                selected.add(child);

        if (selected.size() > 0) {
            cell = new PdfPCell(new Paragraph(item.getText(1), fontTable2ndHeaderText));
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBackgroundColor(colorTable2ndHeaderBackGrd);
            cell.setPadding(cellPaddingTableHeader);
            cell.setColspan(4);
            table.addCell(cell);

            for (TreeItem child : selected) {
                cell = new PdfPCell(new Paragraph(child.getText(1), fontNormalSmallTables));
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setPadding(cellPaddingSmall);
                table.addCell(cell);

                cell = new PdfPCell(new Paragraph(child.getText(2), fontNormalSmallTables));
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setPadding(cellPaddingSmall);
                table.addCell(cell);

                cell = new PdfPCell(new Paragraph(child.getText(3), fontNormalSmallTables));
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setPadding(cellPaddingSmall);
                table.addCell(cell);

                cell = new PdfPCell(new Paragraph(child.getText(4), fontNormalSmallTables));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);

                if (child.getText(4).toLowerCase()
                        .equals(AnalyserConstants.Priority.CRITICAL.toString().toLowerCase())) {
                    cell.setBackgroundColor(colorSeverityCritical);
                } else if (child.getText(4).toLowerCase()
                        .equals(AnalyserConstants.Priority.HIGH.toString().toLowerCase())) {
                    cell.setBackgroundColor(colorSeverityHigh);
                } else if (child.getText(4).toLowerCase()
                        .equals(AnalyserConstants.Priority.NORMAL.toString().toLowerCase())) {
                    cell.setBackgroundColor(colorSeverityNormal);
                }
                cell.setPadding(cellPaddingSmall);
                table.addCell(cell);
            }
        }
    }
    return table;
}

From source file:com.orange.atk.compModel.PDFGenerator.java

License:Apache License

/**
 * @see com.orange.atk.results.logger.documentGenerator.DocumentGenerator#dumpInStream(boolean, interpreter.logger.DocumentLogger)
 */// w  w w  . jav  a  2s.c  o m

public void dumpInStream(boolean isParseErrorHappened, /*DocumentLogger dl,*/ org.w3c.dom.Document xmlDoc,
        Model model3) {
    long endTime = new Date().getTime();
    // step 1: creation of a document-object
    Document document = new Document();
    PdfWriter writer = null;

    // step 2:
    // we create a writer that listens to the document
    // and directs a PDF-stream to the outputStream
    try {
        writer = PdfWriter.getInstance(document, outputStream);
    } catch (DocumentException e1) {
        e1.printStackTrace();
        return;
    }
    writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
    // step 3: we open the document
    document.open();
    document.addTitle("REPORT");
    document.addCreationDate();

    HeaderFooter headerPage = new HeaderFooter(new Phrase("ScreenShot report"), false);
    HeaderFooter footerPage = new HeaderFooter(new Phrase(" - "), new Phrase(" - "));
    headerPage.setAlignment(Element.ALIGN_CENTER);
    footerPage.setAlignment(Element.ALIGN_CENTER);
    document.setHeader(headerPage);
    document.setFooter(footerPage);

    // Chapter 1 : Summary
    // Section 1 : Informations

    Chunk c = new Chunk("Summary");
    c.setBackground(ORANGE_COLOR, 200, 3f, 200f, 3f);
    c.setFont(FONT_PAR_TITLE);

    Paragraph title1 = new Paragraph(c);
    title1.setAlignment("CENTER");
    title1.setLeading(20);
    Chapter chapter1 = new Chapter(title1, 1);
    chapter1.setNumberDepth(0);

    Paragraph title11 = new Paragraph("Informations");
    Section section1 = chapter1.addSection(title11);
    Paragraph pSum = new Paragraph();
    pSum.add("Author : " + author);
    pSum.add(Chunk.NEWLINE);
    pSum.add("Group : " + group);
    pSum.add(Chunk.NEWLINE);
    pSum.add("Script : " + script);
    pSum.add(Chunk.NEWLINE);
    pSum.add("Reference directory: " + model3.getRefDirectory());
    pSum.add(Chunk.NEWLINE);
    pSum.add("Test directory: " + model3.getTestDirectory());
    pSum.add(Chunk.NEWLINE);
    SimpleDateFormat formatter = new SimpleDateFormat("MMMMM dd, yyyy - hh:mm aaa");
    String dateString = formatter.format(endTime);
    pSum.add("Date : " + dateString);
    pSum.add(Chunk.NEWLINE);
    pSum.add(Chunk.NEWLINE);
    if (model3.getNbFail() > 0) {
        pSum.add(model3.getNbFail() + "/" + model3.getNbImages() + " images didn't succeed the test.");
    } else {
        pSum.add("All images (" + model3.getNbImages() + ") have succeed this comparaison.");
    }
    pSum.add(Chunk.NEWLINE);
    pSum.setIndentationLeft(20);
    section1.add(pSum);
    section1.add(new Paragraph(Chunk.NEXTPAGE));

    try {
        document.add(chapter1);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    document.newPage();

    if (isParseErrorHappened) {
        document.close();
        return;
    }

    // Add generated pictures
    Chunk c3 = new Chunk("ScreenShots");
    c3.setBackground(ORANGE_COLOR, 200, 3f, 200f, 3f);
    c3.setFont(FONT_PAR_TITLE);
    Paragraph p3 = new Paragraph(c3);
    p3.setAlignment("CENTER");
    p3.setLeading(20);
    Chapter chapter3 = new Chapter(p3, 1);
    chapter3.setNumberDepth(0);

    NodeList imgs = xmlDoc.getElementsByTagName("img");
    for (int i = 0; i < imgs.getLength(); i++) {
        org.w3c.dom.Element eImg = (org.w3c.dom.Element) imgs.item(i);
        if (eImg.getElementsByTagName("Pass").item(0).getTextContent().equals(Model.FAIL)) {

            org.w3c.dom.Element eRef = (org.w3c.dom.Element) eImg.getElementsByTagName("Ref").item(0);
            org.w3c.dom.Element eTest = (org.w3c.dom.Element) eImg.getElementsByTagName("Test").item(0);
            Paragraph pScreen = new Paragraph(eTest.getAttributes().getNamedItem("name").getNodeValue() + " "
                    + eImg.getElementsByTagName("Pass").item(0).getTextContent());
            Section section31 = chapter3.addSection(pScreen);
            PdfPTable scTable = null;
            scTable = new PdfPTable(2);
            scTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            scTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            scTable.getDefaultCell().setPadding(5);
            if (new File(eRef.getTextContent()).exists()) {
                Image refImg;
                try {
                    refImg = Image.getInstance(eRef.getTextContent());
                    refImg.scaleToFit(310, 260);
                    refImg.setAlignment(Element.ALIGN_BASELINE);
                    scTable.addCell(refImg);
                } catch (BadElementException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (DOMException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (new File(eTest.getTextContent()).exists()) {
                Image testImg;
                try {
                    BufferedImage bi = ImageIO.read(new File(eTest.getTextContent()));
                    Graphics2D g2d = (Graphics2D) bi.getGraphics();
                    g2d.setStroke(new BasicStroke(1.5f));
                    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));

                    Boolean2D dif = model3.getCouplesComparaison().get(i).getDifWithMask();
                    //               if (squarable){
                    g2d.setColor(Color.red);
                    int w = bi.getWidth();
                    int h = bi.getHeight();
                    //TODO: Gurvan, Maybe we need to have the list of masks in the report?
                    Mask mask = model3.getCouplesComparaison().get(i).getMaskSum();
                    //g2d.drawRect(0, 0, mask.getWidth()*2*sampleWidth, mask.getHeight()*2*sampleHeight);
                    for (int x = 0; x < mask.getWidth(); x++) {

                        for (int y = 0; y < mask.getHeight(); y++) {

                            if (mask.getCell(x, y)) {
                                g2d.setColor(Color.blue);
                                ////Logger.getLogger(this.getClass() ).debug("grise");
                                g2d.fillRect(x * 2 * Mask.getCELL_HALF_SIZE(), y * 2 * Mask.getCELL_HALF_SIZE(),
                                        2 * Mask.getCELL_HALF_SIZE(), 2 * Mask.getCELL_HALF_SIZE());
                            }

                            if (!dif.get(x, y)) {
                                g2d.setColor(Color.green);
                                g2d.fillRect(x * 2 * Mask.getCELL_HALF_SIZE(), y * 2 * Mask.getCELL_HALF_SIZE(),
                                        2 * Mask.getCELL_HALF_SIZE(), 2 * Mask.getCELL_HALF_SIZE());
                            }
                        }
                    }
                    testImg = Image.getInstance(bi, null);
                    testImg.scaleToFit(310, 260);
                    testImg.setAlignment(Element.ALIGN_BASELINE);
                    scTable.addCell(testImg);
                    section31.add(Chunk.NEWLINE);
                    section31.add(scTable);
                } catch (BadElementException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (DOMException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            section31.add(new Paragraph("Reference screenshot description : "
                    + eImg.getElementsByTagName("RefDescription").item(0).getTextContent()));
            section31.add(Chunk.NEWLINE);

            section31
                    .add(new Paragraph("Mask : " + eImg.getElementsByTagName("Mask").item(0).getTextContent()));
            section31.add(Chunk.NEWLINE);

            section31.add(new Paragraph(
                    "Comment : " + eImg.getElementsByTagName("Comment").item(0).getTextContent()));
            section31.add(new Paragraph(Chunk.NEXTPAGE));

        }
    }
    try {
        document.add(chapter3);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    document.newPage();

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

From source file:com.parakhcomputer.util.pdf.FirstPdf.java

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

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

    PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);/*from  w ww .  ja  va  2  s .  c  o m*/

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

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

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

    subCatPart.add(table);

}

From source file:com.prime.location.billing.InvoicedBillingManager.java

/**
 * Print invoice/*w  w w  .ja v a  2s.com*/
 */
public void printInvoice() {
    try { //catch better your exceptions, this is just an example
        FacesContext context = FacesContext.getCurrentInstance();

        Document pdf = new Document(PageSize.A4, 5f, 5f, 75f, 45f);

        String fileName = "PDFFile";

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(pdf, baos);
        TableHeader event = new TableHeader();
        event.setHeader("Header Here");
        writer.setPageEvent(event);

        if (!pdf.isOpen()) {
            pdf.open();
        }

        PdfPCell cell;

        PdfPTable header = new PdfPTable(new float[] { 1, 2, 1 });
        cell = new PdfPCell(new Paragraph("INVOICE",
                FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLDITALIC, Color.BLACK)));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setBorder(Rectangle.NO_BORDER);
        header.addCell(cell);
        header.setTotalWidth(527);
        header.setLockedWidth(true);

        cell = new PdfPCell(new Phrase("Agency: " + selectedAgency.getDescription()));
        cell.setColspan(2);
        cell.setBorder(Rectangle.NO_BORDER);
        header.addCell(cell);

        cell = new PdfPCell(new Phrase("Invoice#: " + selectedInvoice.getId()));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        header.addCell(cell);

        cell = new PdfPCell(
                new Phrase("Date: " + DateUtility.dateTimeFormat(selectedInvoice.getInvoiceDate())));
        cell.setColspan(3);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        header.addCell(cell);

        pdf.add(header);

        PdfPTable table = new PdfPTable(new float[] { 1, 3, 2, 1 });
        table.setSpacingBefore(15f);

        table.setTotalWidth(527);
        table.setLockedWidth(true);
        //table.setWidths(new int[]{3, 1, 1});

        table.getDefaultCell().setBackgroundColor(Color.LIGHT_GRAY);

        table.addCell("Date");
        table.addCell("Name");
        table.addCell("Service");
        table.addCell("Rate");

        table.getDefaultCell().setBackgroundColor(null);

        cell = new PdfPCell(new Phrase("Total(US$): "));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setColspan(3);
        cell.setBackgroundColor(Color.LIGHT_GRAY);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(new DecimalFormat("###,###.###").format(selectedInvoice.getAmount())));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setBackgroundColor(Color.LIGHT_GRAY);
        table.addCell(cell);

        // There are three special rows
        table.setHeaderRows(2);
        // One of them is a footer
        table.setFooterRows(1);
        Font f = FontFactory.getFont(FontFactory.HELVETICA, 10);
        //add remaining
        for (AgencyBilling billing : selectedInvoice.getBillings()) {
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
            table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.getDefaultCell().setIndent(2);
            table.getDefaultCell().setFixedHeight(20);

            table.addCell(new Phrase(DateUtility.dateFormat(billing.getBillingDate()), f));
            table.addCell(new Phrase(billing.getPerson().getName(), f));
            table.addCell(new Phrase(billing.getTariff().getTariffType().getDescription(), f));
            cell = new PdfPCell(new Phrase(new DecimalFormat("###,###.###").format(billing.getRate()), f));
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);

            table.addCell(cell);

        }

        pdf.add(table);

        writer.getAcroForm().setNeedAppearances(true);

        //document.add(new Phrase(TEXT));
        //Keep modifying your pdf file (add pages and more)
        pdf.close();

        writePDFToResponse(context.getExternalContext(), baos, fileName);

        context.responseComplete();

    } catch (Exception e) {
        //e.printStackTrace();          
    }
}

From source file:com.prime.report.template.TableHeader.java

/**
 * Adds a header to every page/*w w w  . jav  a2s .  c  o m*/
 *
 * @param writer
 * @param document
 * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
 * com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
 */
@Override
public void onEndPage(PdfWriter writer, Document document) {

    PdfPTable table = new PdfPTable(4);
    try {
        table.setWidths(new int[] { 10, 11, 9, 1 });
        table.setTotalWidth(527);
        table.setLockedWidth(true);
        table.getDefaultCell().setFixedHeight(20);
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);

        String logoPath = "/resources/image/logo.png";
        ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
        String logo = servletContext.getRealPath(logoPath);
        Image img = Image.getInstance(logo);

        table.addCell(Image.getInstance(img));

        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(new Phrase("New South West Facility Center",
                FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLDITALIC, Color.BLACK)));
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(String.format("Page %d of", writer.getPageNumber()));
        PdfPCell cell = new PdfPCell(Image.getInstance(total));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        table.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent());
    } catch (DocumentException de) {
        throw new ExceptionConverter(de);
    } catch (IOException ex) {
        Logger.getLogger(TableHeader.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.pureinfo.srm.patent.action.PatentPrintPdfAction.java

License:Open Source License

/**
 * @see com.pureinfo.ark.interaction.ActionBase#executeAction()
 *///  ww w .  j ava  2s.c  o m
public ActionForward executeAction() throws PureException {
    int nYear = request.getRequiredInt("year", "");

    Rectangle rectPageSize = new Rectangle(PageSize.A4);
    rectPageSize.setBackgroundColor(Color.WHITE);
    rectPageSize.setBorderColor(Color.BLACK);
    rectPageSize = rectPageSize.rotate();
    Document doc = new Document(rectPageSize, 10, 10, 10, 10);
    doc.addTitle(nYear + "");
    doc.addAuthor("PureInfo");
    try {
        BaseFont bfontTitle = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontTitle = new Font(bfontTitle, 18, Font.NORMAL);
        Paragraph paraTitle = new Paragraph(nYear + "",
                fontTitle);
        paraTitle.setAlignment(ElementTags.ALIGN_CENTER);

        BaseFont bfontContent = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontContent = new Font(bfontContent, 10, Font.NORMAL);

        FileFactory fileFactory = FileFactory.getInstance();
        String sPath = fileFactory.lookupPathConfigByFlag(FileFactory.FLAG_DOWNLOADTEMP, true).getLocalPath();

        FileUtil.insurePathExists(sPath);
        PdfWriter.getInstance(doc, new FileOutputStream(new File(sPath, "patent.pdf")));

        doc.open();
        doc.add(paraTitle);
        doc.add(new Paragraph("   "));

        String[] arrTitles = new String[] { "", "", "", "", "", "",
                "", "", "" };
        PdfPTable pTable = new PdfPTable(arrTitles.length);
        pTable.setWidths(new int[] { 5, 10, 9, 9, 20, 15, 12, 10, 10 });

        for (int i = 0; i < arrTitles.length; i++) {
            PdfPCell pCell = new PdfPCell();
            Paragraph para = new Paragraph(arrTitles[i], fontContent);
            para.setAlignment(ElementTags.ALIGN_CENTER);

            pCell.addElement(para);
            pTable.addCell(pCell);
        }

        /**
         * 
         */
        IPatentMgr mgr = (IPatentMgr) ArkContentHelper.getContentMgrOf(Patent.class);
        List patents = mgr.findAllAuthorizedOf(nYear);
        int i = 0;
        for (Iterator iter = patents.iterator(); iter.hasNext(); i++) {
            Patent patent = (Patent) iter.next();
            this.addPatentCell(pTable, String.valueOf(i + 1), fontContent);
            this.addPatentCell(pTable, patent.getPatentSid(), fontContent);
            this.addPatentCell(pTable, ForceConstants.DATE_FORMAT.format(patent.getApplyDate()), fontContent);
            this.addPatentCell(pTable, patent.getWarrantDate() == null ? ""
                    : ForceConstants.DATE_FORMAT.format(patent.getWarrantDate()), fontContent);
            this.addPatentCell(pTable, patent.getName(), fontContent);
            this.addPatentCell(pTable, patent.getAllAuthosName(), fontContent);
            this.addPatentCell(pTable, patent.getRightPerson(), fontContent);
            this.addPatentCell(pTable, getCollegeName(patent), fontContent);
            this.addPatentCell(pTable, patent.getPatentTypeName(), fontContent);
        }

        doc.add(pTable);
        doc.close();

    } catch (DocumentException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace(System.err);
    } catch (IOException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace(System.err);
    }

    List list = new ArrayList();
    list.add(new Pair("/download/patent.pdf", ""));
    request.setAttribute("forward", list);
    return mapping.findForward("success");
}

From source file:com.qcadoo.mes.deliveries.print.DeliveryReportPdf.java

License:Open Source License

private void createHeaderTable(final Document document, final Entity delivery, final Locale locale)
        throws DocumentException {
    PdfPTable dynaminHeaderTable = pdfHelper.createPanelTable(3);

    dynaminHeaderTable.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP);

    PdfPTable firstColumnHeaderTable = new PdfPTable(1);
    PdfPTable secondColumnHeaderTable = new PdfPTable(1);
    PdfPTable thirdColumnHeaderTable = new PdfPTable(1);

    setSimpleFormat(firstColumnHeaderTable);
    setSimpleFormat(secondColumnHeaderTable);
    setSimpleFormat(thirdColumnHeaderTable);

    dynaminHeaderTable.setSpacingBefore(7);

    Map<String, Object> firstColumn = createFirstColumn(delivery);
    Map<String, Object> secondColumn = createSecondColumn(delivery);
    Map<String, Object> thirdColumn = createThirdColumn(delivery, locale);

    int maxSize = pdfHelper.getMaxSizeOfColumnsRows(Lists.newArrayList(
            Integer.valueOf(firstColumn.values().size()), Integer.valueOf(secondColumn.values().size()),
            Integer.valueOf(thirdColumn.values().size())));

    for (int i = 0; i < maxSize; i++) {
        firstColumnHeaderTable = pdfHelper.addDynamicHeaderTableCell(firstColumnHeaderTable, firstColumn,
                locale);//from www. jav a  2  s  .c om
        secondColumnHeaderTable = pdfHelper.addDynamicHeaderTableCell(secondColumnHeaderTable, secondColumn,
                locale);
        thirdColumnHeaderTable = pdfHelper.addDynamicHeaderTableCell(thirdColumnHeaderTable, thirdColumn,
                locale);
    }

    dynaminHeaderTable.addCell(firstColumnHeaderTable);
    dynaminHeaderTable.addCell(secondColumnHeaderTable);
    dynaminHeaderTable.addCell(thirdColumnHeaderTable);

    document.add(dynaminHeaderTable);
}