private void exportVectorGraphics(String formatName, File outputFile) throws ImageExportException { Component component = printableComponent.getExportComponent(); int width = component.getWidth(); int height = component.getHeight(); try (FileOutputStream fs = new FileOutputStream(outputFile)) { switch (formatName) { case PDF: // create pdf document with slightly increased width and height // (otherwise the image gets cut off) Document document = new Document(new Rectangle(width + 5, height + 5)); PdfWriter writer = PdfWriter.getInstance(document, fs); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper()); component.print(g2); g2.dispose(); cb.addTemplate(tp, 0, 0); document.close(); break; case SVG: exportFreeHep(component, fs, new SVGGraphics2D(fs, new Dimension(width, height))); break; case EPS: exportFreeHep(component, fs, new PSGraphics2D(fs, new Dimension(width, height))); break; default: // cannot happen break; } } catch (Exception e) { throw new ImageExportException(I18N.getMessage(I18N.getUserErrorMessagesBundle(), "error.image_export.export_failed"), e); } }
/** Creates an Image from a PdfTemplate. * * @param template the PdfTemplate * @throws BadElementException on error */ public ImgTemplate(PdfTemplate template) throws BadElementException{ super((URL)null); if (template == null) throw new BadElementException("The template can not be null."); if (template.getType() == PdfTemplate.TYPE_PATTERN) throw new BadElementException("A pattern can not be used as a template to create an image."); type = IMGTEMPLATE; scaledHeight = template.getHeight(); setTop(scaledHeight); scaledWidth = template.getWidth(); setRight(scaledWidth); setTemplateData(template); plainWidth = getWidth(); plainHeight = getHeight(); }
/** Reads the WMF into a template. * @param template the template to read to * @throws IOException on error * @throws DocumentException on error */ public void readWMF(PdfTemplate template) throws IOException, DocumentException { setTemplateData(template); template.setWidth(getWidth()); template.setHeight(getHeight()); InputStream is = null; try { if (rawData == null){ is = url.openStream(); } else{ is = new java.io.ByteArrayInputStream(rawData); } MetaDo meta = new MetaDo(is, template); meta.readAll(); } finally { if (is != null) { is.close(); } } }
/** * Gets an instance of a Image from a java.awt.Image. * The image is added as a JPEG with a user defined quality. * * @param cb * the <CODE>PdfContentByte</CODE> object to which the image will be added * @param awtImage * the <CODE>java.awt.Image</CODE> to convert * @param quality * a float value between 0 and 1 * @return an object of type <CODE>PdfTemplate</CODE> * @throws BadElementException * on error * @throws IOException */ public static Image getInstance(PdfContentByte cb, java.awt.Image awtImage, float quality) throws BadElementException, IOException { java.awt.image.PixelGrabber pg = new java.awt.image.PixelGrabber(awtImage, 0, 0, -1, -1, true); try { pg.grabPixels(); } catch (InterruptedException e) { throw new IOException( "java.awt.Image Interrupted waiting for pixels!"); } if ((pg.getStatus() & java.awt.image.ImageObserver.ABORT) != 0) { throw new IOException("java.awt.Image fetch aborted or errored"); } int w = pg.getWidth(); int h = pg.getHeight(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2d = tp.createGraphics(w, h, true, quality); g2d.drawImage(awtImage, 0, 0, null); g2d.dispose(); return getInstance(tp); }
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 { PdfWriter writer = PdfWriter.getInstance(document, out); document.addAuthor("Ross Anderson"); document.addSubject("Kidney Exchange"); 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) { System.err.println(de.getMessage()); } document.close(); }
@Override public void drawCropped(IImage image, float xoffset, float yoffset, float x, float y, float width, float height) throws DocumentException { try { Image pdfImage = getImage(image); PdfTemplate t = delegate.getPdfWriter().getDirectContent().createTemplate(width, height); t.addImage(pdfImage, pdfImage.getWidth(), 0, 0, pdfImage.getHeight(), xoffset, -yoffset+height-pdfImage.getHeight()); pdfImage = Image.getInstance(t); pdfImage.setAbsolutePosition(x, y); delegate.addImage(pdfImage); } catch (Exception e) { throw new DocumentException(e); } }
public void setTotalPage( ITextArea totalPage ) { super.setTotalPage( totalPage ); isTotalPage = true; HashMap<Float, PdfTemplate> map = ( (PDFPageDevice) pageDevice ) .getTemplateMap( ); if ( !map.isEmpty( ) ) { float scaleCache = this.scale; for ( Entry<Float, PdfTemplate> e : map.entrySet( ) ) { Float s = e.getKey( ); PdfTemplate template = e.getValue( ); if ( template != null ) { this.scale = s.floatValue( ); drawText( totalPage ); } } this.scale = scaleCache; } isTotalPage = false; }
private void createTotalPageTemplate( float x, float y, float width, float height, float scale ) { PdfTemplate template = null; if ( pageDevice.hasTemplate( scale ) ) { template = pageDevice.getPDFTemplate( scale ); } else { template = contentByte.createTemplate( width, height ); pageDevice.setPDFTemplate( scale, template ); } y = transformY( y, height ); contentByte.saveState( ); contentByte.addTemplate( template, x, y ); contentByte.restoreState( ); }
protected void drawImage( PdfTemplate image, float imageX, float imageY, float height, float width, String helpText ) throws DocumentException { imageY = transformY( imageY, height ); contentByte.saveState( ); contentByte.concatCTM( 1, 0, 0, 1, imageX, imageY ); float w = image.getWidth( ); float h = image.getHeight( ); contentByte.addTemplate( image, width / w, 0f / w, 0f / h, height / h, 0f, 0f ); if ( helpText != null ) { showHelpText( imageX, imageY, width, height, helpText ); } contentByte.restoreState( ); }
protected PdfTemplate transSVG( String svgPath, byte[] svgData, float x, float y, float height, float width, String helpText ) throws IOException, DocumentException { PdfTemplate template = contentByte.createTemplate( width, height ); Graphics2D g2D = template.createGraphics( width, height ); PrintTranscoder transcoder = new PrintTranscoder( ); if ( null != svgData && svgData.length > 0 ) { transcoder.transcode( new TranscoderInput( new ByteArrayInputStream( svgData ) ), null ); } else if ( null != svgPath ) { transcoder.transcode( new TranscoderInput( svgPath ), null ); } PageFormat pg = new PageFormat( ); Paper p = new Paper( ); p.setSize( width, height ); p.setImageableArea( 0, 0, width, height ); pg.setPaper( p ); transcoder.print( g2D, pg, 0 ); g2D.dispose( ); return template; }
/** Creats an Image from a PdfTemplate. * * @param template the PdfTemplate * @throws BadElementException on error */ public ImgTemplate(PdfTemplate template) throws BadElementException{ super((URL)null); if (template == null) throw new BadElementException("The template can not be null."); if (template.getType() == PdfTemplate.TYPE_PATTERN) throw new BadElementException("A pattern can not be used as a template to create an image."); type = IMGTEMPLATE; scaledHeight = template.getHeight(); setTop(scaledHeight); scaledWidth = template.getWidth(); setRight(scaledWidth); setTemplateData(template); plainWidth = width(); plainHeight = height(); }
public void addTextVertical(PdfPCell cell, String text, boolean bold) throws Exception { if (text==null) return; if (text.indexOf("<span")>=0) text = text.replaceAll("</span>","").replaceAll("<span .*>", ""); Font font = PdfFont.getFont(bold); BaseFont bf = font.getBaseFont(); float width = bf.getWidthPoint(text, font.getSize()); PdfTemplate template = iWriter.getDirectContent().createTemplate(2 * font.getSize() + 4, width); template.beginText(); template.setColorFill(Color.BLACK); template.setFontAndSize(bf, font.getSize()); template.setTextMatrix(0, 2); template.showText(text); template.endText(); template.setWidth(width); template.setHeight(font.getSize() + 2); //make an Image object from the template Image img = Image.getInstance(template); img.setRotationDegrees(270); //embed the image in a Chunk Chunk ck = new Chunk(img, 0, 0); if (cell.getPhrase()==null) { cell.setPhrase(new Paragraph(ck)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); } else { cell.getPhrase().add(ck); } }
/** * Converts a JFreeChart to PDF syntax. * @param filename the name of the PDF file * @param chart the JFreeChart * @param width the width of the resulting PDF * @param height the height of the resulting PDF */ public static void convertToPdf(JFreeChart chart, int width, int height, String filename) { // step 1 Document document = new Document(new Rectangle(width, height)); try { // step 2 PdfWriter writer; writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream(filename)); // step 3 document.open(); // step 4 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(); tp.sanityCheck(); cb.addTemplate(tp, 0, 0); cb.sanityCheck(); } catch(DocumentException de) { de.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } // step 5 document.close(); }
public static void writeToPortableDocumentFormat(Drawing drawing, OutputStream outputStream, float drawingWidth, float drawingHeight, float pageWidth, float pageHeight) throws DocumentException { com.lowagie.text.Document document = new com.lowagie.text.Document(); document.setPageSize(new com.lowagie.text.Rectangle(pageWidth, pageHeight)); PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(drawingWidth, drawingHeight); DefaultFontMapper mapper = PdfFriend.getFontMapper(); final DefaultDrawingContext drawingContext = (DefaultDrawingContext) DrawingContextFactory.getDrawingContext(); Graphics2D g2 = null; if (BalloonEngineState.getInstance().isPreserveAccuracy()) g2 = tp.createGraphicsShapes(drawingWidth, drawingHeight); else g2 = tp.createGraphics(drawingWidth, drawingHeight, mapper); drawingContext.setGraphics(g2); drawingContext.setTargetingPdf(true); drawingContext.setSelected(new Selection()); drawingContext.setExportProfile(new SimpleExportProfile()); drawing.drawOnto(drawingContext); Dimension dimension = new Dimension((int)drawingWidth, (int)drawingHeight); g2.dispose(); cb.addTemplate(tp, 0, 0); document.close(); }
public String createPdfDocument(String fileName, Image image) { Document document = new Document(); File file = null; try { int w = image.getWidth(null); int h = image.getHeight(null); file = new File(fileName); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2 = tp.createGraphics(w, h); g2.setStroke(new BasicStroke(0.1f)); tp.setWidth(w); tp.setHeight(h); g2.drawImage(image, 0, 0, w, h, 0, 0, w, h, null); g2.dispose(); cb.addTemplate(tp, 72, 720 - h); } catch (DocumentException de) { return de.getMessage(); } catch (IOException ioe) { return ioe.getMessage(); } document.close(); return null; }
public final void doExportPDF() { FileDialog dialog = new FileDialog(this, "Export PDF Image...", FileDialog.SAVE); dialog.setVisible(true); if (dialog.getFile() != null) { File file = new File(dialog.getDirectory(), dialog.getFile()); Rectangle2D bounds = mapperPanel.getExportableComponent().getBounds(); Document document = new Document(new com.lowagie.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()); mapperPanel.getExportableComponent().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(); } }
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); }