Example usage for com.lowagie.text.pdf PdfPCell setBackgroundColor

List of usage examples for com.lowagie.text.pdf PdfPCell setBackgroundColor

Introduction

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

Prototype


public void setBackgroundColor(Color backgroundColor) 

Source Link

Document

Sets the backgroundcolor of the rectangle.

Usage

From source file:classroom.filmfestival_c.Movies17.java

@SuppressWarnings("unchecked")
protected static Element getTable(Query q, Date date) throws DocumentException {
    PdfPTable table = new PdfPTable(1);
    table.setWidthPercentage(100f);//  w  w  w.j a va 2 s . c  o  m

    PdfPCell cell = new PdfPCell(new Phrase(date.toString(), NORMALWHITE));
    cell.setBackgroundColor(BLACK);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell);

    q.setDate(0, date);
    java.util.List<FestivalScreening> screenings = q.list();
    for (FestivalScreening screening : screenings) {
        addScreening(table, screening);
    }
    return table;
}

From source file:classroom.filmfestival_c.Movies17.java

protected static Element fullTitle(FestivalScreening screening) throws DocumentException {
    FilmTitle movie = screening.getFilmTitle();
    // a table with 3 cells
    PdfPTable table = new PdfPTable(3);
    table.setWidths(INNER);//  w ww  .  j ava2  s.  c o m
    table.setWidthPercentage(100);

    // the title(s)
    Paragraph p = new Paragraph();
    p.add(new Phrase(movie.getTitle(), BOLD));
    p.setLeading(16);
    // maybe an alternative title
    if (movie.getATitle().trim().length() > 0) {
        p.add(new Phrase(" (" + movie.getATitle() + ")"));
    }
    PdfPCell cell = new PdfPCell();
    cell.addElement(p);
    cell.setBorder(PdfPCell.NO_BORDER);
    cell.setUseAscender(true);
    cell.setUseDescender(true);
    table.addCell(cell);

    // eXploreZone?
    cell = new PdfPCell();
    cell.setBorder(PdfPCell.NO_BORDER);
    if (isExploreZone(movie)) {
        cell.setBackgroundColor(WHITE);
        cell.setUseAscender(true);
        cell.setUseDescender(true);
        cell.addElement(new Paragraph("eXplore"));
    }
    table.addCell(cell);

    // Duration / shortfilm
    cell = new PdfPCell();
    cell.setBorder(PdfPCell.NO_BORDER);
    cell.setBackgroundColor(WHITE);
    cell.setUseAscender(true);
    cell.setUseDescender(true);
    StringBuffer buf = new StringBuffer();
    buf.append(movie.getDuration());
    buf.append('\'');
    if (getExtra(screening) > 0) {
        buf.append(" + KF");
        buf.append(getExtra(screening));
        buf.append('\'');
    }
    p = new Paragraph(buf.toString());
    p.setAlignment(Element.ALIGN_CENTER);
    cell.addElement(p);
    table.addCell(cell);
    return table;
}

From source file:classroom.filmfestival_c.Movies17.java

@SuppressWarnings("unchecked")
protected static void setColor(FilmTitle movie, PdfPCell cell) {
    Set<FestivalEntry> entries = movie.getFestivalEntries();
    for (FestivalEntry entry : entries) {
        if (entry.getId().getYear() == YEAR) {
            cell.setBackgroundColor(COLOR[entry.getFilmCategory().getCategoryId()]);
            return;
        }//from   w  ww .ja  v a2 s.c om
    }
}

From source file:com.actelion.research.spiritapp.ui.util.PDFUtils.java

License:Open Source License

public static void convertHSSF2Pdf(Workbook wb, String header, File reportFile) throws Exception {
    assert wb != null;
    assert reportFile != null;

    //Precompute formula
    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);/*w  w  w .j ava2 s  . c  o m*/

        for (Row r : sheet) {
            for (Cell c : r) {
                if (c.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
                    try {
                        evaluator.evaluateFormulaCell(c);
                    } catch (Exception e) {
                        System.err.println(e);
                    }
                }
            }
        }
    }

    File tmp = File.createTempFile("tmp_", ".xlsx");
    try (OutputStream out = new BufferedOutputStream(new FileOutputStream(tmp))) {
        wb.write(out);
    }

    //Find page orientation
    int maxColumnsGlobal = 0;
    for (int sheetNo = 0; sheetNo < wb.getNumberOfSheets(); sheetNo++) {
        Sheet sheet = wb.getSheetAt(sheetNo);
        for (Iterator<Row> rowIterator = sheet.iterator(); rowIterator.hasNext();) {
            Row row = rowIterator.next();
            maxColumnsGlobal = Math.max(maxColumnsGlobal, row.getLastCellNum());
        }
    }

    Rectangle pageSize = maxColumnsGlobal < 10 ? PageSize.A4 : PageSize.A4.rotate();
    Document pdfDocument = new Document(pageSize, 10f, 10f, 20f, 20f);

    PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(reportFile));
    addHeader(writer, header);
    pdfDocument.open();
    //we have two columns in the Excel sheet, so we create a PDF table with two columns
    //Note: There are ways to make this dynamic in nature, if you want to.
    //Loop through sheets
    for (int sheetNo = 0; sheetNo < wb.getNumberOfSheets(); sheetNo++) {
        Sheet sheet = wb.getSheetAt(sheetNo);

        //Loop through rows, to find number of columns
        int minColumns = 1000;
        int maxColumns = 0;
        for (Iterator<Row> rowIterator = sheet.iterator(); rowIterator.hasNext();) {
            Row row = rowIterator.next();
            if (row.getFirstCellNum() >= 0)
                minColumns = Math.min(minColumns, row.getFirstCellNum());
            if (row.getLastCellNum() >= 0)
                maxColumns = Math.max(maxColumns, row.getLastCellNum());
        }
        if (maxColumns == 0)
            continue;

        //Loop through first rows, to find relative width
        float[] widths = new float[maxColumns];
        int totalWidth = 0;
        for (int c = 0; c < maxColumns; c++) {
            int w = sheet.getColumnWidth(c);
            widths[c] = w;
            totalWidth += w;
        }

        for (int c = 0; c < maxColumns; c++) {
            widths[c] /= totalWidth;
        }

        //Create new page and a new chapter with the sheet's name
        if (sheetNo > 0)
            pdfDocument.newPage();
        Chapter pdfSheet = new Chapter(sheet.getSheetName(), sheetNo + 1);

        PdfPTable pdfTable = null;
        PdfPCell pdfCell = null;
        boolean inTable = false;

        //Loop through cells, to create the content
        //         boolean leftBorder = true;
        //         boolean[] topBorder = new boolean[maxColumns+1];
        for (int r = 0; r <= sheet.getLastRowNum(); r++) {
            Row row = sheet.getRow(r);

            //Check if we exited a table (empty line)
            if (row == null) {
                if (pdfTable != null) {
                    addTable(pdfDocument, pdfSheet, totalWidth, widths, pdfTable);
                    pdfTable = null;
                }
                inTable = false;
                continue;
            }

            //Check if we start a table (>MIN_COL_IN_TABLE columns)
            if (row.getLastCellNum() >= MIN_COL_IN_TABLE) {
                inTable = true;
            }

            if (!inTable) {
                //Process the data outside table, just add the text
                boolean hasData = false;
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    if (cell.getCellType() == Cell.CELL_TYPE_BLANK)
                        continue;
                    Chunk chunk = getChunk(wb, cell);
                    pdfSheet.add(chunk);
                    pdfSheet.add(new Chunk(" "));
                    hasData = true;
                }
                if (hasData)
                    pdfSheet.add(Chunk.NEWLINE);

            } else {
                //Process the data in table
                if (pdfTable == null) {
                    //Create table
                    pdfTable = new PdfPTable(maxColumns);
                    pdfTable.setWidths(widths);
                    //                  topBorder = new boolean[maxColumns+1];
                }

                int cellNumber = minColumns;
                //               leftBorder = false;
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {

                    Cell cell = cellIterator.next();

                    for (; cellNumber < cell.getColumnIndex(); cellNumber++) {
                        pdfCell = new PdfPCell();
                        pdfCell.setBorder(0);
                        pdfTable.addCell(pdfCell);
                    }

                    Chunk phrase = getChunk(wb, cell);
                    pdfCell = new PdfPCell(new Phrase(phrase));
                    pdfCell.setFixedHeight(row.getHeightInPoints() - 3);
                    pdfCell.setNoWrap(!cell.getCellStyle().getWrapText());
                    pdfCell.setPaddingLeft(1);
                    pdfCell.setHorizontalAlignment(
                            cell.getCellStyle().getAlignment() == CellStyle.ALIGN_CENTER ? PdfPCell.ALIGN_CENTER
                                    : cell.getCellStyle().getAlignment() == CellStyle.ALIGN_RIGHT
                                            ? PdfPCell.ALIGN_RIGHT
                                            : PdfPCell.ALIGN_LEFT);
                    pdfCell.setUseBorderPadding(false);
                    pdfCell.setUseVariableBorders(false);
                    pdfCell.setBorderWidthRight(cell.getCellStyle().getBorderRight() == 0 ? 0 : .5f);
                    pdfCell.setBorderWidthBottom(cell.getCellStyle().getBorderBottom() == 0 ? 0
                            : cell.getCellStyle().getBorderBottom() > 1 ? 1 : .5f);
                    pdfCell.setBorderWidthLeft(cell.getCellStyle().getBorderLeft() == 0 ? 0
                            : cell.getCellStyle().getBorderLeft() > 1 ? 1 : .5f);
                    pdfCell.setBorderWidthTop(cell.getCellStyle().getBorderTop() == 0 ? 0
                            : cell.getCellStyle().getBorderTop() > 1 ? 1 : .5f);
                    String color = cell.getCellStyle().getFillForegroundColorColor() == null ? null
                            : ((XSSFColor) cell.getCellStyle().getFillForegroundColorColor()).getARGBHex();
                    if (color != null)
                        pdfCell.setBackgroundColor(new Color(Integer.decode("0x" + color.substring(2))));
                    pdfTable.addCell(pdfCell);
                    cellNumber++;
                }
                for (; cellNumber < maxColumns; cellNumber++) {
                    pdfCell = new PdfPCell();
                    pdfCell.setBorder(0);
                    pdfTable.addCell(pdfCell);
                }
            }

            //Custom code to add all images on the first sheet (works for reporting)
            if (sheetNo == 0 && row.getRowNum() == 0) {
                for (PictureData pd : wb.getAllPictures()) {
                    try {
                        Image pdfImg = Image.getInstance(pd.getData());
                        pdfImg.scaleToFit(
                                pageSize.getWidth() * .8f - pageSize.getBorderWidthLeft()
                                        - pageSize.getBorderWidthRight(),
                                pageSize.getHeight() * .8f - pageSize.getBorderWidthTop()
                                        - pageSize.getBorderWidthBottom());
                        pdfSheet.add(pdfImg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        if (pdfTable != null) {
            addTable(pdfDocument, pdfSheet, totalWidth, widths, pdfTable);
        }

        pdfDocument.add(pdfSheet);
    }
    pdfDocument.close();

}

From source file:com.afunms.report.abstraction.ExcelReport1.java

/**
 * @author wxy pdf//from  w ww.j  av  a2s  .  co  m
 * @param filename
 * @date 2010-3-27
 */
public void createReport_ServiceEventPdf(String filename) throws IOException {

    if (impReport.getTable() == null) {
        fileName = null;
        return;
    }
    try {
        // 
        Document document = new Document(PageSize.A4);
        // (Writer)document(Writer)
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        // 
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        // 
        Font titleFont = new Font(bfChinese, 12, Font.BOLD);
        // 
        Font contextFont = new Font(bfChinese, 12, Font.NORMAL);

        String starttime = (String) reportHash.get("starttime");
        String totime = (String) reportHash.get("totime");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List eventlist = new ArrayList();
        eventlist = (List) reportHash.get("eventlist");
        String ip = "";
        String sum = "";
        String levelone = "";
        String leveltwo = "";
        String levelthree = "";
        String servicename = "";
        if (eventlist != null && eventlist.size() > 0) {

            List _eventlist = (List) eventlist.get(0);
            if (_eventlist != null && _eventlist.size() > 0) {
                ip = (String) _eventlist.get(0);
                servicename = (String) _eventlist.get(1);
                sum = (String) _eventlist.get(2);
                levelone = (String) _eventlist.get(3);
                leveltwo = (String) _eventlist.get(4);
                levelthree = (String) _eventlist.get(5);
            }
        }

        Paragraph title = new Paragraph(servicename + "", titleFont);
        // 
        title.setAlignment(Element.ALIGN_CENTER);
        // title.setFont(titleFont);
        document.add(title);

        String contextString = ":" + impReport.getTimeStamp() + " \n"// 
                + ":" + starttime + "  " + totime;

        Paragraph context = new Paragraph(contextString, contextFont);
        // 
        context.setAlignment(Element.ALIGN_LEFT);
        // context.setFont(contextFont);
        // 
        context.setSpacingBefore(6);
        // 
        context.setFirstLineIndent(6);
        document.add(context);
        document.add(new Paragraph("\n"));

        PdfPTable servTable = new PdfPTable(6);
        float[] cellWidths = { 220f, 220f, 220f, 220f, 220f, 220f };
        servTable.setWidths(cellWidths);
        servTable.setWidthPercentage(100);
        PdfPCell cell = null;
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setColspan(6);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        cell = new PdfPCell(new Phrase("IP", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        cell = new PdfPCell(new Phrase("()", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        cell = new PdfPCell(new Phrase("()", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        cell = new PdfPCell(new Phrase("()", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        cell = new PdfPCell(new Phrase("()", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        cell = new PdfPCell(new Phrase(ip, contextFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        cell = new PdfPCell(new Phrase(servicename, contextFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        cell = new PdfPCell(new Phrase(sum, contextFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        cell = new PdfPCell(new Phrase(levelone, contextFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        cell = new PdfPCell(new Phrase(leveltwo, contextFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        cell = new PdfPCell(new Phrase(levelthree, contextFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        servTable.addCell(cell);
        document.add(servTable);
        document.add(new Paragraph("\n"));

        // 
        PdfPTable eventTable = new PdfPTable(6);
        float[] eventWidths = { 220f, 220f, 220f, 220f, 220f, 220f };
        eventTable.setWidths(eventWidths);
        eventTable.setWidthPercentage(100);
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setColspan(6);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        int index = 0;
        java.text.SimpleDateFormat _sdf = new java.text.SimpleDateFormat("MM-dd HH:mm");
        List list = (ArrayList) reportHash.get("list");
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                index++;
                EventList event = (EventList) list.get(i);
                Date cc = event.getRecordtime().getTime();
                Integer eventid = event.getId();
                String eventlocation = event.getEventlocation();
                String content = event.getContent();
                String level = String.valueOf(event.getLevel1());
                String status = String.valueOf(event.getManagesign());
                String s = status;
                String showlevel = null;
                String act = "";
                if ("1".equals(level)) {
                    showlevel = "";
                } else if ("2".equals(level)) {
                    showlevel = "";
                } else {
                    showlevel = "";
                }
                if ("0".equals(status)) {
                    status = "";
                }
                if ("1".equals(status)) {
                    status = "";
                }
                if ("2".equals(status)) {
                    status = "";
                }
                String rptman = event.getReportman();
                String rtime1 = _sdf.format(cc);
                cell = new PdfPCell(new Phrase(String.valueOf(index), contextFont));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                eventTable.addCell(cell);
                if ("3".equals(level)) {
                    cell = new PdfPCell(new Phrase(showlevel, contextFont));
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                    cell.setBackgroundColor(Color.red);
                    eventTable.addCell(cell);
                } else if ("2".equals(level)) {
                    cell = new PdfPCell(new Phrase(showlevel, contextFont));
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                    cell.setBackgroundColor(Color.orange);
                    eventTable.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(showlevel, contextFont));
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                    cell.setBackgroundColor(Color.yellow);
                    eventTable.addCell(cell);
                }
                cell = new PdfPCell(new Phrase(content, contextFont));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                eventTable.addCell(cell);
                cell = new PdfPCell(new Phrase(rtime1, contextFont));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                eventTable.addCell(cell);
                cell = new PdfPCell(new Phrase(rptman, contextFont));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                eventTable.addCell(cell);
                cell = new PdfPCell(new Phrase(status, contextFont));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                eventTable.addCell(cell);
            }
        }
        document.add(eventTable);
        document.close();
    } catch (Exception e) {
        // SysLogger.error("Error in ExcelReport.createReport()",e);
        SysLogger.error("", e);
    }

}

From source file:com.afunms.report.abstraction.ExcelReport1.java

/**
 * @author HONGLI pdf//from ww w. j  av a 2s. c  om
 * @param filename
 * @throws DocumentException
 * @throws IOException
 */
public void createReport_EventPdf(String filename) throws DocumentException, IOException {
    if (impReport.getTable() == null) {
        fileName = null;
        return;
    }
    try {
        // 
        Document document = new Document(PageSize.A4);
        // (Writer)document(Writer)
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        // 
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        // 
        Font titleFont = new Font(bfChinese, 12, Font.BOLD);
        // 
        Font contextFont = new Font(bfChinese, 12, Font.NORMAL);
        String ip = (String) reportHash.get("ip");
        String typename = (String) reportHash.get("typename");
        DBVo vo = (DBVo) reportHash.get("vo");
        String downnum = (String) reportHash.get("downnum");
        String count = (Integer) reportHash.get("count") + "";
        String Ping = (String) reportHash.get("Ping");
        String starttime = (String) reportHash.get("starttime");
        String totime = (String) reportHash.get("totime");
        Hashtable maxping = (Hashtable) reportHash.get("ping");
        String hostname = (String) reportHash.get("dbname");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newip = doip(ip);
        Paragraph title = new Paragraph(hostname + "", titleFont);
        // 
        title.setAlignment(Element.ALIGN_CENTER);
        // title.setFont(titleFont);
        document.add(title);

        String contextString = ":" + impReport.getTimeStamp() + " \n"// 
                + ":" + starttime + "  " + totime;

        Paragraph context = new Paragraph(contextString, contextFont);
        // 
        context.setAlignment(Element.ALIGN_LEFT);
        // context.setFont(contextFont);
        // 
        context.setSpacingBefore(5);
        // 
        context.setFirstLineIndent(5);
        document.add(context);
        document.add(new Paragraph("\n"));
        /*
         * tmpLabel = new Label(0, 1, ":" + impReport.getTimeStamp());
         * sheet.addCell(tmpLabel); tmpLabel = new Label(0, 2, ": " +
         * starttime + "  " + totime);
         */
        // 
        PdfPTable dbTable = new PdfPTable(5);
        float[] cellWidths = { 220f, 220f, 220f, 220f, 220f };
        dbTable.setWidths(cellWidths);
        dbTable.setWidthPercentage(100);
        PdfPCell cell = null;
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setColspan(5);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        dbTable.addCell(cell);
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        dbTable.addCell(cell);
        cell = new PdfPCell(new Phrase("IP", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        dbTable.addCell(cell);
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        dbTable.addCell(cell);
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        dbTable.addCell(cell);
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        dbTable.addCell(cell);
        cell = new PdfPCell(new Phrase(vo.getDbName(), contextFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        dbTable.addCell(cell);
        cell = new PdfPCell(new Phrase(ip, contextFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        dbTable.addCell(cell);
        cell = new PdfPCell(new Phrase(typename, contextFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        dbTable.addCell(cell);
        cell = new PdfPCell(new Phrase(downnum, contextFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        dbTable.addCell(cell);
        cell = new PdfPCell(new Phrase(count, contextFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        dbTable.addCell(cell);
        document.add(dbTable);
        document.add(new Paragraph("\n"));

        // 
        PdfPTable eventTable = new PdfPTable(6);
        float[] eventWidths = { 220f, 220f, 220f, 220f, 220f, 220f };
        eventTable.setWidths(eventWidths);
        eventTable.setWidthPercentage(100);
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setColspan(6);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        cell = new PdfPCell(new Phrase("", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", titleFont));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        eventTable.addCell(cell);
        int index = 0;
        java.text.SimpleDateFormat _sdf = new java.text.SimpleDateFormat("MM-dd HH:mm");
        List list = (ArrayList) reportHash.get("list");
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                index++;
                EventList eventlist = (EventList) list.get(i);
                Date cc = eventlist.getRecordtime().getTime();
                Integer eventid = eventlist.getId();
                String eventlocation = eventlist.getEventlocation();
                String content = eventlist.getContent();
                String level = String.valueOf(eventlist.getLevel1());
                String status = String.valueOf(eventlist.getManagesign());
                String s = status;
                String showlevel = null;
                String act = "";
                if ("1".equals(level)) {
                    showlevel = "";
                } else if ("2".equals(level)) {
                    showlevel = "";
                } else {
                    showlevel = "";
                }
                if ("0".equals(status)) {
                    status = "";
                }
                if ("1".equals(status)) {
                    status = "";
                }
                if ("2".equals(status)) {
                    status = "";
                }
                String rptman = eventlist.getReportman();
                String rtime1 = _sdf.format(cc);
                cell = new PdfPCell(new Phrase(String.valueOf(index), contextFont));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                eventTable.addCell(cell);
                if ("3".equals(level)) {
                    cell = new PdfPCell(new Phrase(showlevel, contextFont));
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                    cell.setBackgroundColor(Color.red);
                    eventTable.addCell(cell);
                } else if ("2".equals(level)) {
                    cell = new PdfPCell(new Phrase(showlevel, contextFont));
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                    cell.setBackgroundColor(Color.orange);
                    eventTable.addCell(cell);
                } else {
                    cell = new PdfPCell(new Phrase(showlevel, contextFont));
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                    cell.setBackgroundColor(Color.yellow);
                    eventTable.addCell(cell);
                }
                cell = new PdfPCell(new Phrase(content, contextFont));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                eventTable.addCell(cell);
                cell = new PdfPCell(new Phrase(rtime1, contextFont));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                eventTable.addCell(cell);
                cell = new PdfPCell(new Phrase(rptman, contextFont));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                eventTable.addCell(cell);
                cell = new PdfPCell(new Phrase(status, contextFont));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                eventTable.addCell(cell);
            }
        }

        document.add(eventTable);
        document.close();
    } catch (Exception e) {
        // SysLogger.error("Error in ExcelReport.createReport()",e);
        SysLogger.error("", e);
    }
}

From source file:com.aryjr.nheengatu.pdf.PDFTable.java

License:Open Source License

private static float extractVisibleComponents(final Tag tag, final PdfPCell cell, final Paragraph paragraph,
        float cellWidth) throws DocumentException {
    final Iterator tags = tag.tags();// tags inside the cell
    Object component;// www  .j  av  a  2s  .c o  m
    final TagsManager tm = TagsManager.getInstance();
    // Seting the HTML row or table background color
    if (tag.getName().equalsIgnoreCase("TD")) {
        cell.setBorderColor(tm.getPreviousState(2).getBgcolor());
        cell.setBorderWidth(tag.getPropertyValue(PDFTable.CELLSPACING) == null ? 1
                : Integer.parseInt(tag.getPropertyValue(PDFTable.CELLSPACING)));
        //cell.setBackgroundColor(tm.getBgcolor());
        cell.setBackgroundColor(new Color(255, 255, 255));
        cell.setVerticalAlignment(tm.getValign());
        cell.setHorizontalAlignment(tm.getAlign());
        paragraph.setAlignment(tm.getAlign());
    }
    while (tags.hasNext()) {
        component = tags.next();
        if (component instanceof Text) {
            String s = ((Text) component).getText();
            if (s.contains("\\\"")) {
                s = s.replace("\\\"", "\"");
                ((Text) component).setText(s);
            }
            // If it's a text, create a iText text component for it
            paragraph.add(PDFText.createChunk((Text) component));
            cellWidth += PDFText.getWidth((Text) component);
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("br")) {
            // If it's a HTML line break
            paragraph.add("\n");
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("img")) {
            // If it's a HTML image, create a iText image component for it
            try {
                final Image img = PDFImage.createImage((Tag) component);
                //Nato: paragraph.add(new Chunk(img, 0, -2));
                paragraph.add(new Chunk(img, 0, 0));
                cellWidth += img.getScaledWidth();
                // TODO Is there an iText bug here?
                //if (img.scaledHeight() == 1f) {
                //cell.setFixedHeight(img.getScaledHeight());
                //}
            } catch (final Exception e) {
                e.printStackTrace();
            }
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("table")) {
            // If it's a HTML table, create a iText table component for it
            try {
                // TODO the default value of nowrap here will be true or
                // false??? If true, there is a problem with the cell width
                cell.setNoWrap(false);
                final PDFTable t = PDFTable.createTable((Tag) component);
                cell.addElement(t);
                final float[] cw = t.getWidths();
                for (final float element : cw) {
                    cellWidth += element;
                }
            } catch (final Exception e) {
                e.printStackTrace();
            }
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("select")) {
            // If it's a HTML select field, I will get only the selected
            // option
            final Tag select = (Tag) component;
            Tag opt;
            tm.checkTag(select);
            final Iterator opts = select.tags();
            boolean selected = false;
            while (opts.hasNext()) {
                opt = (Tag) opts.next();
                if (opt.getPropertyValue("selected") != null) {
                    tm.checkTag(opt);
                    cellWidth = PDFTable.extractVisibleComponents(opt, cell, paragraph, cellWidth);
                    selected = true;
                    break;
                }
            }
            if (!selected) {
                // if none option have the selected attribute the first will
                // be shown
                opt = select.getFirstTag("option");
                tm.checkTag(opt);
                cellWidth = PDFTable.extractVisibleComponents(opt, cell, paragraph, cellWidth);
            }
        } else {
            // If it's an another tag, check the name and call this method
            // again
            tm.checkTag((Tag) component);
            cellWidth = PDFTable.extractVisibleComponents((Tag) component, cell, paragraph, cellWidth);
            tm.back();
        }
    }
    return cellWidth;
}

From source file:com.aryjr.nheengatu.testes.AbsolutePosition.java

License:Open Source License

private static PdfPTable createTable() {
    final PdfPTable table = new PdfPTable(3);
    table.setTotalWidth(200);//from ww w.j ava 2  s.  co  m
    PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
    cell.setColspan(3);
    table.addCell(cell);
    table.addCell("1.1");
    table.addCell("2.1");
    table.addCell("3.1");
    table.addCell("1.2");
    table.addCell("2.2");
    table.addCell("3.2");
    cell = new PdfPCell(new Paragraph("cell test1"));
    cell.setBorderColor(new Color(255, 0, 0));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("cell test2"));
    cell.setColspan(2);
    cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
    table.addCell(cell);
    return table;
}

From source file:com.bytecode.customexporter.PDFCustomExporter.java

protected PdfPTable exportPDFTable(FacesContext context, DataList list, boolean pageOnly, String encoding) {

    if (!("-".equalsIgnoreCase(encoding))) {
        createCustomFonts(encoding);//from  w ww. ja  v a2  s. c  o m
    }
    int first = list.getFirst();
    int rowCount = list.getRowCount();
    int rowsToExport = first + list.getRows();

    PdfPTable pdfTable = new PdfPTable(1);
    if (list.getHeader() != null) {
        String value = exportValue(FacesContext.getCurrentInstance(), list.getHeader());
        PdfPCell cell = new PdfPCell(new Paragraph((value), this.facetFont));
        if (facetBackground != null) {
            cell.setBackgroundColor(facetBackground);
        }
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfTable.addCell(cell);
        pdfTable.completeRow();

    }

    StringBuilder builder = new StringBuilder();
    String output = null;

    if (pageOnly) {
        output = exportPageOnly(first, list, rowsToExport, builder);
    } else {
        output = exportAll(list, rowCount, builder);
    }

    pdfTable.addCell(new Paragraph(output, cellFont));
    pdfTable.completeRow();

    if (list.getFooter() != null) {
        String value = exportValue(FacesContext.getCurrentInstance(), list.getFooter());
        PdfPCell cell = new PdfPCell(new Paragraph((value), this.facetFont));
        if (facetBackground != null) {
            cell.setBackgroundColor(facetBackground);
        }
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfTable.addCell(cell);
        pdfTable.completeRow();

    }

    return pdfTable;
}

From source file:com.bytecode.customexporter.PDFCustomExporter.java

protected void tableFacet(FacesContext context, PdfPTable pdfTable, DataTable table, int columnCount,
        String facetType) {/*from w ww  .j av a 2 s.  c o  m*/
    Map<String, UIComponent> map = table.getFacets();
    UIComponent component = map.get(facetType);
    if (component != null) {
        String headerValue = null;
        if (component instanceof HtmlCommandButton) {
            headerValue = exportValue(context, component);
        } else if (component instanceof HtmlCommandLink) {
            headerValue = exportValue(context, component);
        } else if (component instanceof UIPanel || component instanceof OutputPanel) {
            String header = "";
            for (UIComponent child : component.getChildren()) {
                headerValue = exportValue(context, child);
                header = header + headerValue;
            }
            PdfPCell cell = new PdfPCell(new Paragraph((header), this.facetFont));
            if (facetBackground != null) {
                cell.setBackgroundColor(facetBackground);
            }
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            //addColumnAlignments(component,cell);
            cell.setColspan(columnCount);
            pdfTable.addCell(cell);
            pdfTable.completeRow();
            return;
        } else {
            headerValue = exportFacetValue(context, component);
        }
        PdfPCell cell = new PdfPCell(new Paragraph((headerValue), this.facetFont));
        if (facetBackground != null) {
            cell.setBackgroundColor(facetBackground);
        }
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        //addColumnAlignments(component,cell);
        cell.setColspan(columnCount);
        pdfTable.addCell(cell);
        pdfTable.completeRow();

    }
}