Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void saveChartAsPDF(JFreeChart chart, File fileName, int width, int height) throws IOException, TransformerException, FOPException {
- File pdfRootDirectory = new File(configRootDirectory.getAbsolutePath()+System.getProperty("file.separator")+"pdf");
- DOMImplementation domImpl= GenericDOMImplementation.getDOMImplementation();
- // Create an instance of org.w3c.dom.Document
- Document document = domImpl.createDocument(null, "svg", null);
- // Create an instance of the SVG Generator
- SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
- // set the precision to avoid a null pointer exception in Batik 1.5
- svgGenerator.getGeneratorContext().setPrecision(6);
- // Ask the chart to render into the SVG Graphics2D implementation
- chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null);
- // Finally, stream out SVG to a file using UTF-8 character to
- // byte encoding
- boolean useCSS = true;
- Writer w = new OutputStreamWriter(
- new FileOutputStream(new File("test.svg")), "UTF-8");
- svgGenerator.stream(w, useCSS);
- // <editor-fold defaultstate="collapsed" desc="Export chart as PNG file">
- ChartUtilities.saveChartAsPNG(new File(pdfRootDirectory.getAbsolutePath()+ System.getProperty("file.separator") +"tmp.png"), chart, width, height);
- // </editor-fold>
- File xslt = new File(pdfRootDirectory.getAbsolutePath() + System.getProperty("file.separator") + "xslt"+System.getProperty("file.separator")+"stylesheet.xsl");
- File xml = new File(pdfRootDirectory.getAbsolutePath() + System.getProperty("file.separator") + "xml" + System.getProperty("file.separator") + "data.xml");
- File fo = new File(pdfRootDirectory.getAbsolutePath() + System.getProperty("file.separator") + "fo" + System.getProperty("file.separator") + "temp.fo");
- convertXML2FO(xml, xslt, fo);
- FopFactory fopFactory = FopFactory.newInstance();
- OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
- try {
- Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
- TransformerFactory factory = TransformerFactory.newInstance();
- Transformer transformer = factory.newTransformer();
- Source src = new StreamSource(fo);
- Result res = new SAXResult(fop.getDefaultHandler());
- transformer.transform(src, res);
- } finally {
- out.close();
- }
- }
- /**
- * Converts an XML file to an XSL-FO file using JAXP (XSLT).
- * @see http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleXML2FO.java?view=markup
- * @param xml the XML file
- * @param xslt the stylesheet file
- * @param fo the target XSL-FO file
- * @throws IOException In case of an I/O problem
- * @throws TransformerException In case of a XSL transformation problem
- */
- public void convertXML2FO(File xml, File xslt, File fo) throws IOException, TransformerException {
- //Setup output
- OutputStream out = new java.io.FileOutputStream(fo);
- try {
- //Setup XSLT
- TransformerFactory factory = TransformerFactory.newInstance();
- Transformer transformer = factory.newTransformer(new StreamSource(xslt));
- //Setup input for XSLT transformation
- Source src = new StreamSource(xml);
- //Resulting SAX events (the generated FO) must be piped through to FOP
- Result res = new StreamResult(out);
- //Start XSLT transformation and FOP processing
- transformer.transform(src, res);
- } finally {
- out.close();
- }
- }
- /**
- * Method called when user clicks on save button of the panel interface
- * @param e
- */
- public void actionPerformed(ActionEvent e) {
- // <editor-fold defaultstate="collapsed" desc="Configure file chooser">
- JFileChooser jFileChooser = new JFileChooser() {
- @Override
- public void approveSelection() {
- File f = getSelectedFile();
- if (f.exists() && getDialogType() == SAVE_DIALOG) {
- int result = JOptionPane.showConfirmDialog(this, "The file exists, overwrite?", "Existing file", JOptionPane.YES_NO_CANCEL_OPTION);
- switch (result) {
- case JOptionPane.YES_OPTION:
- super.approveSelection();
- return;
- case JOptionPane.NO_OPTION:
- return;
- case JOptionPane.CANCEL_OPTION:
- cancelSelection();
- return;
- }
- }
- super.approveSelection();
- }
- };
- jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
- jFileChooser.addChoosableFileFilter(new GenericFileFilter(new String[]{"pdf"}, "PDF Files"));
- jFileChooser.setMultiSelectionEnabled(false);
- jFileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
- // </editor-fold>
- if (jFileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
- try {
- File fileName = jFileChooser.getSelectedFile();
- saveChartAsPDF(chart, fileName, 1000, 750);
- JOptionPane.showMessageDialog((Component) e.getSource(), "Graph successfully saved");
- } catch (FOPException ex) {
- Logger.getLogger(GraphController.class.getName()).log(Level.SEVERE, null, ex);
- } catch (TransformerException ex) {
- Logger.getLogger(GraphController.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(GraphController.class.getName()).log(Level.SEVERE, null, ex);
- JOptionPane.showMessageDialog((Component) e.getSource(), "An error occured with the file");
- }
- } else {
- System.out.println("Command cancelled by user");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment