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:org.unitime.timetable.webutil.timegrid.PdfExamGridTable.java

License:Open Source License

public void printCell(ExamGridModel model, int day, int slot, int idx, int maxIdx, boolean head,
        boolean vertical, boolean in, boolean eod, boolean eol) {
    ExamPeriod period = getPeriod(day, slot);
    ExamGridCell cell = model.getAssignment(period, idx);
    PdfPCell c = createCell();
    c.setBorderWidthTop(head || (!in && !vertical) ? 1 : 0);
    c.setBorderWidthRight(eod || eol ? 1 : 0);
    if (cell == null) {
        String bgColor = model.getBackground(period);
        if (bgColor == null && !model.isAvailable(period))
            bgColor = sBgColorNotAvailable;
        if (period == null)
            bgColor = sBgColorNotAvailable;
        if (bgColor != null)
            c.setBackgroundColor(getColor(bgColor));
        c.setBorderWidthBottom(idx < maxIdx ? 0 : 1);
        addText(c, " ");
    } else {/*  w w  w .  j a  v  a2 s.com*/
        String bgColor = cell.getBackground();
        if (iForm.getBackground() == sBgNone && !sBgColorNotAvailable.equals(bgColor)) {
            if (!model.isAvailable(period))
                bgColor = sBgColorNotAvailableButAssigned;
        }
        if (bgColor != null)
            c.setBackgroundColor(getColor(bgColor));
        addText(c, cell.getName());
        if (iForm.getResource() != sResourceRoom)
            addText(c, cell.getRoomName());
        else
            addText(c, cell.getShortCommentNoColors() == null ? "" : cell.getShortCommentNoColors());
    }
    iPdfTable.addCell(c);
}

From source file:org.unitime.timetable.webutil.timegrid.PdfExamGridTable.java

License:Open Source License

private void addLegendRow(String color, String text) {
    PdfPCell c = createCellNoBorder();
    c.setBorderWidth(1);//from ww  w.java 2s .  c o m
    c.setBackgroundColor(getColor(color));
    iPdfTable.addCell(c);
    c = createCellNoBorder();
    addText(c, "  " + text);
    c.setHorizontalAlignment(Element.ALIGN_LEFT);
    iPdfTable.addCell(c);
}

From source file:org.unitime.timetable.webutil.timegrid.PdfTimetableGridTable.java

License:Open Source License

public void printToPdf(TimetableGridModel model, int rowNumber) throws Exception {
    model.clearRendered();//from  ww w.j  a  v  a  2s  .co  m

    if (iTable.isDispModePerWeek()) {
        createTable();
        printHeader(model, 0);
    } else {
        if (rowNumber == 0) {
            createTable();
            printHeader(model, 0);
        }
    }

    if (iTable.isDispModeInRow()) {
        int maxIdx = model.getMaxIdxForDay(iDay, iTable.firstSlot(), iTable.lastSlot());
        for (int idx = 0; idx <= maxIdx; idx++) {
            PdfPCell c = createCell();
            c.setBorderWidthTop(0);
            if (idx == 0)
                addText(c, model.getName() + (model.getSize() > 0 ? " (" + model.getSize() + ")" : ""), true);
            c.setBorderWidthBottom(idx == maxIdx ? 1 : 0);
            c.setColspan(12);
            iPdfTable.addCell(c);
            for (int slot = iTable.firstSlot(); slot <= iTable.lastSlot(); slot++) {
                int slotsToEnd = iTable.lastSlot() - slot + 1;
                TimetableGridCell cell = model.getCell(iDay, slot, idx);
                int length = (cell == null ? 1 : cell.getLength() + cell.getSlot() - slot);
                int colSpan = (cell == null ? 1 : Math.min(length, slotsToEnd));
                if (cell == null) {
                    String bgColor = model.getBackground(iDay, slot);
                    if (bgColor == null && !model.isAvailable(iDay, slot))
                        bgColor = TimetableGridCell.sBgColorNotAvailable;
                    c = createCell();
                    c.setBorderWidthTop(0);
                    c.setBorderWidthBottom(
                            idx == maxIdx ? 1 : model.getCell(iDay, slot, idx + 1) != null ? 1 : 0);
                    c.setBorderWidthLeft(0);
                    boolean eod = (slot == iTable.lastSlot());
                    boolean in = !eod && model.getCell(iDay, slot + 1, idx) == null
                            && ((slot + 1 - iTable.firstSlot()) % iTable.nrSlotsPerPeriod()) != 0;
                    c.setBorderWidthRight(eod || !in ? 1 : 0);
                    c.setColspan(colSpan);
                    if (bgColor != null)
                        c.setBackgroundColor(getColor(bgColor));
                    iPdfTable.addCell(c);
                } else {
                    String bgColor = cell.getBackground();
                    if (iTable.getBgMode() == TimetableGridModel.sBgModeNone) {
                        for (int i = 0; i < length; i++)
                            if (!model.isAvailable(iDay, slot + i)) {
                                bgColor = TimetableGridCell.sBgColorNotAvailableButAssigned;
                                break;
                            }
                    }
                    c = createCell();
                    c.setBorderWidthTop(0);
                    c.setBorderWidthLeft(0);
                    c.setColspan(colSpan);
                    c.setBackgroundColor(getColor(bgColor));
                    addText(c, cell.getName());
                    if (iTable.getShowTimes())
                        addText(c, cell.getTime());
                    if (iTable.getResourceType() != TimetableGridModel.sResourceTypeRoom)
                        addText(c, cell.getRoomName());
                    if (iTable.getResourceType() != TimetableGridModel.sResourceTypeInstructor
                            && iTable.getShowInstructors() && !cell.getInstructor().isEmpty())
                        addText(c, cell.getInstructor());
                    if (iTable.getShowComments())
                        addText(c,
                                cell.getShortCommentNoColors() == null ? null : cell.getShortCommentNoColors());
                    if (iTable.getWeek() == -100 && cell.hasDays()
                            && !cell.getDays().equals(iTable.getDefaultDatePatternName()))
                        addText(c, cell.getDays());
                    iPdfTable.addCell(c);
                    slot += length - 1;
                }
            }
        }
    } else if (iTable.isDispModePerWeekHorizontal()) {
        for (int day = iTable.startDay(); day <= iTable.endDay(); day++) {
            int maxIdx = model.getMaxIdxForDay(day, iTable.firstSlot(), iTable.lastSlot());
            for (int idx = 0; idx <= maxIdx; idx++) {
                PdfPCell c = createCell();
                c.setBorderWidthTop(0);
                if (idx == 0)
                    addText(c, Constants.DAY_NAME[day], true);
                c.setBorderWidthBottom(idx == maxIdx ? 1 : 0);
                c.setColspan(12);
                iPdfTable.addCell(c);
                for (int slot = iTable.firstSlot(); slot <= iTable.lastSlot(); slot++) {
                    int slotsToEnd = iTable.lastSlot() - slot + 1;
                    TimetableGridCell cell = model.getCell(day, slot, idx);
                    int length = (cell == null ? 1 : cell.getLength() + cell.getSlot() - slot);
                    int colSpan = (cell == null ? 1 : Math.min(length, slotsToEnd));
                    if (cell == null) {
                        String bgColor = model.getBackground(day, slot);
                        if (bgColor == null && !model.isAvailable(day, slot))
                            bgColor = TimetableGridCell.sBgColorNotAvailable;
                        c = createCell();
                        c.setBorderWidthTop(0);
                        c.setBorderWidthBottom(
                                idx == maxIdx ? 1 : model.getCell(day, slot, idx + 1) != null ? 1 : 0);
                        c.setBorderWidthLeft(0);
                        boolean eod = (slot == iTable.lastSlot());
                        boolean in = !eod && model.getCell(day, slot + 1, idx) == null
                                && ((slot + 1 - iTable.firstSlot()) % iTable.nrSlotsPerPeriod()) != 0;
                        c.setBorderWidthRight(eod || !in ? 1 : 0);
                        iPdfTable.addCell(c);
                    } else {
                        String bgColor = cell.getBackground();
                        if (iTable.getBgMode() == TimetableGridModel.sBgModeNone) {
                            for (int i = 0; i < length; i++)
                                if (!model.isAvailable(day, slot + i)) {
                                    bgColor = TimetableGridCell.sBgColorNotAvailableButAssigned;
                                    break;
                                }
                        }
                        c = createCell();
                        c.setBorderWidthTop(0);
                        c.setBorderWidthLeft(0);
                        c.setColspan(colSpan);
                        if (bgColor != null)
                            c.setBackgroundColor(getColor(bgColor));
                        addText(c, cell.getName());
                        if (iTable.getShowTimes())
                            addText(c, cell.getTime());
                        if (iTable.getResourceType() != TimetableGridModel.sResourceTypeRoom)
                            addText(c, cell.getRoomName());
                        if (iTable.getResourceType() != TimetableGridModel.sResourceTypeInstructor
                                && iTable.getShowInstructors() && !cell.getInstructor().isEmpty())
                            addText(c, cell.getInstructor());
                        if (iTable.getShowComments())
                            addText(c, cell.getShortCommentNoColors() == null ? null
                                    : cell.getShortCommentNoColors());
                        if (iTable.getWeek() == -100 && cell.hasDays()
                                && !cell.getDays().equals(iTable.getDefaultDatePatternName()))
                            addText(c, cell.getDays());
                        iPdfTable.addCell(c);
                        slot += length - 1;
                    }
                }
            }
        }
    } else if (iTable.isDispModeWeekByWeekHorizontal()) {
        Calendar cal = Calendar.getInstance(Locale.US);
        cal.setTime(iTable.iFirstDate);
        for (int d = 0; d < 365; d++) {
            if (d > 0)
                cal.add(Calendar.DAY_OF_YEAR, 1);
            int date = d + iTable.iFirstDay;
            if (model.getFirstDay() >= 0 && (date < model.getFirstDay() || date > model.getFirstDay() + 6))
                continue;
            int day = d % 7;
            if (day < iTable.startDay() || day > iTable.endDay())
                continue;
            boolean hasClasses = false;
            for (int slot = iTable.firstSlot(); slot <= iTable.lastSlot(); slot++) {
                if (model.getCell(day, slot, 0, date) != null) {
                    hasClasses = true;
                    break;
                }
            }
            if (!hasClasses)
                continue;
            int maxIdx = model.getMaxIdxForDay(day, iTable.firstSlot(), iTable.lastSlot(), date);
            for (int idx = 0; idx <= maxIdx; idx++) {
                PdfPCell c = createCell();
                c.setBorderWidthTop(0);
                if (idx == 0)
                    addText(c, Constants.DAY_NAME[day] + " " + sDF.format(cal.getTime()), true);
                c.setBorderWidthBottom(idx == maxIdx ? 1 : 0);
                c.setColspan(12);
                iPdfTable.addCell(c);
                for (int slot = iTable.firstSlot(); slot <= iTable.lastSlot(); slot++) {
                    int slotsToEnd = iTable.lastSlot() - slot + 1;
                    TimetableGridCell cell = model.getCell(day, slot, idx, date);
                    int length = (cell == null ? 1 : cell.getLength() + cell.getSlot() - slot);
                    int colSpan = (cell == null ? 1 : Math.min(length, slotsToEnd));
                    if (cell == null) {
                        String bgColor = model.getBackground(day, slot);
                        if (bgColor == null && !model.isAvailable(day, slot))
                            bgColor = TimetableGridCell.sBgColorNotAvailable;
                        c = createCell();
                        c.setBorderWidthTop(0);
                        c.setBorderWidthBottom(
                                idx == maxIdx ? 1 : model.getCell(day, slot, idx + 1, date) != null ? 1 : 0);
                        c.setBorderWidthLeft(0);
                        boolean eod = (slot == iTable.lastSlot());
                        boolean in = !eod && model.getCell(day, slot + 1, idx, date) == null
                                && ((slot + 1 - iTable.firstSlot()) % iTable.nrSlotsPerPeriod()) != 0;
                        c.setBorderWidthRight(eod || !in ? 1 : 0);
                        iPdfTable.addCell(c);
                    } else {
                        String bgColor = cell.getBackground();
                        if (iTable.getBgMode() == TimetableGridModel.sBgModeNone) {
                            for (int i = 0; i < length; i++)
                                if (!model.isAvailable(day, slot + i)) {
                                    bgColor = TimetableGridCell.sBgColorNotAvailableButAssigned;
                                    break;
                                }
                        }
                        c = createCell();
                        c.setBorderWidthTop(0);
                        c.setBorderWidthLeft(0);
                        c.setColspan(colSpan);
                        if (bgColor != null)
                            c.setBackgroundColor(getColor(bgColor));
                        addText(c, cell.getName());
                        if (iTable.getShowTimes())
                            addText(c, cell.getTime());
                        if (iTable.getResourceType() != TimetableGridModel.sResourceTypeRoom)
                            addText(c, cell.getRoomName());
                        if (iTable.getResourceType() != TimetableGridModel.sResourceTypeInstructor
                                && iTable.getShowInstructors() && !cell.getInstructor().isEmpty())
                            addText(c, cell.getInstructor());
                        if (iTable.getShowComments())
                            addText(c, cell.getShortCommentNoColors() == null ? null
                                    : cell.getShortCommentNoColors());
                        iPdfTable.addCell(c);
                        slot += length - 1;
                    }
                }
            }
        }
    } else { //isDispModePerWeekVertical
        for (int day = iTable.startDay(); day <= iTable.endDay(); day++) {
            int maxIdx = model.getMaxIdxForDay(day, iTable.firstSlot(), iTable.lastSlot());
            for (int idx = 0; idx <= maxIdx; idx++) {
                PdfPCell c = null;
                for (int slot = iTable.lastSlot(); slot >= iTable.firstSlot(); slot--) {
                    int slotsToEnd = slot + 1 - iTable.firstSlot();
                    TimetableGridCell cell = model.getCell(day, slot, idx);
                    int length = (cell == null ? 1 : 1 + slot - cell.getSlot());
                    int colSpan = (cell == null ? 1 : Math.min(length, slotsToEnd));
                    if (cell == null) {
                        String bgColor = model.getBackground(day, slot);
                        if (bgColor == null && !model.isAvailable(day, slot))
                            bgColor = TimetableGridCell.sBgColorNotAvailable;
                        c = createCell();
                        c.setBorderWidthTop(0);
                        c.setMinimumHeight(100f);
                        c.setBorderWidthBottom(
                                idx == maxIdx ? 1 : model.getCell(day, slot, idx + 1) != null ? 1 : 0);
                        c.setBorderWidthLeft(slot == iTable.lastSlot() ? 1 : 0);
                        boolean eod = (slot == iTable.firstSlot());
                        boolean in = !eod && model.getCell(day, slot - 1, idx) == null
                                && ((slot - iTable.firstSlot()) % iTable.nrSlotsPerPeriod()) != 0;
                        c.setBorderWidthRight(eod || !in ? 1 : 0);
                        iPdfTable.addCell(c);
                    } else {
                        String bgColor = cell.getBackground();
                        if (iTable.getBgMode() == TimetableGridModel.sBgModeNone) {
                            for (int i = 0; i < length; i++)
                                if (!model.isAvailable(day, slot - i)) {
                                    bgColor = TimetableGridCell.sBgColorNotAvailableButAssigned;
                                    break;
                                }
                        }
                        c = createCell();
                        c.setBorderWidthTop(0);
                        c.setBorderWidthLeft(slot == iTable.lastSlot() ? 1 : 0);
                        c.setColspan(colSpan);
                        if (bgColor != null)
                            c.setBackgroundColor(getColor(bgColor));
                        if (iTable.getWeek() == -100 && cell.hasDays()
                                && !cell.getDays().equals(iTable.getDefaultDatePatternName()))
                            addTextVertical(c, cell.getDays());
                        if (iTable.getShowTimes())
                            addText(c, cell.getTime());
                        if (iTable.getResourceType() != TimetableGridModel.sResourceTypeRoom)
                            addTextVertical(c, cell.getRoomName());
                        if (iTable.getResourceType() != TimetableGridModel.sResourceTypeInstructor
                                && iTable.getShowInstructors() && !cell.getInstructor().isEmpty())
                            addTextVertical(c, cell.getInstructor());
                        if (iTable.getShowComments())
                            addTextVertical(c, cell.getShortCommentNoColors() == null ? null
                                    : cell.getShortCommentNoColors());
                        addTextVertical(c, cell.getName());
                        iPdfTable.addCell(c);
                        slot -= length - 1;
                    }
                }
                c = createCell();
                c.setBorderWidthTop(0);
                c.setBorderWidthLeft(0);
                if (idx == 0)
                    addTextVertical(c, Constants.DAY_NAME[day], true);
                c.setBorderWidthBottom(idx == maxIdx ? 1 : 0);
                c.setColspan(12);
                iPdfTable.addCell(c);
            }
        }
        /*
        int step = iTable.nrSlotsPerPeriod();
        for (int slot=iTable.firstSlot();slot<=iTable.lastSlot();slot+=step) {
           int time = slot * Constants.SLOT_LENGTH_MIN + Constants.FIRST_SLOT_TIME_MIN;
        int slotsToEnd = iTable.lastSlot()-slot+1;
        if ((slot%iTable.nrSlotsPerPeriod()) == 0) {
        c = createCell("TimetableHeadCell"+(slot==iTable.firstSlot()?"":"In")+"Vertical");
        addText(c, Constants.toTime(time), true);
        iPdfTable.addCell(c);
        } else {
           c = createCell("TimetableHeadCellInVertical");
           iPdfTable.addCell(c);
        }
        for (int day=iTable.startDay();day<=iTable.endDay();day++) {
           int maxIdx = model.getMaxIdxForDay(day,iTable.firstSlot(),iTable.lastSlot());
           for (int idx=0;idx<=maxIdx;idx++) {
               TimetableGridCell cell = model.getCell(day,slot, idx);
               if (model.isRendered(day,slot,idx)) continue;
          int rowSpan = (cell==null?1:Math.min(cell.getLength()+cell.getSlot()-slot,slotsToEnd));
          int colSpan = (iTable.getResourceType()==TimetableGridModel.sResourceTypeDepartment && cell!=null?1:model.getDepth(day,slot,idx,maxIdx,rowSpan)); 
          model.setRendered(day,slot,idx,colSpan,rowSpan);
          int rowSpanDivStep = (int)Math.ceil(((double)rowSpan)/step);
                       
               if (cell==null) {
             String bgColor = model.getBackground(day,slot);
             if (bgColor==null && !model.isAvailable(day,slot))
                bgColor=TimetableGridCell.sBgColorNotAvailable;
                    boolean eol = (day==iTable.endDay() && (idx+colSpan-1)==maxIdx);
             c = createCell("TimetableCell"+(slot==iTable.firstSlot()?"":"In")+"Vertical"+(eol?"EOL":""));
             c.setColspan(colSpan);
             //c.setRowspan(rowSpanDivStep);
             if (bgColor!=null)
                c.setBackgroundColor(getColor(bgColor));
             iPdfTable.addCell(c);
               } else {
                  String bgColor = cell.getBackground();
                  if (iTable.getBgMode()==TimetableGridModel.sBgModeNone) {
                      for (int i=0;i<cell.getLength();i++)
                         if (!model.isAvailable(day,slot+i)) {
                            bgColor = TimetableGridCell.sBgColorNotAvailableButAssigned;
                            break;
                         }
                  }
                  boolean eol = (day==iTable.endDay());
                  c = createCell("TimetableCell"+(slot==iTable.firstSlot()?"":"In")+"Vertical" + (eol?"EOL":""));
             c.setColspan(colSpan);
             //c.setRowspan(rowSpanDivStep);
             if (bgColor!=null)
                c.setBackgroundColor(getColor(bgColor));
             addText(c, cell.getName());
             if (iTable.getResourceType()!=TimetableGridModel.sResourceTypeRoom)
                addText(c, cell.getRoomName());
             if (iTable.getShowComments())
                addText(c, cell.getShortComment()==null?"":cell.getShortComment());
             if (iTable.getWeek()==-100 && cell.hasDays() && !cell.getDays().equals(iTable.getDefaultDatePatternName()))
                addText(c, cell.getDays());
             iPdfTable.addCell(c);
               }
            }
        }
        }
        */
    }

    if (iTable.isDispModePerWeek()) {
        flushTable();
    }
}

From source file:org.webguitoolkit.ui.util.export.PDFTableExport.java

License:Apache License

/**
 * @param table/*w w  w  . j a v  a  2  s . co m*/
 * @param footer
 * @param header
 * @return
 */
public PdfPTable pdfExport(Table table) {
    TableExportOptions exportOptions = table.getExportOptions();
    boolean showOnlyDisplayed = exportOptions.isShowOnlyDisplayedColumns();
    Font headfont = new Font(Font.UNDEFINED, Font.DEFAULTSIZE, Font.BOLD);

    Font bodyfont = new Font(exportOptions.getPdfFontSize(), exportOptions.getPdfFontSize(), Font.NORMAL);
    if (exportOptions.getPdfFontColour() != null) {
        Color fontColor = exportOptions.getPdfFontColour();
        bodyfont.setColor(fontColor);
        headfont.setColor(fontColor);
    }

    List<ITableColumn> columns = getTableColumns(showOnlyDisplayed, table);
    int columnCount = 0;
    for (int i = 0; i < columns.size(); i++) {
        TableColumn c = (TableColumn) columns.get(i);
        //hide select checkbox column
        if (!c.isExporatble())
            continue;

        columnCount++;

    }
    PdfPTable resulttable = new PdfPTable(columnCount);
    resulttable.setWidthPercentage(100f);

    if (StringUtils.isNotEmpty(exportOptions.getTableHeadline())) {
        PdfPCell cell = new PdfPCell(new Paragraph(exportOptions.getTableHeadline()));
        cell.setColspan(columns.size());
        cell.setBackgroundColor(Color.lightGray);
        resulttable.addCell(cell);
    }

    for (int i = 0; i < columns.size(); i++) {
        TableColumn c = (TableColumn) columns.get(i);
        // hide select checkbox column
        if (!c.isExporatble())
            continue;

        String cellData = TextService.getString(c.getTitle());
        if (cellData.contains("<br/>") || cellData.contains("<br>")) {
            cellData = cellData.replaceAll("<br\\/>", "\\/");
            cellData = cellData.replaceAll("<br>", "\\/");
        }
        PdfPCell cell = new PdfPCell(new Paragraph((cellData), headfont));
        cell.setBackgroundColor(Color.lightGray);
        resulttable.addCell(cell);

    }

    List tabledata = table.getDefaultModel().getFilteredList();
    if (tabledata.isEmpty()) {
        for (int i = 0; i < columns.size(); i++) {
            resulttable.addCell(new PdfPCell((new Phrase(""))));
        }
    }

    for (Iterator it = tabledata.iterator(); it.hasNext();) {
        DataBag dbag = (DataBag) it.next();
        for (int i = 0; i < columns.size(); i++) {
            TableColumn tableColumn = (TableColumn) columns.get(i);
            if (!tableColumn.isExporatble())
                continue;
            logger.debug("property: " + tableColumn.getProperty());
            IColumnRenderer renderer = tableColumn.getRenderer();
            Converter converter = tableColumn.getConverter();

            addObjectCell(bodyfont, resulttable, dbag, tableColumn, renderer, converter);
        }
    }

    if (StringUtils.isNotEmpty(exportOptions.getTableFooter())) {
        PdfPCell cell = new PdfPCell(new Paragraph(exportOptions.getTableFooter()));
        cell.setColspan(columnCount);
        cell.setBackgroundColor(Color.lightGray);
        resulttable.addCell(cell);
    }

    resulttable.setHeaderRows(1);
    return resulttable;

}

From source file:oscar.oscarLab.ca.all.pageUtil.LabPDFCreator.java

License:Open Source License

public void printPdf() throws IOException, DocumentException {

    // check that we have data to print
    if (handler == null)
        throw new DocumentException();

    //response.setContentType("application/pdf");  //octet-stream
    //response.setHeader("Content-Disposition", "attachment; filename=\""+handler.getPatientName().replaceAll("\\s", "_")+"_LabReport.pdf\"");

    //Create the document we are going to write to
    document = new Document();
    //PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
    PdfWriter writer = PdfWriter.getInstance(document, os);

    //Set page event, function onEndPage will execute each time a page is finished being created
    writer.setPageEvent(this);

    document.setPageSize(PageSize.LETTER);
    document.addTitle("Title of the Document");
    document.addCreator("OSCAR");
    document.open();/*from   ww w .  j  a  va2s . com*/

    //Create the fonts that we are going to use
    bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    font = new Font(bf, 9, Font.NORMAL);
    boldFont = new Font(bf, 10, Font.BOLD);
    redFont = new Font(bf, 9, Font.NORMAL, Color.RED);

    // add the header table containing the patient and lab info to the document
    createInfoTable();

    // add the tests and test info for each header
    ArrayList<String> headers = handler.getHeaders();
    for (int i = 0; i < headers.size(); i++)
        addLabCategory(headers.get(i));

    // add end of report table
    PdfPTable table = new PdfPTable(1);
    table.setWidthPercentage(100);
    PdfPCell cell = new PdfPCell();
    cell.setBorder(0);
    cell.setPhrase(new Phrase("  "));
    table.addCell(cell);
    cell.setBorder(15);
    cell.setBackgroundColor(new Color(210, 212, 255));
    cell.setPhrase(new Phrase("END OF REPORT", boldFont));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    table.addCell(cell);
    document.add(table);

    document.close();

    os.flush();
}

From source file:oscar.oscarLab.ca.all.pageUtil.LabPDFCreator.java

License:Open Source License

private void addLabCategory(String header) throws DocumentException {

    float[] mainTableWidths = { 5f, 3f, 1f, 3f, 2f, 4f, 2f };
    PdfPTable table = new PdfPTable(mainTableWidths);
    table.setHeaderRows(3);/*  w ww . ja  v a 2 s. c  o m*/
    table.setWidthPercentage(100);

    PdfPCell cell = new PdfPCell();
    // category name
    cell.setPadding(3);
    cell.setPhrase(new Phrase("  "));
    cell.setBorder(0);
    cell.setColspan(7);
    table.addCell(cell);
    cell.setBorder(15);
    cell.setPadding(3);
    cell.setColspan(2);
    cell.setPhrase(new Phrase(header.replaceAll("<br\\s*/*>", "\n"), new Font(bf, 12, Font.BOLD)));
    table.addCell(cell);
    cell.setPhrase(new Phrase("  "));
    cell.setBorder(0);
    cell.setColspan(5);
    table.addCell(cell);

    // table headers
    cell.setColspan(1);
    cell.setBorder(15);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBackgroundColor(new Color(210, 212, 255));
    cell.setPhrase(new Phrase("Test Name(s)", boldFont));
    table.addCell(cell);
    cell.setPhrase(new Phrase("Result", boldFont));
    table.addCell(cell);
    cell.setPhrase(new Phrase("Abn", boldFont));
    table.addCell(cell);
    cell.setPhrase(new Phrase("Reference Range", boldFont));
    table.addCell(cell);
    cell.setPhrase(new Phrase("Units", boldFont));
    table.addCell(cell);
    cell.setPhrase(new Phrase("Date/Time Completed", boldFont));
    table.addCell(cell);
    cell.setPhrase(new Phrase("Status", boldFont));
    table.addCell(cell);

    // add test results
    int obrCount = handler.getOBRCount();
    int linenum = 0;
    cell.setBorder(12);
    cell.setBorderColor(Color.BLACK); // cell.setBorderColor(Color.WHITE);
    cell.setBackgroundColor(new Color(255, 255, 255));

    if (handler.getMsgType().equals("MEDVUE")) {

        //cell.setBackgroundColor(getHighlightColor(linenum));
        linenum++;
        cell.setPhrase(new Phrase(handler.getRadiologistInfo(), boldFont));
        cell.setColspan(7);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);
        cell.setPaddingLeft(100);
        cell.setColspan(7);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setPhrase(new Phrase(handler.getOBXComment(1, 1, 1).replaceAll("<br\\s*/*>", "\n"), font));
        table.addCell(cell);

    } else {
        for (int j = 0; j < obrCount; j++) {
            boolean obrFlag = false;
            int obxCount = handler.getOBXCount(j);
            for (int k = 0; k < obxCount; k++) {
                String obxName = handler.getOBXName(j, k);

                if (!handler.getOBXResultStatus(j, k).equals("TDIS")) {

                    // ensure that the result is a real result
                    if ((!handler.getOBXResultStatus(j, k).equals("DNS") && !obxName.equals("")
                            && handler.getObservationHeader(j, k).equals(header))
                            || (handler.getMsgType().equals("EPSILON")
                                    && handler.getOBXIdentifier(j, k).equals(header) && !obxName.equals(""))
                            || (handler.getMsgType().equals("PFHT") && !obxName.equals("")
                                    && handler.getObservationHeader(j, k).equals(header))) { // <<-- DNS only needed for
                        // MDS messages
                        String obrName = handler.getOBRName(j);

                        // add the obrname if necessary
                        if (!obrFlag && !obrName.equals("") && !(obxName.contains(obrName) && obxCount < 2)) {
                            // cell.setBackgroundColor(getHighlightColor(linenum));
                            linenum++;
                            cell.setPhrase(new Phrase(obrName, boldFont));
                            cell.setColspan(7);
                            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                            table.addCell(cell);
                            cell.setColspan(1);
                            obrFlag = true;
                        }

                        // add the obx results and info
                        Font lineFont = new Font(bf, 8, Font.NORMAL,
                                getTextColor(handler.getOBXAbnormalFlag(j, k)));
                        // cell.setBackgroundColor(getHighlightColor(linenum));
                        linenum++;
                        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                        cell.setPhrase(new Phrase((obrFlag ? "   " : "") + obxName, lineFont));
                        table.addCell(cell);
                        cell.setPhrase(new Phrase(handler.getOBXResult(j, k).replaceAll("<br\\s*/*>", "\n"),
                                lineFont));
                        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        table.addCell(cell);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setPhrase(new Phrase(
                                (handler.isOBXAbnormal(j, k) ? handler.getOBXAbnormalFlag(j, k) : "N"),
                                lineFont));
                        table.addCell(cell);
                        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                        cell.setPhrase(new Phrase(handler.getOBXReferenceRange(j, k), lineFont));
                        table.addCell(cell);
                        cell.setPhrase(new Phrase(handler.getOBXUnits(j, k), lineFont));
                        table.addCell(cell);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setPhrase(new Phrase(handler.getTimeStamp(j, k), lineFont));
                        table.addCell(cell);
                        cell.setPhrase(new Phrase(handler.getOBXResultStatus(j, k), lineFont));
                        table.addCell(cell);

                        if (!handler.getMsgType().equals("PFHT")) {
                            // add obx comments
                            if (handler.getOBXCommentCount(j, k) > 0) {
                                // cell.setBackgroundColor(getHighlightColor(linenum));
                                linenum++;
                                cell.setPaddingLeft(100);
                                cell.setColspan(7);
                                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                                for (int l = 0; l < handler.getOBXCommentCount(j, k); l++) {

                                    cell.setPhrase(new Phrase(
                                            handler.getOBXComment(j, k, l).replaceAll("<br\\s*/*>", "\n"),
                                            font));
                                    table.addCell(cell);

                                }
                                cell.setPadding(3);
                                cell.setColspan(1);
                            }
                        }
                        // if (DNS)
                    } else if ((handler.getMsgType().equals("EPSILON")
                            && handler.getOBXIdentifier(j, k).equals(header) && obxName.equals(""))
                            || (handler.getMsgType().equals("PFHT") && obxName.equals("")
                                    && handler.getObservationHeader(j, k).equals(header))) {
                        // cell.setBackgroundColor(getHighlightColor(linenum));
                        linenum++;
                        cell.setPaddingLeft(100);
                        cell.setColspan(7);
                        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                        cell.setPhrase(
                                new Phrase(handler.getOBXResult(j, k).replaceAll("<br\\s*/*>", "\n"), font));
                        table.addCell(cell);
                        cell.setPadding(3);
                        cell.setColspan(1);

                    }
                    if (handler.getMsgType().equals("PFHT") && !handler.getNteForOBX(j, k).equals("")
                            && handler.getNteForOBX(j, k) != null) {
                        // cell.setBackgroundColor(getHighlightColor(linenum));
                        linenum++;
                        cell.setPaddingLeft(100);
                        cell.setColspan(7);
                        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                        cell.setPhrase(
                                new Phrase(handler.getNteForOBX(j, k).replaceAll("<br\\s*/*>", "\n"), font));
                        table.addCell(cell);
                        cell.setPadding(3);
                        cell.setColspan(1);

                        if (handler.getOBXCommentCount(j, k) > 0) {
                            // cell.setBackgroundColor(getHighlightColor(linenum));
                            linenum++;
                            cell.setPaddingLeft(100);
                            cell.setColspan(7);
                            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                            for (int l = 0; l < handler.getOBXCommentCount(j, k); l++) {

                                cell.setPhrase(new Phrase(
                                        handler.getOBXComment(j, k, l).replaceAll("<br\\s*/*>", "\n"), font));
                                table.addCell(cell);

                            }
                            cell.setPadding(3);
                            cell.setColspan(1);
                        }
                    }
                } else {
                    if (handler.getOBXCommentCount(j, k) > 0) {
                        // cell.setBackgroundColor(getHighlightColor(linenum));
                        linenum++;
                        cell.setPaddingLeft(100);
                        cell.setColspan(7);
                        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                        for (int l = 0; l < handler.getOBXCommentCount(j, k); l++) {

                            cell.setPhrase(new Phrase(
                                    handler.getOBXComment(j, k, l).replaceAll("<br\\s*/*>", "\n"), font));
                            table.addCell(cell);

                        }
                        cell.setPadding(3);
                        cell.setColspan(1);
                    }
                } // if (!handler.getOBXResultStatus(j, k).equals("TDIS"))

            }

            if (!handler.getMsgType().equals("PFHT")) {
                // add obr comments
                if (handler.getObservationHeader(j, 0).equals(header)) {
                    cell.setColspan(7);
                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    for (int k = 0; k < handler.getOBRCommentCount(j); k++) {
                        // the obrName should only be set if it has not been
                        // set already which will only have occured if the
                        // obx name is "" or if it is the same as the obr name
                        if (!obrFlag && handler.getOBXName(j, 0).equals("")) {
                            // cell.setBackgroundColor(getHighlightColor(linenum));
                            linenum++;

                            cell.setPhrase(new Phrase(handler.getOBRName(j), boldFont));
                            table.addCell(cell);
                            obrFlag = true;
                        }

                        // cell.setBackgroundColor(getHighlightColor(linenum));
                        linenum++;
                        cell.setPaddingLeft(100);
                        cell.setPhrase(
                                new Phrase(handler.getOBRComment(j, k).replaceAll("<br\\s*/*>", "\n"), font));
                        table.addCell(cell);
                        cell.setPadding(3);
                    }
                    cell.setColspan(1);
                }
            }
        } // for (j)

    } // if (isMEDVUE)

    document.add(table);

}

From source file:oscar.oscarLab.ca.all.pageUtil.LabPDFCreator.java

License:Open Source License

private void createInfoTable() throws DocumentException {

    //Create patient info table
    PdfPCell cell = new PdfPCell();
    cell.setBorder(0);/* w w  w  .  ja v a2  s .c o m*/
    float[] pInfoWidths = { 2f, 4f, 3f, 2f };
    PdfPTable pInfoTable = new PdfPTable(pInfoWidths);
    cell.setPhrase(new Phrase("Patient Name: ", boldFont));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase(handler.getPatientName(), font));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase("Home Phone: ", boldFont));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase(handler.getHomePhone(), font));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase("Date of Birth: ", boldFont));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase(handler.getDOB(), font));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase("Work Phone: ", boldFont));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase(handler.getWorkPhone(), font));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase("Age: ", boldFont));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase(handler.getAge(), font));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase("Sex: ", boldFont));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase(handler.getSex(), font));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase("Health #: ", boldFont));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase(handler.getHealthNum(), font));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase("Patient Location: ", boldFont));
    pInfoTable.addCell(cell);
    cell.setPhrase(new Phrase(handler.getPatientLocation(), font));
    pInfoTable.addCell(cell);

    //Create results info table
    PdfPTable rInfoTable = new PdfPTable(2);
    cell.setPhrase(new Phrase("Date of Service: ", boldFont));
    rInfoTable.addCell(cell);
    cell.setPhrase(new Phrase(handler.getServiceDate(), font));
    rInfoTable.addCell(cell);
    cell.setPhrase(new Phrase("Date Received: ", boldFont));
    rInfoTable.addCell(cell);
    cell.setPhrase(new Phrase(dateLabReceived, font));
    rInfoTable.addCell(cell);
    cell.setPhrase(new Phrase("Report Status: ", boldFont));
    rInfoTable.addCell(cell);
    cell.setPhrase(new Phrase((handler.getOrderStatus().equals("F") ? "Final"
            : (handler.getOrderStatus().equals("C") ? "Corrected" : "Partial")), font));
    rInfoTable.addCell(cell);
    cell.setPhrase(new Phrase("Client Ref. #: ", boldFont));
    rInfoTable.addCell(cell);
    cell.setPhrase(new Phrase(handler.getClientRef(), font));
    rInfoTable.addCell(cell);
    cell.setPhrase(new Phrase("Accession #: ", boldFont));
    rInfoTable.addCell(cell);
    cell.setPhrase(new Phrase(handler.getAccessionNum(), font));
    rInfoTable.addCell(cell);

    //Create client table
    float[] clientWidths = { 2f, 3f };
    Phrase clientPhrase = new Phrase();
    PdfPTable clientTable = new PdfPTable(clientWidths);
    clientPhrase.add(new Chunk("Requesting Client:  ", boldFont));
    clientPhrase.add(new Chunk(handler.getDocName(), font));
    cell.setPhrase(clientPhrase);
    clientTable.addCell(cell);

    clientPhrase = new Phrase();
    clientPhrase.add(new Chunk("cc: Client:  ", boldFont));
    clientPhrase.add(new Chunk(handler.getCCDocs(), font));
    cell.setPhrase(clientPhrase);
    clientTable.addCell(cell);

    //Create header info table
    float[] tableWidths = { 2f, 1f };
    PdfPTable table = new PdfPTable(tableWidths);
    if (multiID.length > 1) {
        cell = new PdfPCell(new Phrase("Version: " + versionNum + " of " + multiID.length, boldFont));
        cell.setBackgroundColor(new Color(210, 212, 255));
        cell.setPadding(3);
        cell.setColspan(2);
        table.addCell(cell);
    }
    cell = new PdfPCell(new Phrase("Detail Results: Patient Info", boldFont));
    cell.setBackgroundColor(new Color(210, 212, 255));
    cell.setPadding(3);
    table.addCell(cell);
    cell.setPhrase(new Phrase("Results Info", boldFont));
    table.addCell(cell);

    // add the created tables to the document
    table = addTableToTable(table, pInfoTable, 1);
    table = addTableToTable(table, rInfoTable, 1);
    table = addTableToTable(table, clientTable, 2);

    table.setWidthPercentage(100);

    document.add(table);
}

From source file:pdf.PersonPdfView.java

public PdfPTable setCell(String[] cellList) {
    PdfPTable table = new PdfPTable(cellList.length);
    for (String cellLabel : cellList) {
        PdfPCell cell = new PdfPCell(new Phrase(cellLabel));
        cell.setBackgroundColor(Color.RED);
        table.addCell(cell);//from  w  w  w  . j a  v a  2  s .c om

    }
    return table;
}

From source file:questions.tables.RotatedCells.java

public static void main(String[] args) {
    Document document = new Document();
    try {/*from   w w w  .  j  a va 2s .  co m*/
        PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();

        float[] widths = { 1, 4 };
        PdfPTable table = new PdfPTable(widths);
        table.setWidthPercentage(40);
        PdfPCell cell;
        cell = new PdfPCell(new Paragraph("test 1"));
        cell.setPadding(4);
        cell.setBackgroundColor(Color.YELLOW);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        table.addCell("Cell without any rotation.");

        cell = new PdfPCell(new Paragraph("test 2"));
        cell.setPadding(4);
        cell.setBackgroundColor(Color.YELLOW);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setRotation(90);
        table.addCell(cell);
        table.addCell("Cell rotated 90 degrees.");

        cell = new PdfPCell(new Paragraph("test 3"));
        cell.setPadding(4);
        cell.setBackgroundColor(Color.YELLOW);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setRotation(180);
        table.addCell(cell);
        table.addCell("Cell upside down.");

        cell = new PdfPCell(new Paragraph("test 4"));
        cell.setPadding(4);
        cell.setBackgroundColor(Color.YELLOW);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setRotation(270);
        table.addCell(cell);
        table.addCell("Cell rotated 270 degrees.");
        document.add(table);
    } 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: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 {/*w ww  . j  a  va 2s. c o  m*/
        // 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();
}