List of usage examples for com.lowagie.text.pdf PdfTemplate createGraphics
public java.awt.Graphics2D createGraphics(float width, float height)
Graphics2D
to write on. From source file:org.eclipse.birt.report.engine.emitter.pdf.PDFPage.java
License:Open Source License
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); }//from w w w . ja v a 2s .c o m 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; }
From source file:org.locationtech.udig.project.ui.wizard.export.image.Image2Pdf.java
License:Open Source License
/** * writes a buffered image to pdf at a given resolution * * * @param image/* w w w .j ava2 s . com*/ * the image to write * @param pdfPath * the path to the pdf document to create * @param paper * the paper type * @param widthBorder * border in pixels to use on the x-axis * @param heightBorder * border in pixels to use on the y-axis * @param lanscape * true if the document should be in landscape mode * @param dpi the output dpi */ public static void write(BufferedImage image, String pdfPath, Paper paper, int widthBorder, int heightBorder, boolean landscape, int dpi) { Dimension printPageSize = null; printPageSize = new Dimension(paper.getPixelWidth(landscape, dpi), paper.getPixelHeight(landscape, dpi)); // step 1: creation of a document-object Document document = new Document(new Rectangle(printPageSize.width, printPageSize.height)); 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(pdfPath)); // step 3: we open the document document.open(); // step 4: we create a template and a Graphics2D object that // corresponds with it int w = printPageSize.width; int h = printPageSize.height; PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2 = tp.createGraphics(w, h); tp.setWidth(w); tp.setHeight(h); g2.drawImage(image, null, widthBorder, heightBorder); g2.dispose(); cb.addTemplate(tp, 0, 0); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } // step 5: we close the document document.close(); }
From source file:org.openscience.jmol.app.jmolpanel.PdfCreator.java
License:Open Source License
public String createPdfDocument(String fileName, Image image) { Document document = new Document(); File file = null;// w w w. j a va 2s.c om 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; }
From source file:PresentationLayer.CreatePuzzleJFrame.java
private void savePuzzlejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savePuzzlejButtonActionPerformed fileChooser.resetChoosableFileFilters(); fileChooser.setAcceptAllFileFilterUsed(false); fileChooser.setDialogTitle("Save Puzzle"); fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF", "pdf")); // fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("JPEG", "jpeg")); // fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("JPG", "jpg")); // fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PNG", "png")); // fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("BMP", "bmp")); int r = fileChooser.showSaveDialog(this); if (r == JFileChooser.APPROVE_OPTION) { String name = fileChooser.getSelectedFile().getAbsolutePath(); String type = fileChooser.getFileFilter().getDescription(); JPanel jPanelToPrint = printablejPanel; if (type.equals("PDF")) { //Save as pdf puzzlejTable.setBackground(Color.black); try { // Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50); Document document = new Document(new Rectangle(docWidth, docHeight)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(name + ".pdf")); document.open();/*from w w w . j a v a 2 s . c om*/ PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(jPanelToPrint.getWidth(), jPanelToPrint.getHeight()); Graphics2D g2 = tp.createGraphics(jPanelToPrint.getWidth(), jPanelToPrint.getHeight()); //g2.scale(0.8, 1.0); jPanelToPrint.print(g2); g2.dispose(); cb.addTemplate(tp, 0, 0); document.close(); } catch (DocumentException ex) { Logger.getLogger(CreatePuzzleJFrame.class.getName()).log(Level.SEVERE, null, ex); } catch (FileNotFoundException ex) { Logger.getLogger(CreatePuzzleJFrame.class.getName()).log(Level.SEVERE, null, ex); } puzzlejTable.setBackground(Color.white); } else { //Save as image BufferedImage bi = ScreenImage.createImage(jPanelToPrint); try { ScreenImage.writeImage(bi, name + "." + type.toLowerCase()); } catch (IOException ex) { Logger.getLogger(CreatePuzzleJFrame.class.getName()).log(Level.SEVERE, null, ex); } } } }
From source file:vjmol.VJMol.java
License:GNU General Public License
protected void saveImage() { String strPath = m_txfImage.getText(); if (strPath == null || strPath.equals("")) return;//w w w . j av a 2 s .c o m StringTokenizer strTok = new StringTokenizer(strPath); if (strTok.hasMoreTokens()) strPath = strTok.nextToken(); strPath = new StringBuffer().append(m_strImageDir).append(File.separator).append(strPath).toString(); try { Image image = m_vjmolPanel.getImage(); File file = new File(strPath); FileOutputStream os = new FileOutputStream(strPath); String strFormat = (String) m_cmbFormat.getSelectedItem(); if (strFormat.equals("JPG")) { JpegEncoder jc = new JpegEncoder(image, 100, os); jc.Compress(); } else if (strFormat.equals("PPM")) { PpmEncoder pc = new PpmEncoder(image, os); pc.encode(); } else if (strFormat.equals("GIF")) { GifEncoder gc = new GifEncoder(image, os, true); gc.encode(); } else if (strFormat.equals("PNG")) { PngEncoder png = new PngEncoder(image); byte[] pngbytes = png.pngEncode(); os.write(pngbytes); } else if (strFormat.equals("BMP")) { BMPFile bmp = new BMPFile(); bmp.saveBitmap(os, image); } else if (strFormat.equals("PDF")) { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, os); document.open(); int w = m_vjmolPanel.getWidth(); int h = m_vjmolPanel.getHeight(); 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); m_vjmolPanel.print(g2); g2.dispose(); cb.addTemplate(tp, 72, 720 - h); document.close(); } os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } }