Java tutorial
package com.datamyne.charts; /** * Copyright 2010 Jee Vang * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.data.general.DefaultPieDataset; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.DefaultFontMapper; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfTemplate; import com.itextpdf.text.pdf.PdfWriter; /** * Almost there demo in fixing UglyDemo. * * @author Jee Vang * */ public class AlmostThereDemo { /** * Gets a {@link JFreeChart}. * @return {@link JFreeChart}. */ public JFreeChart getChart() { //create dummy data //taken from http://www.java2s.com/Code/Java/Chart/JFreeChartPieChartDemo1.htm DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("One", new Double(43.2)); dataset.setValue("Two", new Double(10.0)); dataset.setValue("Three", new Double(27.5)); dataset.setValue("Four", new Double(17.5)); dataset.setValue("Five", new Double(11.0)); dataset.setValue("Six", new Double(19.4)); //use the ChartFactory to create a pie chart JFreeChart chart = ChartFactory.createPieChart("Dummy Data", dataset, true, true, false); return chart; } /** * Creates PDf file. * @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) { } } } } /** * Main method. * @param args No args required. * @throws FileNotFoundException * @throws DocumentException * @throws IOException */ public static void main(String[] args) throws FileNotFoundException, DocumentException, IOException { (new AlmostThereDemo()).create(new FileOutputStream(new File("/storage/tradeFiles/almost-there-demo.pdf"))); } }