List of usage examples for com.itextpdf.text.pdf PdfTemplate createGraphics
public java.awt.Graphics2D createGraphics(final float width, final float height, final FontMapper fontMapper)
Graphics2D
to write on. From source file:Servlet3.java
/** * Handles the HTTP <code>GET</code> method. * * @param request servlet request/* w w w.j ava 2 s.c o m*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { System.out.println("inside servlet"); String a = request.getParameter("countryf"); String c = request.getParameter("submit"); String b = request.getParameter("paramf"); //code added by Murugappan String CurentUID = request.getParameter("UIDvalue2f"); String URLRequest = request.getRequestURL().append('?').append(request.getQueryString()).toString(); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 1); SimpleDateFormat format1 = new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy"); String date1 = cal.getTime().toString(); System.out.println("inside servlet"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:server"); //iserting data to UserActivity table Statement sthistoryinsert3 = con.createStatement(); String insertstring = "Insert into UserActivity values('" + CurentUID + "','" + date1 + "','Future Data Forecast','" + a + "','" + b + "','" + URLRequest + "')"; sthistoryinsert3.executeUpdate(insertstring); sthistoryinsert3.close(); System.out.println("\n Step 1"); Statement st = con.createStatement(); // Statement st2 = con.createStatement(); XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries series = new XYSeries(b); String query = "SELECT [2000],[2012] FROM country where CountryName='" + a + "' AND SeriesName='" + b + "'"; System.out.println(query); ResultSet rs = st.executeQuery(query); if (rs == null) System.out.println("\n no rows "); else System.out.println("Rows present "); rs.next(); Double start = Double.parseDouble(rs.getString(1)); Double end = Double.parseDouble(rs.getString(2)); Double period = 13.0; Double growth = Math.pow((end / start), (1 / period)) - 1; System.out.println("growth percentage =" + growth); rs.close(); String query2 = "select [2011],[2012] from country where CountryName='" + a + "' AND SeriesName='" + b + "'"; rs = st.executeQuery(query2); rs.next(); series.add(2011, Double.parseDouble(rs.getString(1))); Double second = Double.parseDouble(rs.getString(2)); series.add(2012, second); Double growthvalue = second + (second * growth); series.add(2013, growthvalue); for (int i = 2014; i <= 2016; i++) { System.out.println("actual growth value = " + growthvalue); series.add((i++), (growthvalue + growthvalue * growth)); growthvalue = growthvalue + growthvalue * growth; } rs.close(); dataset.addSeries(series); DecimalFormat format_2Places = new DecimalFormat("0.00"); growth = growth * 100; growth = Double.valueOf(format_2Places.format(growth)); JFreeChart chart = ChartFactory.createXYLineChart( "Energy forecasting for " + a + " based on " + b + " with growth value estimated at " + growth + "% ", "Year", "Energy consumed in millions", dataset, PlotOrientation.VERTICAL, true, true, false); // JFreeChart chart1=ChartFactory.createLineChart("World population Growth","Year","population in millions",dataSet1,PlotOrientation.VERTICAL,true,true,false); ByteArrayOutputStream bos = new ByteArrayOutputStream(); chart.setBackgroundPaint(Color.white); final XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(Color.white); plot.setDomainGridlinesVisible(true); plot.setRangeGridlinesVisible(true); plot.setDomainGridlinePaint(Color.black); plot.setRangeGridlinePaint(Color.black); final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); renderer.setSeriesLinesVisible(2, false); renderer.setSeriesShapesVisible(2, false); plot.setRenderer(renderer); /* We have to insert this colored Pie Chart into the PDF file using iText now */ if (c.equals("View Graph in Browser")) { ChartUtilities.writeChartAsPNG(bos, chart, 700, 500); response.setContentType("image/png"); OutputStream out = new BufferedOutputStream(response.getOutputStream()); out.write(bos.toByteArray()); out.flush(); out.close(); } else { int width = 640; /* Width of our chart */ int height = 480; /* Height of our chart */ Document PieChart = new Document(new com.itextpdf.text.Rectangle(width, height)); java.util.Date date = new java.util.Date(); String chartname = "My_Colored_Chart" + date.getTime() + ".pdf"; PdfWriter writer = PdfWriter.getInstance(PieChart, new FileOutputStream(chartname)); PieChart.open(); /* Add some Metadata to identify document later */ PieChart.addTitle("How to color your Pie Chart and embed in a PDF file using iText"); PieChart.addAuthor("Thinktibits"); PieChart.addKeywords("iText,Color PieChart,JFreeChart,PDF,Example Tutorial"); PdfContentByte Add_Chart_Content = writer.getDirectContent(); PdfTemplate template_Chart_Holder = Add_Chart_Content.createTemplate(width, height); Graphics2D Graphics_Chart = template_Chart_Holder.createGraphics(width, height, new DefaultFontMapper()); Rectangle2D Chart_Region = new Rectangle2D.Double(0, 0, 540, 380); chart.draw(Graphics_Chart, Chart_Region); Graphics_Chart.dispose(); Add_Chart_Content.addTemplate(template_Chart_Holder, 0, 0); PieChart.close(); //PrintWriter out = response.getWriter(); //out.println("<!DOCTYPE html> <html> <body> <a href =file:///C:/apache-tomcat-8.0.12/bin/"+chartname+" download=newFileName> Download your file here </a> </body></html>"); PdfReader reader = new PdfReader(chartname); PdfStamper stamper = null; try { stamper = new PdfStamper(reader, bos); } catch (DocumentException e) { e.printStackTrace(); } try { stamper.close(); } catch (DocumentException e) { e.printStackTrace(); } // set some response headers response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setContentType("application/pdf"); response.setContentLength(bos.size()); OutputStream os = response.getOutputStream(); bos.writeTo(os); os.flush(); os.close(); } } catch (Exception i) { i.printStackTrace(); } }
From source file:by.bsuir.group172301.matskevich.tour.util.PDFCreator.java
public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName, int col1, int col2, double perc) { PdfWriter writer = null;/*w ww .java 2 s . c om*/ Document document = new Document(); try { writer = PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); Paragraph paragraph = new Paragraph(); // We add one empty line paragraph = new Paragraph("Results:"); paragraph.setAlignment(Element.ALIGN_CENTER); document.add(paragraph); paragraph = new Paragraph("Test before: " + col1); document.add(paragraph); paragraph = new Paragraph("Test after: " + col2); document.add(paragraph); paragraph = new Paragraph("Percentage: " + perc); document.add(paragraph); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(width, height); Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height); chart.draw(graphics2d, rectangle2d); graphics2d.dispose(); contentByte.addTemplate(template, 150, 250); } catch (Exception e) { e.printStackTrace(); } document.close(); }
From source file:ca.uwo.csd.cs2212.team02.MainWindow.java
/** * Export current page to PDF/* w w w . jav a 2 s. co m*/ * * @param jpanel Panel to be exported to PDF */ public void createPDF(JLayeredPane jpanel) { playButtonSound(); com.itextpdf.text.Rectangle r = new com.itextpdf.text.Rectangle(0, 0, jpanel.getWidth(), jpanel.getHeight()); Document doc = new Document(r); String filename = fileChooser(); if (filename != "INVALID") { try { PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(filename)); doc.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(PageSize.A4.getHeight() * 2, PageSize.A4.getWidth() * 2); Graphics2D g2; g2 = tp.createGraphics(jpanel.getWidth(), jpanel.getHeight(), new DefaultFontMapper()); jpanel.print(g2); g2.dispose(); cb.addTemplate(tp, 0, 0); PopUpWindow saveSuccess = new PopUpWindow(filename, "Save Successful!", 8); } catch (Exception e) { new PopUpWindow("Please try again.", "Error saving file"); } } doc.close(); }
From source file:canreg.client.analysis.Tools.java
License:Open Source License
public static void exportChartAsPDF(JFreeChart chart, Rectangle bounds, File file) throws IOException, DocumentException { System.out.println(file.getPath()); PdfWriter writer = null;//from w ww.ja v a 2 s . c o m com.itextpdf.text.Document document = new com.itextpdf.text.Document(); document.addCreator("CanReg5"); document.addCreationDate(); writer = PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(bounds.width, bounds.height); Graphics2D graphics2d = template.createGraphics(bounds.width, bounds.height, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, bounds.width, bounds.height); chart.draw(graphics2d, rectangle2d); graphics2d.dispose(); contentByte.addTemplate(template, 0, 0); document.close(); }
From source file:com.datamyne.charts.AlmostThereDemo.java
License:Apache License
/** * Creates PDf file./*ww w . j a v a 2s.co 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./* www.ja 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(); //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.miraflorescarwash.controller.PdfController.java
private void crearPdfCombosReporte(Document doc, List<ComboReporte> combos, PdfWriter writer) { Paragraph parrafo;//from www .j a va 2 s . 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;// w w w .j a va 2s . 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 w w.j a v a2s. c o 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) { } }