public void toPDFGraphicFile(File file, int width, int height) throws IOException { // otherwise toolbar appears plotToolBar.setVisible(false); com.lowagie.text.Document document = new com.lowagie.text.Document(); document.setPageSize(new Rectangle(width, height)); FileOutputStream fos = new FileOutputStream(file); PdfWriter writer = null; try { writer = PdfWriter.getInstance(document, fos); } catch (DocumentException ex) { Logger.getLogger(PlotPanel.class.getName()).log(Level.SEVERE, null, ex); } document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2d = tp.createGraphics(width, height); Image image = createImage(getWidth(), getHeight()); paint(image.getGraphics()); image = new ImageIcon(image).getImage(); g2d.drawImage(image, 0, 0, Color.WHITE, null); g2d.dispose(); cb.addTemplate(tp, 1, 0, 0, 1, 0, 0); document.close(); // make it reappear plotToolBar.setVisible(true); }
@Override public void createITextObject(FacesContext context) { if (getBorderBackgroundPaint() != null) { chart.setBackgroundPaint(findColor(getBorderBackgroundPaint())); } if (getBorderPaint() != null) { chart.setBorderPaint(findColor(getBorderPaint())); } if (getBorderStroke() != null) { chart.setBorderStroke(findStroke(getBorderStroke())); } chart.setBorderVisible(getBorderVisible()); configurePlot(chart.getPlot()); try { UIDocument doc = (UIDocument) findITextParent(getParent(), UIDocument.class); if (doc != null) { PdfWriter writer = (PdfWriter) doc.getWriter(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(getWidth(), getHeight()); UIFont font = (UIFont) findITextParent(this, UIFont.class); DefaultFontMapper fontMapper; if (font == null) { fontMapper = new DefaultFontMapper(); } else { fontMapper = new AsianFontMapper(font.getName(), font.getEncoding()); } Graphics2D g2 = tp.createGraphics(getWidth(), getHeight(), fontMapper); chart.draw(g2, new Rectangle2D.Double(0, 0, getWidth(), getHeight())); g2.dispose(); image = new ImgTemplate(tp); } else { ByteArrayOutputStream stream = new ByteArrayOutputStream(); ChartUtilities.writeChartAsJPEG(stream, chart, getWidth(), getHeight()); imageData = stream.toByteArray(); stream.close(); } } catch (Exception e) { throw new RuntimeException(e); } }
/** * This creates a file to capture the pdf output generated by calls to graphics2D (e.g., use * makeMap). This will overwrite an existing file. * * @param pageSize e.g, PageSize.LETTER or PageSize.LETTER.rotate() (or A4, or, ...) * @param width the bounding box width, in 1/144ths of an inch * @param height the bounding box height, in 1/144ths of an inch * @param outputStream * @return an object[] with 0=g2D, 1=document, 2=pdfContentByte, 3=pdfTemplate * @throws Exception if trouble */ public static Object[] createPdf( com.lowagie.text.Rectangle pageSize, int bbWidth, int bbHeight, OutputStream outputStream) throws Exception { // currently, this uses itext // see the sample program: // // file://localhost/C:/programs/iText/examples/com/lowagie/examples/directcontent/graphics2D/G2D.java // Document.compress = false; //for test purposes only Document document = new Document(pageSize); document.addCreationDate(); document.addCreator("gov.noaa.pfel.coastwatch.SgtUtil.createPdf"); document.setPageSize(pageSize); PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); // create contentByte and template and Graphics2D objects PdfContentByte pdfContentByte = writer.getDirectContent(); PdfTemplate pdfTemplate = pdfContentByte.createTemplate(bbWidth, bbHeight); Graphics2D g2D = pdfTemplate.createGraphics(bbWidth, bbHeight); return new Object[] {g2D, document, pdfContentByte, pdfTemplate}; }