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:de.bfs.radon.omsimulation.gui.data.OMExports.java
License:Open Source License
/** * /*from ww w .j a v a 2 s .c om*/ * Method to export charts as PDF files using the defined path. * * @param path * The filename and absolute path. * @param chart * The JFreeChart object. * @param width * The width of the PDF file. * @param height * The height of the PDF file. * @param mapper * The font mapper for the PDF file. * @param title * The title of the PDF file. * @throws IOException * If writing a PDF file fails. */ @SuppressWarnings("deprecation") public static void exportPdf(String path, JFreeChart chart, int width, int height, FontMapper mapper, String title) throws IOException { File file = new File(path); FileOutputStream pdfStream = new FileOutputStream(file); BufferedOutputStream pdfOutput = new BufferedOutputStream(pdfStream); Rectangle pagesize = new Rectangle(width, height); Document document = new Document(); document.setPageSize(pagesize); document.setMargins(50, 50, 50, 50); document.addAuthor("OMSimulationTool"); document.addSubject(title); try { PdfWriter pdfWriter = PdfWriter.getInstance(document, pdfOutput); document.open(); PdfContentByte contentByte = pdfWriter.getDirectContent(); PdfTemplate template = contentByte.createTemplate(width, height); Graphics2D g2D = template.createGraphics(width, height, mapper); Double r2D = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2D, r2D); g2D.dispose(); contentByte.addTemplate(template, 0, 0); } catch (DocumentException de) { JOptionPane.showMessageDialog(null, "Failed to write PDF document.\n" + de.getMessage(), "Failed", JOptionPane.ERROR_MESSAGE); de.printStackTrace(); } document.close(); }
From source file:edu.fullerton.viewerplugin.PluginSupport.java
License:Open Source License
public void saveImageAsPdfFile(JFreeChart chart, String filename) throws WebUtilException { try {//from w w w .j ava 2 s. c om OutputStream out = new BufferedOutputStream(new FileOutputStream(filename)); Rectangle pagesize = new Rectangle(width, height); com.itextpdf.text.Document document; document = new com.itextpdf.text.Document(pagesize, 50, 50, 50, 50); try { PdfWriter writer = PdfWriter.getInstance(document, out); FontMapper mapper = new DefaultFontMapper(); document.addAuthor("JFreeChart"); document.addSubject("Demonstration"); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2 = tp.createGraphics(width, height, mapper); Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2, r2D); g2.dispose(); cb.addTemplate(tp, 0, 0); } catch (DocumentException de) { throw new WebUtilException("Saving as pdf", de); } document.close(); } catch (FileNotFoundException ex) { throw new WebUtilException("Saving plot as pdf: ", ex); } }
From source file:edu.gcsc.vrl.jfreechart.JFExport.java
License:Open Source License
/** * Export jfreechart to image format. File must have an extension like jpg, * png, pdf, svg, eps. Otherwise export will fail. * * @param file Destination Filedescriptor * @param chart JFreechart//from w w w. j a v a 2 s . c o m * @throws Exception */ public boolean export(File file, JFreeChart chart) throws Exception { /*Get extension from file - File must have one extension of jpg,png,pdf,svg,eps. Otherwise the export will fail. */ String ext = JFUtils.getExtension(file); // TODO - Make x,y variable int x, y; // Set size for image (jpg) x = 550; y = 470; int found = 0; // JPEG if (ext.equalsIgnoreCase("jpg")) { ChartUtilities.saveChartAsJPEG(file, chart, x, y); found++; } // PNG if (ext.equalsIgnoreCase("png")) { ChartUtilities.saveChartAsPNG(file, chart, x, y); found++; } // PDF if (ext.equalsIgnoreCase("pdf")) { //JRAbstractRenderer jfcRenderer = new JFreeChartRenderer(chart); // Use here size of r2d2 (see below, Rectangel replaced by Rectangle2D !) Rectangle pagesize = new Rectangle(x, y); Document document = new Document(pagesize, 50, 50, 50, 50); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(x, y); Graphics2D g2 = tp.createGraphics(x, y, new DefaultFontMapper()); // Draw doesn't works with Rectangle argument - use rectangle2D instead ! //chart.draw(g2, (java.awt.geom.Rectangle2D) new Rectangle(x,y)); Rectangle2D r2d2 = new Rectangle2D.Double(0, 0, x, y); chart.draw(g2, r2d2); g2.dispose(); cb.addTemplate(tp, 0, 0); document.close(); found++; } // SVG if (ext.equalsIgnoreCase("svg")) { // When exporting to SVG don't forget this VERY important line: // svgGenerator.setSVGCanvasSize(new Dimension(width, height)); // Otherwise renderers will not know the size of the image. It will be drawn to the correct rectangle, but the image will not have a correct default siz // Get a DOMImplementation DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation(); org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null); SVGGraphics2D svgGenerator = new SVGGraphics2D(document); //chart.draw(svgGenerator,new Rectangle(x,y)); chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, x, y)); boolean useCSS = true; // we want to use CSS style attribute Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); svgGenerator.stream(out, useCSS); out.close(); found++; } if (ext.equalsIgnoreCase("eps")) { //Graphics2D g = new EpsGraphics2D(); FileOutputStream out = new FileOutputStream(file); //Writer out=new FileWriter(file); Graphics2D g = new EpsGraphics(file.getName(), out, 0, 0, x, y, ColorMode.COLOR_RGB); chart.draw(g, new Rectangle2D.Double(0, 0, x, y)); //Writer out=new FileWriter(file); out.write(g.toString().getBytes()); out.close(); found++; } if (found == 0) { throw new IllegalArgumentException("File format '" + ext + "' not supported!"); } return true; }
From source file:edu.umn.genomics.component.SavePDF.java
License:Open Source License
/** * Saves the Component to a Portable Document File, PDF, with the * file location selected using the JFileChooser. * @param c the Component to save as a PDF *//*from w w w . ja v a 2s . c om*/ public static boolean savePDF(Component c) throws IOException { System.out.println(""); final int w = c.getWidth() > 0 ? c.getWidth() : 1; final int h = c.getHeight() > 0 ? c.getHeight() : 1; final Dimension dim = c.getPreferredSize(); JFileChooser chooser = new JFileChooser(); JPanel ap = new JPanel(); ap.setLayout(new BoxLayout(ap, BoxLayout.Y_AXIS)); JPanel sp = new JPanel(new GridLayout(0, 1)); sp.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Image Size")); final JTextField iwtf = new JTextField("" + w, 4); iwtf.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "width")); final JTextField ihtf = new JTextField("" + h, 4); ihtf.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "height")); JButton curSzBtn = new JButton("As Viewed: " + w + "x" + h); curSzBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { iwtf.setText("" + w); ihtf.setText("" + h); } }); sp.add(curSzBtn); if (dim != null && dim.getWidth() > 0 && dim.getHeight() > 0) { JButton prefSzBtn = new JButton("As Preferred: " + dim.width + "x" + dim.height); prefSzBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { iwtf.setText("" + dim.width); ihtf.setText("" + dim.height); } }); sp.add(prefSzBtn); } sp.add(iwtf); sp.add(ihtf); ap.add(sp); chooser.setAccessory(ap); // ImageFilter filter = new ImageFilter(fmt); // chooser.setFileFilter(filter); int returnVal = chooser.showSaveDialog(c); boolean status = false; if (returnVal == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); //Added the below code to add th .pdf extension if it is not provided by the user String name = file.getAbsolutePath(); if (!(name.substring(name.length() - 4, name.length()).equalsIgnoreCase(".pdf"))) { name = name.concat(".pdf"); file = new File(name); } int iw = w; int ih = h; try { iw = Integer.parseInt(iwtf.getText()); ih = Integer.parseInt(ihtf.getText()); } catch (Exception ex) { ExceptionHandler.popupException("" + ex); } iw = iw > 0 ? iw : w; ih = ih > 0 ? ih : h; if (iw != w || ih != h) { c.setSize(iw, ih); } // step 1: creation of a document-object Document document = new Document(); try { // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); // step 3: we open the document document.open(); // step 4: we grab the ContentByte and do some stuff with it // we create a fontMapper and read all the fonts in the font directory DefaultFontMapper mapper = new DefaultFontMapper(); // mapper.insertDirectory("c:\\winnt\\fonts"); com.itextpdf.text.Rectangle pgSize = document.getPageSize(); // we create a template and a Graphics2D object that corresponds with it PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(iw, ih); tp.setWidth(iw); tp.setHeight(ih); Graphics2D g2 = tp.createGraphics(iw, ih, mapper); g2.setStroke(new BasicStroke(.1f)); //cb.setLineWidth(.1f); //cb.stroke(); c.paintAll(g2); g2.dispose(); //cb.addTemplate(tp, 0, 0); float sfx = (float) (pgSize.getWidth() / iw); float sfy = (float) (pgSize.getHeight() / ih); // preserve the aspect ratio float sf = (float) Math.min(sfx, sfy); cb.addTemplate(tp, sf, 0f, 0f, sf, 0f, 0f); } catch (DocumentException de) { ExceptionHandler.popupException("" + de); } catch (IOException ioe) { ExceptionHandler.popupException("" + ioe); } catch (Exception ex) { ExceptionHandler.popupException("" + ex); } // step 5: we close the document document.close(); if (iw != w || ih != h) { c.setSize(w, h); } } return true; }
From source file:figtree.application.FigTreeFrame.java
License:Open Source License
public final void doExportPDF() { FileDialog dialog = new FileDialog(this, "Export PDF Image...", FileDialog.SAVE); dialog.setVisible(true);// w w w . j a v a2s. c om if (dialog.getFile() != null) { File file = new File(dialog.getDirectory(), dialog.getFile()); Rectangle2D bounds = treeViewer.getContentPane().getBounds(); Document document = new Document( new com.itextpdf.text.Rectangle((float) bounds.getWidth(), (float) bounds.getHeight())); try { // step 2 PdfWriter writer; writer = PdfWriter.getInstance(document, new FileOutputStream(file)); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate((float) bounds.getWidth(), (float) bounds.getHeight()); Graphics2D g2d = tp.createGraphics((float) bounds.getWidth(), (float) bounds.getHeight(), new DefaultFontMapper()); treeViewer.getContentPane().print(g2d); g2d.dispose(); cb.addTemplate(tp, 0, 0); } catch (DocumentException de) { JOptionPane.showMessageDialog(this, "Error writing PDF file: " + de, "Export PDF Error", JOptionPane.ERROR_MESSAGE); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(this, "Error writing PDF file: " + e, "Export PDF Error", JOptionPane.ERROR_MESSAGE); } document.close(); } }
From source file:info.sarihh.unimodeling.gui.DynamicBPEstimateFrame.java
public static void writeChartAsPDF(OutputStream out, JFreeChart chart, int width, int height, FontMapper mapper) throws IOException { Rectangle pagesize = new Rectangle(width, height); Document document = new Document(pagesize, 50, 50, 50, 50); try {//from w w w . j av a 2 s. c o m PdfWriter writer = PdfWriter.getInstance(document, out); document.addAuthor("JFreeChart"); document.addSubject("Demonstration"); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2 = tp.createGraphics(width, height, mapper); Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2, r2D, null); g2.dispose(); cb.addTemplate(tp, 0, 0); } catch (DocumentException de) { System.err.println(de.getMessage()); } document.close(); }
From source file:net.pickapack.chart.ChartPdfExporter.java
License:Open Source License
/** * Export the specified chart to the specified PDF file. * * @param chart the chart// w w w. ja va 2 s . c o m * @param width the width of the PDF file * @param height the height of the PDF file * @param fileName the PDF file name */ public static void exportPdf(JFreeChart chart, int width, int height, String fileName) { try { Document document = new Document(new Rectangle(width, height)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper()); Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2d, r2d); g2d.dispose(); cb.addTemplate(tp, 0, 0); document.close(); } catch (DocumentException e) { throw new RuntimeException(e); } catch (FileNotFoundException e) { throw new RuntimeException(e); } }
From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java
License:Open Source License
/** * This method saves a chart as a PDF with given dimensions * /*from w ww. j a va 2 s. c o m*/ * @param chart * @param width * @param height * @param fileName is a full path */ public static void writeChartToPDF(JFreeChart chart, int width, int height, File fileName) throws Exception { PdfWriter writer = null; Document document = new Document(new Rectangle(width, height)); try { writer = PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); 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, 0, 0); } catch (Exception e) { e.printStackTrace(); throw e; } finally { document.close(); } }
From source file:org.fhaes.fhsamplesize.view.SSIZCurveChart.java
License:Open Source License
/** * Save chart as PDF file. Requires iText library. * // w ww . ja va 2s . com * @param chart JFreeChart to save. * @param fileName Name of file to save chart in. * @param width Width of chart graphic. * @param height Height of chart graphic. * @throws Exception if failed. * @see <a href="http://www.lowagie.com/iText">iText</a> */ @SuppressWarnings("deprecation") public static void writeAsPDF(File fileToSave, int width, int height) throws Exception { if (chart != null) { BufferedOutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(fileToSave.getAbsolutePath())); // convert chart to PDF with iText: Rectangle pagesize = new Rectangle(width, height); Document document = new Document(pagesize, 50, 50, 50, 50); try { PdfWriter writer = PdfWriter.getInstance(document, out); document.addAuthor("JFreeChart"); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper()); Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2, r2D, null); g2.dispose(); cb.addTemplate(tp, 0, 0); } finally { document.close(); } } finally { if (out != null) out.close(); } } }
From source file:org.openscience.cdk.applications.taverna.basicutilities.ChartTool.java
License:Open Source License
/** * Adds a chart to the pdf file.//from w w w. j ava 2 s . c om * * @param chart * @param cb */ private void addChartPageToPDF(JFreeChart chart, PdfContentByte cb) { PdfTemplate tp = cb.createTemplate(this.width, this.height); Graphics2D g2 = tp.createGraphics(this.width, this.height, new DefaultFontMapper()); Rectangle2D r2D = new Rectangle2D.Double(0, 0, this.width, this.height); chart.draw(g2, r2D); g2.dispose(); cb.addTemplate(tp, 0, 0); }