List of usage examples for com.itextpdf.text.pdf PdfPCell setUseDescender
public void setUseDescender(boolean useDescender)
From source file:fll.util.PdfUtils.java
License:Open Source License
/** * Create a table cell from the specified {@link Chunk}. *//*from ww w.ja va2 s . c o m*/ public static PdfPCell createBasicCell(final Chunk chunk) throws BadElementException { final PdfPCell cell = new PdfPCell(new Phrase(chunk)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setUseDescender(true); return cell; }
From source file:ro.nextreports.engine.exporter.PdfExporter.java
License:Apache License
private PdfPCell renderPdfCell(BandElement bandElement, Object value, int gridRow, int rowSpan, int colSpan, boolean image, int column) { Map<String, Object> style = buildCellStyleMap(bandElement, value, gridRow, column, colSpan); FontFactoryImp fact = new FontFactoryImp(); com.itextpdf.text.Font fnt;/*from w w w . jav a 2 s . c o m*/ if (bandElement != null) { fontName = (String) style.get(StyleFormatConstants.FONT_NAME_KEY); int size = ((Float) style.get(StyleFormatConstants.FONT_SIZE)).intValue(); fnt = getFont(size); } else { fnt = getFont(10); } PdfPCell cell; if (image) { if (value == null) { cell = new PdfPCell(new Phrase(IMAGE_NOT_FOUND)); } else { ImageBandElement ibe = (ImageBandElement) bandElement; try { byte[] imageBytes = getImage((String) value); cell = getImageCell(ibe, imageBytes, column, colSpan); } catch (Exception e) { cell = new PdfPCell(new Phrase(IMAGE_NOT_LOADED)); } } } else if (bandElement instanceof HyperlinkBandElement) { Hyperlink hyperlink = ((HyperlinkBandElement) bandElement).getHyperlink(); Anchor anchor = new Anchor(hyperlink.getText(), fnt); anchor.setReference(hyperlink.getUrl()); Phrase ph = new Phrase(); ph.add(anchor); cell = new PdfPCell(ph); } else if (bandElement instanceof ReportBandElement) { Report report = ((ReportBandElement) bandElement).getReport(); ExporterBean eb = null; try { eb = getSubreportExporterBean(report); PdfExporter subExporter = new PdfExporter(eb); subExporter.export(); PdfPTable innerTable = subExporter.getTable(); cell = new PdfPCell(innerTable); } catch (Exception e) { cell = new PdfPCell(); e.printStackTrace(); } finally { if ((eb != null) && (eb.getResult() != null)) { eb.getResult().close(); } } } else if ((bandElement instanceof VariableBandElement) && (VariableFactory .getVariable(((VariableBandElement) bandElement).getVariable()) instanceof TotalPageNoVariable)) { try { cell = new PdfPCell(Image.getInstance(total)); } catch (BadElementException e) { cell = new PdfPCell(new Phrase("NA")); } } else if (bandElement instanceof ImageColumnBandElement) { try { String v = StringUtil.getValueAsString(value, null); if (StringUtil.BLOB.equals(v)) { cell = new PdfPCell(new Phrase(StringUtil.BLOB)); } else { byte[] bytes = StringUtil.decodeImage(v); cell = getImageCell(bandElement, bytes, column, colSpan); } } catch (Exception e) { e.printStackTrace(); cell = new PdfPCell(new Phrase(IMAGE_NOT_LOADED)); } } else { String stringValue; if (style.containsKey(StyleFormatConstants.PATTERN)) { stringValue = StringUtil.getValueAsString(value, (String) style.get(StyleFormatConstants.PATTERN), getReportLanguage()); } else { stringValue = StringUtil.getValueAsString(value, null, getReportLanguage()); } if (stringValue == null) { stringValue = ""; } if (stringValue.startsWith("<html>")) { StringReader reader = new StringReader(stringValue); List<Element> elems = new ArrayList<Element>(); try { elems = HTMLWorker.parseToList(reader, new StyleSheet()); Phrase ph = new Phrase(); for (int i = 0; i < elems.size(); i++) { Element elem = (Element) elems.get(i); ph.add(elem); } cell = new PdfPCell(ph); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Phrase ph = new Phrase(stringValue, fnt); cell = new PdfPCell(ph); } } else { Phrase ph = new Phrase(stringValue, fnt); cell = new PdfPCell(ph); } } cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell.setUseDescender(true); // needed for a cell without padding cell.setMinimumHeight(MINIMUM_HEIGHT); // needed if there is a row in which all cells are empty if (bandElement != null) { cell.setRotation(bandElement.getTextRotation()); } if (colSpan > 1) { cell.setColspan(colSpan); } if (rowSpan > 1) { cell.setRowspan(rowSpan); } if (style != null) { updateFont(style, fnt); if (style.containsKey(StyleFormatConstants.BACKGROUND_COLOR)) { Color val = (Color) style.get(StyleFormatConstants.BACKGROUND_COLOR); cell.setBackgroundColor(new BaseColor(val)); } if (style.containsKey(StyleFormatConstants.HORIZONTAL_ALIGN_KEY)) { if (StyleFormatConstants.HORIZONTAL_ALIGN_LEFT .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) { cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); } if (StyleFormatConstants.HORIZONTAL_ALIGN_RIGHT .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) { cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); } if (StyleFormatConstants.HORIZONTAL_ALIGN_CENTER .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) { cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); } } if (style.containsKey(StyleFormatConstants.VERTICAL_ALIGN_KEY)) { if (StyleFormatConstants.VERTICAL_ALIGN_TOP .equals(style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) { cell.setVerticalAlignment(Element.ALIGN_TOP); } if (StyleFormatConstants.VERTICAL_ALIGN_MIDDLE .equals(style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) { cell.setVerticalAlignment(Element.ALIGN_MIDDLE); } if (StyleFormatConstants.VERTICAL_ALIGN_BOTTOM .equals(style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) { cell.setVerticalAlignment(Element.ALIGN_BOTTOM); } } if (style.containsKey(StyleFormatConstants.PADDING_LEFT)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_LEFT); cell.setPaddingLeft(val); } if (style.containsKey(StyleFormatConstants.PADDING_RIGHT)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_RIGHT); cell.setPaddingRight(val); } if (style.containsKey(StyleFormatConstants.PADDING_TOP)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_TOP); cell.setPaddingTop(val); } if (style.containsKey(StyleFormatConstants.PADDING_BOTTOM)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_BOTTOM); cell.setPaddingBottom(val); } cell.setBorderWidth(0); if (style.containsKey(StyleFormatConstants.BORDER_LEFT)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_LEFT); cell.setBorderWidthLeft(val / 2); Color color = (Color) style.get(StyleFormatConstants.BORDER_LEFT_COLOR); cell.setBorderColorLeft(new BaseColor(color)); } if (style.containsKey(StyleFormatConstants.BORDER_RIGHT)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_RIGHT); cell.setBorderWidthRight(val / 2); Color color = (Color) style.get(StyleFormatConstants.BORDER_RIGHT_COLOR); cell.setBorderColorRight(new BaseColor(color)); } if (style.containsKey(StyleFormatConstants.BORDER_TOP)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_TOP); cell.setBorderWidthTop(val / 2); Color color = (Color) style.get(StyleFormatConstants.BORDER_TOP_COLOR); cell.setBorderColorTop(new BaseColor(color)); } if (style.containsKey(StyleFormatConstants.BORDER_BOTTOM)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_BOTTOM); cell.setBorderWidthBottom(val / 2); Color color = (Color) style.get(StyleFormatConstants.BORDER_BOTTOM_COLOR); cell.setBorderColorBottom(new BaseColor(color)); } // for subreports we use default no wrap if (cell.getTable() == null) { cell.setNoWrap(true); if (bandElement != null) { if (bandElement.isWrapText()) { cell.setNoWrap(false); } } } // to see a background image all cells must not have any background! if (bean.getReportLayout().getBackgroundImage() != null) { cell.setBackgroundColor(null); } } return cell; }
From source file:timeclock.reports.EmployeeListReport.java
@Override public PdfPTable createTable(List list) { PdfPTable table = new PdfPTable(new float[] { 2, 3, 4, 1, 1 }); table.setWidthPercentage(100f);//from w ww. j av a 2s . c o m PdfPCell dCell = table.getDefaultCell(); dCell.setUseAscender(true); dCell.setUseDescender(true); dCell.setPadding(3); dCell.setHorizontalAlignment(Element.ALIGN_CENTER); dCell.setBackgroundColor(BaseColor.DARK_GRAY); table.addCell(new Phrase("Employee ID", MyFonts.font.get("header"))); table.addCell(new Phrase("First Name", MyFonts.font.get("header"))); table.addCell(new Phrase("Last Name", MyFonts.font.get("header"))); table.addCell(new Phrase("Salary", MyFonts.font.get("header"))); table.addCell(new Phrase("Admin", MyFonts.font.get("header"))); dCell.setBackgroundColor(null); table.setHeaderRows(1); table.setFooterRows(0); // used for alternating row color int rowCount = 0; // Display all employees for (Object obj : list) { Employee emp = (Employee) obj; rowCount++; dCell.setBackgroundColor((rowCount % 2 == 0 ? BaseColor.LIGHT_GRAY : null)); table.addCell(new Paragraph(Integer.toString(emp.getEmployeeID()), MyFonts.font.get("normal"))); table.addCell(new Paragraph(emp.getFirstName(), MyFonts.font.get("normal"))); table.addCell(new Paragraph(emp.getLastName(), MyFonts.font.get("normal"))); table.addCell(new Paragraph(emp.getIsSalary_bool() ? "Yes" : "No", MyFonts.font.get("normal"))); table.addCell(new Paragraph(emp.getIsAdmin_bool() ? "Yes" : "No", MyFonts.font.get("normal"))); } return table; }
From source file:timeclock.reports.EmployeeTimeReport.java
@Override public PdfPTable createTable(List list) { // Create table and set basic table properties PdfPTable table = new PdfPTable(new float[] { 1, 4, 4, 3 }); table.setWidthPercentage(100f);// w w w . j a va2 s .c o m PdfPCell dCell = table.getDefaultCell(); dCell.setUseAscender(true); dCell.setUseDescender(true); dCell.setPadding(3); dCell.setHorizontalAlignment(Element.ALIGN_CENTER); // Set header background color dCell.setBackgroundColor(BaseColor.DARK_GRAY); // Display the employee first in header table.addCell(new Phrase(Integer.toString(employee.getEmployeeID()), MyFonts.font.get("header"))); table.addCell(new Phrase(employee.getFirstName(), MyFonts.font.get("header"))); table.addCell(new Phrase(employee.getLastName(), MyFonts.font.get("header"))); table.addCell(new Phrase("", MyFonts.font.get("header"))); // Display the column names in header table.addCell(new Phrase("", MyFonts.font.get("header"))); table.addCell(new Phrase("Time In", MyFonts.font.get("header"))); table.addCell(new Phrase("Time Out", MyFonts.font.get("header"))); table.addCell(new Phrase("Time Worked", MyFonts.font.get("header"))); // Reset the cell background color dCell.setBackgroundColor(null); // Set the number of rows included in the header and footer table.setHeaderRows(2); table.setFooterRows(0); // Make a variable to hold the total of all time worked long totalTimeDiff = 0; // used for alternating row color int rowCount = 0; // Display all time punches for (Object obj : list) { TimePunch tp = (TimePunch) obj; rowCount++; dCell.setBackgroundColor((rowCount % 2 == 0 ? BaseColor.LIGHT_GRAY : null)); Timestamp ts1, ts2; ts1 = tp.getPunchInTimestamp(); ts2 = tp.getPunchOutTimestamp(); totalTimeDiff += ts2.getTime() - ts1.getTime(); String id = Integer.toString(tp.getPunchId()); String in = DateUtils.getDateTimeFromTimestamp(ts1); String out = DateUtils.getDateTimeFromTimestamp(ts2); String diff = DateUtils.getTimestampDiff_Str(ts1, ts2); // Add information to row table.addCell(new Paragraph("", MyFonts.font.get("normal"))); table.addCell(new Paragraph(in, MyFonts.font.get("normal"))); table.addCell(new Paragraph(out, MyFonts.font.get("normal"))); table.addCell(new Paragraph(diff, MyFonts.font.get("normal"))); } // Set background to null dCell.setBackgroundColor(null); // Get total time worked and create a totals row table.addCell(new Paragraph("", MyFonts.font.get("normal"))); table.addCell(new Paragraph("", MyFonts.font.get("normal"))); table.addCell(new Paragraph("Total:", MyFonts.font.get("bold"))); table.addCell(new Paragraph(DateUtils.getTimeStrFromLong(totalTimeDiff), MyFonts.font.get("bold"))); return table; }