Font font = FontFactory.getFont("/fonts/Sansation_Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 0.8f, Font.NORMAL, BaseColor.BLACK); BaseFont baseFont = font.getBaseFont();
Phrase content = new Phrase("Blah blah blah", Font); Float fontSize = content.getFont().getSize(); Float capHeight = content.getFont().getBaseFont().getFontDescriptor(BaseFont.CAPHEIGHT, fontSize); Float padding = 5f; PdfPCell cell = new PdfPCell(content); cell.setPadding(padding); cell.setPaddingTop(capHeight - fontSize + padding);
int width=paper.getWidth(); int height=paper.getHeight(); Rectangle pageSize=new RectangleReadOnly(width,height); if(paper.getOrientation().equals(Orientation.landscape)){ pageSize=pageSize.rotate(); int topMargin=paper.getTopMargin(); int bottomMargin=paper.getBottomMargin(); Document document=new Document(pageSize,leftMargin,rightMargin,topMargin,bottomMargin); try{ PdfWriter writer=PdfWriter.getInstance(document,outputStream); PageHeaderFooterEvent headerFooterEvent=new PageHeaderFooterEvent(report); writer.setPageEvent(headerFooterEvent); document.open(); List<Column> columns=report.getColumns(); List<Integer> columnsWidthList=new ArrayList<Integer>(); float tableHeight=pageSize.getHeight()-paper.getTopMargin()-paper.getBottomMargin(); Map<Row, Map<Column, Cell>> cellMap=report.getRowColCellMap(); for(List<Page> pages:list){ PdfPTable table=new PdfPTable(size); table.setLockedWidth(true); table.setTotalWidth(w); table.setWidths(widths); table.setHorizontalAlignment(Element.ALIGN_LEFT); int ps=pages.size(); for(int i=0;i<ps;i++){ if(i>0){ PdfPCell pdfMarginCell=new PdfPCell();
Document document = new Document(PageSize.A4); PdfDocument pdf = new PdfDocument(); document.addDocListener(pdf); PdfWriter writer = PdfWriter.getInstance(pdf, outputStream); pdf.addWriter(writer); document.open(); Rectangle pageSize = document.getPageSize(); int w = (int) (pageSize.getWidth() * 0.9); int h = (int) (pageSize.getHeight() * 0.95); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2 = tp.createPrinterGraphics(w, h, null); tp.setWidth(w); tp.setHeight(h); float offsetX = (float) ((pageSize.getWidth() - w) / 2); float offsetY = (float) ((pageSize.getHeight() - h) / 2); cb.addTemplate(tp, offsetX, offsetY); document.close();
public static void render(Drawable drawable, Rectangle2D bounds, File file) { initialize(); Rectangle size = new Rectangle((float) bounds.getWidth(), (float) bounds.getHeight()); Document document = new Document(size); FileOutputStream fos; try { fos = new FileOutputStream(file); } catch (FileNotFoundException e) { throw new RuntimeException("The file " + file + "could not be created", e); } PdfWriter writer; try { writer = PdfWriter.getInstance(document, fos); } catch (DocumentException e) { throw new RuntimeException("An error occurred while creating a PdfWriter object.", e); } document.open(); PdfContentByte contentByte = writer.getDirectContent(); Graphics2D g = new PdfGraphics2D(contentByte, (float) bounds.getWidth(), (float) bounds.getHeight(), fontMapper); g.translate(-bounds.getX(), -bounds.getY()); drawable.draw(g); g.dispose(); document.close(); }
private PdfPCell buildPdfPCell(HeaderFooter phf,String text,int type){ PdfPCell cell=new PdfPCell(); cell.setPadding(0); cell.setBorder(Rectangle.NO_BORDER); Font font=FontBuilder.getFont(phf.getFontFamily(), phf.getFontSize(), phf.isBold(), phf.isItalic(),phf.isUnderline()); String fontColor=phf.getForecolor(); if(StringUtils.isNotEmpty(fontColor)){ String[] color=fontColor.split(","); font.setColor(Integer.valueOf(color[0]), Integer.valueOf(color[1]), Integer.valueOf(color[2])); } Paragraph graph=new Paragraph(text,font); cell.setPhrase(graph); switch(type){ case 1: cell.setHorizontalAlignment(Element.ALIGN_LEFT); break; case 2: cell.setHorizontalAlignment(Element.ALIGN_CENTER); break; case 3: cell.setHorizontalAlignment(Element.ALIGN_RIGHT); break; } cell.setVerticalAlignment(Element.ALIGN_MIDDLE); return cell; } }
CellStyle colStyle=cellInfo.getColumn().getCustomCellStyle(); PdfPCell cell=newPdfCell(cellInfo,cellHeight); cell.setPadding(0); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new CellBorderEvent(style,customStyle)); int rowSpan=cellInfo.getPageRowSpan(); if(rowSpan>0){ cell.setRowspan(rowSpan); cell.setColspan(colSpan); cell.setHorizontalAlignment(Element.ALIGN_LEFT); }else if(align.equals(Alignment.center)){ cell.setHorizontalAlignment(Element.ALIGN_CENTER); }else if(align.equals(Alignment.right)){ cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_TOP); }else if(valign.equals(Alignment.middle)){ cell.setVerticalAlignment(Element.ALIGN_MIDDLE); }else if(valign.equals(Alignment.bottom)){ cell.setVerticalAlignment(Element.ALIGN_BOTTOM); cell.setBackgroundColor(new BaseColor(Integer.valueOf(colors[0]),Integer.valueOf(colors[1]),Integer.valueOf(colors[2])));
cb.moveTo(position.getLeft(), position.getTop()); cb.lineTo(position.getLeft(), position.getBottom()); cb.stroke(); if(leftBorder.getStyle().equals(BorderStyle.doublesolid)){ cb.moveTo(position.getLeft()+2, position.getTop()-2); cb.lineTo(position.getLeft()+2, position.getBottom()+2); cb.stroke(); cb.restoreState(); cb.moveTo(position.getLeft(), position.getTop()); cb.lineTo(position.getRight(), position.getTop()); cb.stroke(); if(topBorder.getStyle().equals(BorderStyle.doublesolid)){ cb.moveTo(position.getLeft()+2, position.getTop()-2); cb.lineTo(position.getRight()-2, position.getTop()-2); cb.stroke(); cb.restoreState(); cb.moveTo(position.getRight(), position.getTop()); cb.lineTo(position.getRight(), position.getBottom()); cb.stroke(); if(rightBorder.getStyle().equals(BorderStyle.doublesolid)){ cb.moveTo(position.getRight()-2, position.getTop()-2); cb.lineTo(position.getRight()-2, position.getBottom()+2); cb.stroke(); cb.restoreState();
/** * Adds the producer to a Document. * * @return <CODE>true</CODE> if successful, <CODE>false</CODE> otherwise */ public boolean addProducer() { try { return add(new Meta(Element.PRODUCER, Version.getInstance().getVersion())); } catch (DocumentException de) { throw new ExceptionConverter(de); } }
private PdfContentByte bulidCellBorder(PdfContentByte[] canvases,Border border){ PdfContentByte cb=canvases[PdfPTable.LINECANVAS]; cb.saveState(); BigDecimal w=new BigDecimal(border.getWidth()); cb.setLineWidth(w.divide(new BigDecimal(2),10,RoundingMode.HALF_UP).floatValue()); if(border.getStyle().equals(BorderStyle.dashed)){ cb.setLineDash(new float[]{2f,3f,1f},2); } String borderColor[]=border.getColor().split(","); cb.setColorStroke(new BaseColor(Integer.valueOf(borderColor[0]),Integer.valueOf(borderColor[1]),Integer.valueOf(borderColor[2]))); return cb; } }
private com.itextpdf.text.Image buildPdfImage(String base64Data, int width,int height) throws Exception{ com.itextpdf.text.Image pdfImg=null; InputStream input=ImageUtils.base64DataToInputStream(base64Data); try{ byte[] bytes=IOUtils.toByteArray(input); pdfImg=com.itextpdf.text.Image.getInstance(bytes); float imgWidth=pdfImg.getWidth(); float imgHeight=pdfImg.getHeight(); if(width==0){ width=Float.valueOf(imgWidth).intValue(); } if(height==0){ height=Float.valueOf(imgHeight).intValue(); } width=UnitUtils.pixelToPoint(width-2); height=UnitUtils.pixelToPoint(height-2); pdfImg.scaleToFit(width,height); }finally{ IOUtils.closeQuietly(input); } return pdfImg; } }
font.setColor(Integer.valueOf(color[0]), Integer.valueOf(color[1]), Integer.valueOf(color[2]));
/** * Adds the producer to a Document. * * @return <CODE>true</CODE> if successful, <CODE>false</CODE> otherwise */ public boolean addProducer() { try { return add(new Meta(Element.PRODUCER, Version.getInstance().getVersion())); } catch (DocumentException de) { throw new ExceptionConverter(de); } }
/** * Makes chunk content bold. * @param chunk The chunk whose content is to be rendered bold. * @param color The font color desired. */ private void setBoldStyle(Chunk chunk, BaseColor color) { Font font = chunk.getFont(); chunk.setFont(FontFactory.getFont(font.getFamilyname(), font.getSize(), Font.BOLD, color)); }