001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2014, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ---------------- 028 * ExportUtils.java 029 * ---------------- 030 * (C) Copyright 2014, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes: 036 * -------- 037 * 27-Jun-2014 : Version 1 (DG); 038 * 039 */ 040 041package org.jfree.chart.util; 042 043import java.awt.Graphics2D; 044import java.awt.Rectangle; 045import java.awt.geom.Rectangle2D; 046import java.awt.image.BufferedImage; 047import java.io.BufferedOutputStream; 048import java.io.File; 049import java.io.FileNotFoundException; 050import java.io.FileOutputStream; 051import java.io.IOException; 052import java.io.OutputStream; 053import java.lang.reflect.Constructor; 054import java.lang.reflect.InvocationTargetException; 055import java.lang.reflect.Method; 056import javax.imageio.ImageIO; 057import org.jfree.ui.Drawable; 058 059/** 060 * Utility functions for exporting charts to SVG and PDF format. 061 * 062 * @since 1.0.18 063 */ 064public class ExportUtils { 065 066 /** 067 * Returns <code>true</code> if JFreeSVG is on the classpath, and 068 * <code>false</code> otherwise. The JFreeSVG library can be found at 069 * http://www.jfree.org/jfreesvg/ 070 * 071 * @return A boolean. 072 */ 073 public static boolean isJFreeSVGAvailable() { 074 Class<?> svgClass = null; 075 try { 076 svgClass = Class.forName("org.jfree.graphics2d.svg.SVGGraphics2D"); 077 } catch (ClassNotFoundException e) { 078 // svgClass will be null so the function will return false 079 } 080 return (svgClass != null); 081 } 082 083 /** 084 * Returns <code>true</code> if OrsonPDF is on the classpath, and 085 * <code>false</code> otherwise. The OrsonPDF library can be found at 086 * http://www.object-refinery.com/pdf/ 087 * 088 * @return A boolean. 089 */ 090 public static boolean isOrsonPDFAvailable() { 091 Class<?> pdfDocumentClass = null; 092 try { 093 pdfDocumentClass = Class.forName("com.orsonpdf.PDFDocument"); 094 } catch (ClassNotFoundException e) { 095 // pdfDocument class will be null so the function will return false 096 } 097 return (pdfDocumentClass != null); 098 } 099 100 /** 101 * Writes the current content to the specified file in SVG format. This 102 * will only work when the JFreeSVG library is found on the classpath. 103 * Reflection is used to ensure there is no compile-time dependency on 104 * JFreeSVG. 105 * 106 * @param drawable the drawable (<code>null</code> not permitted). 107 * @param w the chart width. 108 * @param h the chart height. 109 * @param file the output file (<code>null</code> not permitted). 110 */ 111 public static void writeAsSVG(Drawable drawable, int w, int h, 112 File file) { 113 if (!ExportUtils.isJFreeSVGAvailable()) { 114 throw new IllegalStateException( 115 "JFreeSVG is not present on the classpath."); 116 } 117 ParamChecks.nullNotPermitted(drawable, "drawable"); 118 ParamChecks.nullNotPermitted(file, "file"); 119 try { 120 Class<?> svg2Class = Class.forName( 121 "org.jfree.graphics2d.svg.SVGGraphics2D"); 122 Constructor<?> c1 = svg2Class.getConstructor(int.class, int.class); 123 Graphics2D svg2 = (Graphics2D) c1.newInstance(w, h); 124 Rectangle2D drawArea = new Rectangle2D.Double(0, 0, w, h); 125 drawable.draw(svg2, drawArea); 126 Class<?> svgUtilsClass = Class.forName( 127 "org.jfree.graphics2d.svg.SVGUtils"); 128 Method m1 = svg2Class.getMethod("getSVGElement", (Class[]) null); 129 String element = (String) m1.invoke(svg2, (Object[]) null); 130 Method m2 = svgUtilsClass.getMethod("writeToSVG", File.class, 131 String.class); 132 m2.invoke(svgUtilsClass, file, element); 133 } catch (ClassNotFoundException ex) { 134 throw new RuntimeException(ex); 135 } catch (InstantiationException ex) { 136 throw new RuntimeException(ex); 137 } catch (IllegalAccessException ex) { 138 throw new RuntimeException(ex); 139 } catch (NoSuchMethodException ex) { 140 throw new RuntimeException(ex); 141 } catch (SecurityException ex) { 142 throw new RuntimeException(ex); 143 } catch (IllegalArgumentException ex) { 144 throw new RuntimeException(ex); 145 } catch (InvocationTargetException ex) { 146 throw new RuntimeException(ex); 147 } 148 } 149 150 /** 151 * Writes a {@link Drawable} to the specified file in PDF format. This 152 * will only work when the OrsonPDF library is found on the classpath. 153 * Reflection is used to ensure there is no compile-time dependency on 154 * OrsonPDF. 155 * 156 * @param drawable the drawable (<code>null</code> not permitted). 157 * @param w the chart width. 158 * @param h the chart height. 159 * @param file the output file (<code>null</code> not permitted). 160 */ 161 public static final void writeAsPDF(Drawable drawable, 162 int w, int h, File file) { 163 if (!ExportUtils.isOrsonPDFAvailable()) { 164 throw new IllegalStateException( 165 "OrsonPDF is not present on the classpath."); 166 } 167 ParamChecks.nullNotPermitted(drawable, "drawable"); 168 ParamChecks.nullNotPermitted(file, "file"); 169 try { 170 Class<?> pdfDocClass = Class.forName("com.orsonpdf.PDFDocument"); 171 Object pdfDoc = pdfDocClass.newInstance(); 172 Method m = pdfDocClass.getMethod("createPage", Rectangle2D.class); 173 Rectangle2D rect = new Rectangle(w, h); 174 Object page = m.invoke(pdfDoc, rect); 175 Method m2 = page.getClass().getMethod("getGraphics2D"); 176 Graphics2D g2 = (Graphics2D) m2.invoke(page); 177 Rectangle2D drawArea = new Rectangle2D.Double(0, 0, w, h); 178 drawable.draw(g2, drawArea); 179 Method m3 = pdfDocClass.getMethod("writeToFile", File.class); 180 m3.invoke(pdfDoc, file); 181 } catch (ClassNotFoundException ex) { 182 throw new RuntimeException(ex); 183 } catch (InstantiationException ex) { 184 throw new RuntimeException(ex); 185 } catch (IllegalAccessException ex) { 186 throw new RuntimeException(ex); 187 } catch (NoSuchMethodException ex) { 188 throw new RuntimeException(ex); 189 } catch (SecurityException ex) { 190 throw new RuntimeException(ex); 191 } catch (IllegalArgumentException ex) { 192 throw new RuntimeException(ex); 193 } catch (InvocationTargetException ex) { 194 throw new RuntimeException(ex); 195 } 196 } 197 198 /** 199 * Writes the current content to the specified file in PNG format. 200 * 201 * @param drawable the drawable (<code>null</code> not permitted). 202 * @param w the chart width. 203 * @param h the chart height. 204 * @param file the output file (<code>null</code> not permitted). 205 * 206 * @throws FileNotFoundException if the file is not found. 207 * @throws IOException if there is an I/O problem. 208 */ 209 public static void writeAsPNG(Drawable drawable, int w, int h, 210 File file) throws FileNotFoundException, IOException { 211 BufferedImage image = new BufferedImage(w, h, 212 BufferedImage.TYPE_INT_ARGB); 213 Graphics2D g2 = image.createGraphics(); 214 drawable.draw(g2, new Rectangle(w, h)); 215 OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 216 try { 217 ImageIO.write(image, "png", out); 218 } 219 finally { 220 out.close(); 221 } 222 } 223 224 /** 225 * Writes the current content to the specified file in JPEG format. 226 * 227 * @param drawable the drawable (<code>null</code> not permitted). 228 * @param w the chart width. 229 * @param h the chart height. 230 * @param file the output file (<code>null</code> not permitted). 231 * 232 * @throws FileNotFoundException if the file is not found. 233 * @throws IOException if there is an I/O problem. 234 */ 235 public static void writeAsJPEG(Drawable drawable, int w, int h, 236 File file) throws FileNotFoundException, IOException { 237 BufferedImage image = new BufferedImage(w, h, 238 BufferedImage.TYPE_INT_RGB); 239 Graphics2D g2 = image.createGraphics(); 240 drawable.draw(g2, new Rectangle(w, h)); 241 OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 242 try { 243 ImageIO.write(image, "jpg", out); 244 } 245 finally { 246 out.close(); 247 } 248 } 249 250}