List of usage examples for com.itextpdf.text.pdf PdfContentByte createTemplate
public PdfTemplate createTemplate(final float width, final float height)
From source file:com.centurylink.mdw.pdf.PdfExportHelper.java
License:Apache License
private void printGraph(DocWriter writer, ProcessCanvas canvas, Process process, Rectangle pageSize, Chapter chapter) throws Exception { Dimension graphsize = getGraphSize(process); // we create a template and a Graphics2D object that corresponds with it int w;/*from ww w. j a va2 s . co m*/ int h; float scale; if ((float) graphsize.width < pageSize.getWidth() * 0.8 && (float) graphsize.height < pageSize.getHeight() * 0.8) { w = graphsize.width + 36; h = graphsize.height + 36; scale = -1f; } else { scale = pageSize.getWidth() * 0.8f / (float) graphsize.width; if (scale > pageSize.getHeight() * 0.8f / (float) graphsize.height) scale = pageSize.getHeight() * 0.8f / (float) graphsize.height; w = (int) (graphsize.width * scale) + 36; h = (int) (graphsize.height * scale) + 36; } Image img; canvas.setBackground(Color.white); PdfContentByte cb = ((PdfWriter) writer).getDirectContent(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2 = tp.createGraphics(w, h); if (scale > 0) g2.scale(scale, scale); tp.setWidth(w); tp.setHeight(h); canvas.paintComponent(g2); g2.dispose(); img = new ImgTemplate(tp); chapter.add(img); }
From source file:com.datamyne.charts.AlmostThereDemo.java
License:Apache License
/** * Creates PDf file./*from w w w . j a va 2 s.c o m*/ * @param outputStream {@link OutputStream}. * @throws DocumentException * @throws IOException */ public void create(OutputStream outputStream) throws DocumentException, IOException { Document document = null; PdfWriter writer = null; try { //instantiate document and writer document = new Document(); writer = PdfWriter.getInstance(document, outputStream); //open document document.open(); //add image int width = 300; int height = 300; JFreeChart chart = getChart(); //create PdfContentByte //if you work with this object, you write to //the top most layer, meaning anything behind //will be clipped PdfContentByte contentByte = writer.getDirectContent(); //create PdfTemplate from PdfContentByte PdfTemplate template = contentByte.createTemplate(width, height); //create Graphics2D from PdfTemplate Graphics2D g2 = template.createGraphics(width, height, new DefaultFontMapper()); //setup the drawing area Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height); //pass the Graphics2D and drawing area to JFreeChart chart.draw(g2, r2D, null); g2.dispose(); //always dispose this //add the PdfTemplate to the PdfContentByte contentByte.addTemplate(template, 0, 300); //release resources document.close(); document = null; writer.close(); writer = null; } catch (DocumentException de) { throw de; } finally { //release resources if (null != document) { try { document.close(); } catch (Exception ex) { } } if (null != writer) { try { writer.close(); } catch (Exception ex) { } } } }
From source file:com.datamyne.charts.FinallyDemo.java
License:Apache License
/** * Creates PDf file.//from w ww . j av a2 s. c o m * @param outputStream {@link OutputStream}. * @throws DocumentException * @throws IOException */ public void create(OutputStream outputStream) throws DocumentException, IOException { Document document = null; PdfWriter writer = null; try { //instantiate document and writer document = new Document(); writer = PdfWriter.getInstance(document, outputStream); //open document document.open(); //get dummy text String text = getText(); //create text font com.itextpdf.text.Font font = new com.itextpdf.text.Font(FontFamily.TIMES_ROMAN, 10.0f); //add text before document.add(new Paragraph(new Chunk(text, font))); //add image int width = 300; int height = 300; JFreeChart chart = getChart(); //create PdfContentByte //if you work with this object, you write to //the top most layer, meaning anything behind //will be clipped PdfContentByte contentByte = writer.getDirectContent(); //create PdfTemplate from PdfContentByte PdfTemplate template = contentByte.createTemplate(width, height); //create Graphics2D from PdfTemplate Graphics2D g2 = template.createGraphics(width, height, new DefaultFontMapper()); //setup the drawing area Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height); //pass the Graphics2D and drawing area to JFreeChart chart.draw(g2, r2D, null); g2.dispose(); //always dispose this //create Image from PdfTemplate Image image = Image.getInstance(template); document.add(image); //add text after document.add(new Paragraph(new Chunk(text, font))); //release resources document.close(); document = null; writer.close(); writer = null; } catch (DocumentException de) { throw de; } finally { //release resources if (null != document) { try { document.close(); } catch (Exception ex) { } } if (null != writer) { try { writer.close(); } catch (Exception ex) { } } } }
From source file:com.github.luischavez.levsym.modulos.funcion.PDF.java
License:Open Source License
public void CreateTablePDF(JTable tabla) { boolean shapes = false; Document document = new Document(); Calendar c = Calendar.getInstance(); String date = Integer.toString(c.get(Calendar.DAY_OF_MONTH)) + "-" + Integer.toString(c.get(Calendar.MONTH)) + "-" + Integer.toString(c.get(Calendar.YEAR)) + " " + Integer.toString(c.get(Calendar.HOUR_OF_DAY)) + "-" + Integer.toString(c.get(Calendar.MINUTE)) + "-" + Integer.toString(c.get(Calendar.SECOND)); File Dir = new File(System.getProperty("user.dir") + "/Reportes/"); if (!Dir.exists()) { Dir.mkdirs();//from w w w .j av a 2 s .c om } try { PdfWriter writer; if (shapes) { writer = PdfWriter.getInstance(document, new FileOutputStream(System.getProperty("user.dir") + "/Reportes/Tabla " + date + ".pdf")); } else { writer = PdfWriter.getInstance(document, new FileOutputStream(System.getProperty("user.dir") + "/Reportes/Tabla " + date + ".pdf")); } document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(500, 500); Graphics2D g2; if (shapes) { g2 = tp.createGraphicsShapes(500, 500); } else { g2 = tp.createGraphics(500, 500); } g2.dispose(); cb.addTemplate(tp, 30, 300); } catch (Exception e) { Log.SaveLog(e.toString()); } document.close(); }
From source file:com.miraflorescarwash.controller.PdfController.java
private void crearPdfCombosReporte(Document doc, List<ComboReporte> combos, PdfWriter writer) { Paragraph parrafo;/* ww w . java2s .c o m*/ PdfPTable tabla; String txt; PdfPCell cell; Phrase frase; String fuente; int i; JFreeChart chart; int w, h; int pos; pos = 0; w = h = 500; fuente = "arial"; if (combos.isEmpty()) { return; } try { // // Se abre el documento. doc.open(); txt = "Miraflores Car Wash"; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 10, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_LEFT); doc.add(parrafo); txt = "Reporte de Combos"; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 30, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_CENTER); doc.add(parrafo); LineSeparator ls = new LineSeparator(); doc.add(new Chunk(ls)); tabla = new PdfPTable(3); frase = new Phrase("Id", FontFactory.getFont(fuente, 12, Font.BOLD, BaseColor.WHITE)); cell = new PdfPCell(frase); cell.setBackgroundColor(BaseColor.RED); tabla.addCell(cell); frase = new Phrase("Nombre", FontFactory.getFont(fuente, 12, Font.BOLD, BaseColor.WHITE)); cell = new PdfPCell(frase); cell.setBackgroundColor(BaseColor.RED); tabla.addCell(cell); frase = new Phrase("Dinero Recaudado", FontFactory.getFont(fuente, 12, Font.BOLD, BaseColor.WHITE)); cell = new PdfPCell(frase); cell.setBackgroundColor(BaseColor.RED); tabla.addCell(cell); i = 1; for (ComboReporte combo : combos) { if (i % 2 == 0) { cell = new PdfPCell(); cell.setBackgroundColor(new BaseColor(244, 119, 119)); frase = new Phrase(combo.getId() + ""); cell.setPhrase(frase); tabla.addCell(cell); frase = new Phrase(combo.getNombre()); cell.setPhrase(frase); tabla.addCell(cell); frase = new Phrase("S/. " + combo.getCantidad() + "0"); cell.setPhrase(frase); tabla.addCell(cell); } else { tabla.addCell(combo.getId() + ""); tabla.addCell(combo.getNombre()); tabla.addCell("S/. " + combo.getCantidad() + "0"); } i++; } doc.add(tabla); doc.newPage(); txt = "Miraflores Car Wash"; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 10, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_LEFT); doc.add(parrafo); chart = comboService.generarChartReporte(combos); w = 500; h = 500; PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2d = tp.createGraphics(w, h, new DefaultFontMapper()); Rectangle2D r2d = new Rectangle2D.Double(0, 0, w, h); chart.draw(g2d, r2d); cb.addTemplate(tp, 50, 250); g2d.dispose(); doc.close(); } catch (DocumentException ex) { } }
From source file:com.miraflorescarwash.controller.PdfController.java
private void crearPdfVentasReporte(Document doc, JFreeChart chart, PdfWriter writer, String fechaInicio, String fechaFin) {/*from w w w . ja v a 2 s. c o m*/ Paragraph parrafo; Phrase frase; String txt; String fuente; int w, h; fuente = "arial"; if (chart == null) { return; } try { // // Se abre el documento. doc.open(); txt = "Miraflores Car Wash"; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 10, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_LEFT); doc.add(parrafo); txt = "Reporte de Ventas"; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 30, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_CENTER); doc.add(parrafo); LineSeparator ls = new LineSeparator(); doc.add(new Chunk(ls)); doc.add(Chunk.NEWLINE); txt = "Inicio: "; frase = new Phrase(txt, FontFactory.getFont(fuente, 14, Font.BOLD, BaseColor.BLACK)); doc.add(frase); txt = fechaInicio; frase = new Phrase(txt, FontFactory.getFont(fuente, 14, Font.NORMAL, BaseColor.BLACK)); doc.add(frase); doc.add(Chunk.NEWLINE); txt = "Fin: "; frase = new Phrase(txt, FontFactory.getFont(fuente, 14, Font.BOLD, BaseColor.BLACK)); doc.add(frase); txt = fechaFin; frase = new Phrase(txt, FontFactory.getFont(fuente, 14, Font.NORMAL, BaseColor.BLACK)); doc.add(frase); w = 500; h = 500; PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2d = tp.createGraphics(w, h, new DefaultFontMapper()); Rectangle2D r2d = new Rectangle2D.Double(0, 0, w, h); chart.draw(g2d, r2d); cb.addTemplate(tp, 50, 50); g2d.dispose(); doc.close(); } catch (DocumentException ex) { } }
From source file:com.miraflorescarwash.controller.PdfController.java
private void crearPdfClientesFreq(Document doc, List<ClienteReporte> reporteCompras, PdfWriter writer) { Paragraph parrafo;//from w w w. jav a 2 s .c o m PdfPTable tabla; String txt; PdfPCell cell; Phrase frase; String fuente; JFreeChart chart; int i, w, h; fuente = "arial"; if (reporteCompras.isEmpty()) { return; } try { // // Se abre el documento. doc.open(); txt = "Miraflores Car Wash"; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 10, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_LEFT); doc.add(parrafo); txt = "Clientes Frecuentes"; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 30, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_CENTER); doc.add(parrafo); LineSeparator ls = new LineSeparator(); doc.add(new Chunk(ls)); doc.add(Chunk.NEWLINE); tabla = new PdfPTable(3); frase = new Phrase("Id", FontFactory.getFont(fuente, 12, Font.BOLD, BaseColor.WHITE)); cell = new PdfPCell(frase); cell.setBackgroundColor(BaseColor.RED); tabla.addCell(cell); frase = new Phrase("Nombre", FontFactory.getFont(fuente, 12, Font.BOLD, BaseColor.WHITE)); cell = new PdfPCell(frase); cell.setBackgroundColor(BaseColor.RED); tabla.addCell(cell); frase = new Phrase("Dinero", FontFactory.getFont(fuente, 12, Font.BOLD, BaseColor.WHITE)); cell = new PdfPCell(frase); cell.setBackgroundColor(BaseColor.RED); tabla.addCell(cell); i = 1; for (ClienteReporte clienteReporte : reporteCompras) { if (i % 2 == 0) { cell = new PdfPCell(); cell.setBackgroundColor(new BaseColor(244, 119, 119)); frase = new Phrase(clienteReporte.getId() + ""); cell.setPhrase(frase); tabla.addCell(cell); frase = new Phrase(clienteReporte.getApellido() + " " + clienteReporte.getNombre()); cell.setPhrase(frase); tabla.addCell(cell); frase = new Phrase("S/. " + clienteReporte.getTotal() + "0"); cell.setPhrase(frase); tabla.addCell(cell); } else { tabla.addCell(clienteReporte.getId() + ""); tabla.addCell(clienteReporte.getApellido() + " " + clienteReporte.getNombre()); tabla.addCell("S/. " + clienteReporte.getTotal() + "0"); } i++; } doc.add(tabla); doc.newPage(); txt = "Miraflores Car Wash"; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 10, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_LEFT); doc.add(parrafo); chart = clienteService.generarChartReporte(reporteCompras); w = 500; h = 500; PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2d = tp.createGraphics(w, h, new DefaultFontMapper()); Rectangle2D r2d = new Rectangle2D.Double(0, 0, w, h); chart.draw(g2d, r2d); cb.addTemplate(tp, 50, 250); g2d.dispose(); doc.close(); } catch (DocumentException ex) { } }
From source file:com.miraflorescarwash.controller.PdfController.java
private void crearPdfClientesEvolucion(Document doc, List<ClienteReporte> reporteCompras, PdfWriter writer) { Paragraph parrafo;//w ww .j av a 2 s . co m PdfPTable tabla; String txt; PdfPCell cell; Phrase frase; String fuente; JFreeChart chart; ClienteReporte cliente; int i, w, h; fuente = "arial"; if (reporteCompras.isEmpty()) { return; } try { // cliente = reporteCompras.get(0); // Se abre el documento. doc.open(); txt = "Miraflores Car Wash"; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 10, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_LEFT); doc.add(parrafo); txt = "Evolucin de Clientes "; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 30, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_CENTER); doc.add(parrafo); LineSeparator ls = new LineSeparator(); doc.add(new Chunk(ls)); txt = "Id: "; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 14, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_LEFT); doc.add(parrafo); txt = cliente.getId() + ""; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 14, Font.NORMAL, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_LEFT); doc.add(parrafo); // doc.add(Chunk.NEWLINE); txt = "Nombres: "; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 14, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_LEFT); doc.add(parrafo); txt = cliente.getNombre(); parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 14, Font.NORMAL, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_LEFT); doc.add(parrafo); // doc.add(Chunk.NEWLINE); txt = "Apellidos: "; parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 14, Font.BOLD, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_LEFT); doc.add(parrafo); txt = cliente.getApellido(); parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 14, Font.NORMAL, BaseColor.BLACK)); parrafo.setAlignment(Element.ALIGN_LEFT); doc.add(parrafo); // doc.add(Chunk.NEWLINE); chart = clienteService.generarChartReporteMes(reporteCompras); w = 500; h = 500; PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2d = tp.createGraphics(w, h, new DefaultFontMapper()); Rectangle2D r2d = new Rectangle2D.Double(0, 0, w, h); chart.draw(g2d, r2d); cb.addTemplate(tp, 50, 50); g2d.dispose(); doc.close(); } catch (DocumentException ex) { } }
From source file:com.rapidminer.gui.actions.ExportPdfAction.java
License:Open Source License
/** * Creates a pdf showing the given {@link Component} via {@link PdfTemplate} usage. * @param component/*w ww. j a v a 2 s . c o m*/ * @param document * @param cb * @throws DocumentException */ private void createPdfViaTemplate(Component component, Document document, PdfContentByte cb) throws DocumentException { PdfTemplate tp = cb.createTemplate(500, PageSize.A4.getHeight() / 2); Graphics2D g2 = tp.createGraphics(500, PageSize.A4.getHeight() / 2); // special handling for charts as we only want to export the chart but not the control panel // chart cannot be scaled to size of component because otherwise we would break the chart aspect-ratio if (component.getClass().isAssignableFrom(JPanel.class)) { JPanel panel = (JPanel) component; if (panel.getLayout().getClass().isAssignableFrom(CardLayout.class)) { for (final Component comp : panel.getComponents()) { // iterate over all card components and see if there is a chart which would require special handling // if not we don't do anything in this loop and do the standard behavior at the bottom of the method if (comp.isVisible() && ChartConfigurationPanel.class.isAssignableFrom(comp.getClass())) { final ChartConfigurationPanel chartConfigPanel = (ChartConfigurationPanel) comp; // create new LinkAndBrushChartPanel with double buffering set to false to get vector graphic export // The real chart has to use double buffering for a) performance and b) zoom rectangle drawing LinkAndBrushChartPanel newLaBPanel = new LinkAndBrushChartPanel( chartConfigPanel.getPlotEngine().getChartPanel().getChart(), chartConfigPanel.getPlotEngine().getChartPanel().getWidth(), chartConfigPanel.getPlotEngine().getChartPanel().getHeight(), chartConfigPanel.getPlotEngine().getChartPanel().getMinimumDrawWidth(), chartConfigPanel.getPlotEngine().getChartPanel().getMinimumDrawHeight(), false, false); newLaBPanel.setSize(chartConfigPanel.getPlotEngine().getChartPanel().getSize()); newLaBPanel .setOverlayList(chartConfigPanel.getPlotEngine().getChartPanel().getOverlayList()); AffineTransform at = new AffineTransform(); double factor = 500d / chartConfigPanel.getPlotEngine().getChartPanel().getWidth(); at.scale(factor, factor); g2.transform(at); newLaBPanel.print(g2); g2.dispose(); document.add(new Paragraph(componentName)); document.add(Image.getInstance(tp)); return; } else if (comp.isVisible() && PlotterPanel.class.isAssignableFrom(comp.getClass())) { // special case for PlotterPanel as the Panel itself is wider than the plotter // not having a special case here results in the exported image being too wide (empty space to the left) final PlotterPanel plotterPanel = (PlotterPanel) comp; AffineTransform at = new AffineTransform(); double factor = 500d / plotterPanel.getPlotterComponent().getWidth(); at.scale(factor, factor); g2.transform(at); plotterPanel.print(g2); g2.dispose(); document.add(new Paragraph(componentName)); document.add(Image.getInstance(tp)); return; } } } } AffineTransform at = new AffineTransform(); double factor = 500d / component.getWidth(); at.scale(factor, factor); g2.transform(at); component.print(g2); g2.dispose(); document.add(new Paragraph(componentName)); document.add(Image.getInstance(tp)); }
From source file:com.rapidminer.gui.actions.ExportPdfAction.java
License:Open Source License
/** * Creates a pdf showing the given {@link PlotterTemplate} via {@link PdfTemplate} usage. * @param template/*from w w w . j a va 2 s. c o m*/ * @param document * @param cb * @throws DocumentException */ private void createPdfViaTemplate(PlotterTemplate template, Document document, PdfContentByte cb) throws DocumentException { PdfTemplate tp = cb.createTemplate(500, PageSize.A4.getHeight() / 2); Graphics2D g2 = tp.createGraphics(500, PageSize.A4.getHeight() / 2); AffineTransform at = new AffineTransform(); double factor = 500d / template.getPlotEngine().getChartPanel().getWidth(); at.scale(factor, factor); g2.transform(at); template.getPlotEngine().getChartPanel().print(g2); g2.dispose(); document.add(new Paragraph(componentName)); document.add(Image.getInstance(tp)); }