SHOW:
|
|
- or go back to the newest paste.
| 1 | - | File pdfRootDirectory = new File(configRootDirectory.getAbsolutePath()+System.getProperty("file.separator")+"pdf");
|
| 1 | + | |
| 2 | private void saveChartAsPDF(JFreeChart chart, File fileName, int width, int height) throws IOException, TransformerException, FOPException {
| |
| 3 | File pdfRootDirectory = new File(configRootDirectory.getAbsolutePath()+System.getProperty("file.separator")+"pdf");
| |
| 4 | ||
| 5 | DOMImplementation domImpl= GenericDOMImplementation.getDOMImplementation(); | |
| 6 | // Create an instance of org.w3c.dom.Document | |
| 7 | Document document = domImpl.createDocument(null, "svg", null); | |
| 8 | // Create an instance of the SVG Generator | |
| 9 | SVGGraphics2D svgGenerator = new SVGGraphics2D(document); | |
| 10 | // set the precision to avoid a null pointer exception in Batik 1.5 | |
| 11 | svgGenerator.getGeneratorContext().setPrecision(6); | |
| 12 | // Ask the chart to render into the SVG Graphics2D implementation | |
| 13 | chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null); | |
| 14 | // Finally, stream out SVG to a file using UTF-8 character to | |
| 15 | // byte encoding | |
| 16 | boolean useCSS = true; | |
| 17 | Writer w = new OutputStreamWriter( | |
| 18 | new FileOutputStream(new File("test.svg")), "UTF-8");
| |
| 19 | svgGenerator.stream(w, useCSS); | |
| 20 | ||
| 21 | // <editor-fold defaultstate="collapsed" desc="Export chart as PNG file"> | |
| 22 | ChartUtilities.saveChartAsPNG(new File(pdfRootDirectory.getAbsolutePath()+ System.getProperty("file.separator") +"tmp.png"), chart, width, height);
| |
| 23 | // </editor-fold> | |
| 24 | ||
| 25 | File xslt = new File(pdfRootDirectory.getAbsolutePath() + System.getProperty("file.separator") + "xslt"+System.getProperty("file.separator")+"stylesheet.xsl");
| |
| 26 | File xml = new File(pdfRootDirectory.getAbsolutePath() + System.getProperty("file.separator") + "xml" + System.getProperty("file.separator") + "data.xml");
| |
| 27 | File fo = new File(pdfRootDirectory.getAbsolutePath() + System.getProperty("file.separator") + "fo" + System.getProperty("file.separator") + "temp.fo");
| |
| 28 | ||
| 29 | convertXML2FO(xml, xslt, fo); | |
| 30 | ||
| 31 | FopFactory fopFactory = FopFactory.newInstance(); | |
| 32 | ||
| 33 | OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName)); | |
| 34 | try {
| |
| 35 | Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out); | |
| 36 | ||
| 37 | TransformerFactory factory = TransformerFactory.newInstance(); | |
| 38 | Transformer transformer = factory.newTransformer(); | |
| 39 | ||
| 40 | Source src = new StreamSource(fo); | |
| 41 | ||
| 42 | Result res = new SAXResult(fop.getDefaultHandler()); | |
| 43 | ||
| 44 | transformer.transform(src, res); | |
| 45 | - | } |
| 45 | + | |
| 46 | out.close(); | |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 | ||
| 51 | /** | |
| 52 | * Converts an XML file to an XSL-FO file using JAXP (XSLT). | |
| 53 | * @see http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleXML2FO.java?view=markup | |
| 54 | * @param xml the XML file | |
| 55 | * @param xslt the stylesheet file | |
| 56 | * @param fo the target XSL-FO file | |
| 57 | * @throws IOException In case of an I/O problem | |
| 58 | * @throws TransformerException In case of a XSL transformation problem | |
| 59 | */ | |
| 60 | public void convertXML2FO(File xml, File xslt, File fo) throws IOException, TransformerException {
| |
| 61 | //Setup output | |
| 62 | OutputStream out = new java.io.FileOutputStream(fo); | |
| 63 | try {
| |
| 64 | //Setup XSLT | |
| 65 | TransformerFactory factory = TransformerFactory.newInstance(); | |
| 66 | Transformer transformer = factory.newTransformer(new StreamSource(xslt)); | |
| 67 | ||
| 68 | //Setup input for XSLT transformation | |
| 69 | Source src = new StreamSource(xml); | |
| 70 | ||
| 71 | //Resulting SAX events (the generated FO) must be piped through to FOP | |
| 72 | Result res = new StreamResult(out); | |
| 73 | ||
| 74 | //Start XSLT transformation and FOP processing | |
| 75 | transformer.transform(src, res); | |
| 76 | } finally {
| |
| 77 | out.close(); | |
| 78 | } | |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Method called when user clicks on save button of the panel interface | |
| 83 | * @param e | |
| 84 | */ | |
| 85 | public void actionPerformed(ActionEvent e) {
| |
| 86 | // <editor-fold defaultstate="collapsed" desc="Configure file chooser"> | |
| 87 | JFileChooser jFileChooser = new JFileChooser() {
| |
| 88 | ||
| 89 | @Override | |
| 90 | public void approveSelection() {
| |
| 91 | File f = getSelectedFile(); | |
| 92 | if (f.exists() && getDialogType() == SAVE_DIALOG) {
| |
| 93 | int result = JOptionPane.showConfirmDialog(this, "The file exists, overwrite?", "Existing file", JOptionPane.YES_NO_CANCEL_OPTION); | |
| 94 | switch (result) {
| |
| 95 | case JOptionPane.YES_OPTION: | |
| 96 | super.approveSelection(); | |
| 97 | return; | |
| 98 | case JOptionPane.NO_OPTION: | |
| 99 | return; | |
| 100 | case JOptionPane.CANCEL_OPTION: | |
| 101 | cancelSelection(); | |
| 102 | return; | |
| 103 | } | |
| 104 | } | |
| 105 | super.approveSelection(); | |
| 106 | } | |
| 107 | }; | |
| 108 | ||
| 109 | jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); | |
| 110 | jFileChooser.addChoosableFileFilter(new GenericFileFilter(new String[]{"pdf"}, "PDF Files"));
| |
| 111 | jFileChooser.setMultiSelectionEnabled(false); | |
| 112 | jFileChooser.setDialogType(JFileChooser.SAVE_DIALOG); | |
| 113 | // </editor-fold> | |
| 114 | ||
| 115 | if (jFileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
| |
| 116 | try {
| |
| 117 | File fileName = jFileChooser.getSelectedFile(); | |
| 118 | saveChartAsPDF(chart, fileName, 1000, 750); | |
| 119 | JOptionPane.showMessageDialog((Component) e.getSource(), "Graph successfully saved"); | |
| 120 | } catch (FOPException ex) {
| |
| 121 | Logger.getLogger(GraphController.class.getName()).log(Level.SEVERE, null, ex); | |
| 122 | } catch (TransformerException ex) {
| |
| 123 | Logger.getLogger(GraphController.class.getName()).log(Level.SEVERE, null, ex); | |
| 124 | } catch (IOException ex) {
| |
| 125 | Logger.getLogger(GraphController.class.getName()).log(Level.SEVERE, null, ex); | |
| 126 | JOptionPane.showMessageDialog((Component) e.getSource(), "An error occured with the file"); | |
| 127 | } | |
| 128 | } else {
| |
| 129 | System.out.println("Command cancelled by user");
| |
| 130 | } | |
| 131 | } |