List of usage examples for com.itextpdf.awt PdfGraphics2D PdfGraphics2D
public PdfGraphics2D(PdfContentByte cb, final float width, final float height, final boolean onlyShapes)
From source file:net.panthema.BispanningGame.GamePanel.java
License:Open Source License
public void writePdf() throws FileNotFoundException, DocumentException { // Query user for filename JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle("Specify PDF file to save"); chooser.setCurrentDirectory(new File(".")); FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF Documents", "pdf"); chooser.setFileFilter(filter);/*from w w w .jav a 2s . c o m*/ if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) return; File outfile = chooser.getSelectedFile(); if (!outfile.getAbsolutePath().endsWith(".pdf")) { outfile = new File(outfile.getAbsolutePath() + ".pdf"); } // Calculate page size rectangle Dimension size = mVV.getSize(); Rectangle rsize = new Rectangle(size.width, size.height); // Open the PDF file for writing - and create a Graphics2D object Document document = new Document(rsize); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outfile)); document.open(); PdfContentByte contentByte = writer.getDirectContent(); PdfGraphics2D graphics2d = new PdfGraphics2D(contentByte, size.width, size.height, new DefaultFontMapper()); // Create a container to hold the visualization Container container = new Container(); container.addNotify(); container.add(mVV); container.setVisible(true); container.paintComponents(graphics2d); // Dispose of the graphics and close the document graphics2d.dispose(); document.close(); // Put mVV back onto visible plane setLayout(new BorderLayout()); add(mVV, BorderLayout.CENTER); }
From source file:org.ujmp.itext.ExportPDF.java
License:Open Source License
public static final void save(File file, Component c, int width, int height) { if (file == null) { logger.log(Level.WARNING, "no file selected"); return;// w w w. j a v a 2s . c o m } if (c == null) { logger.log(Level.WARNING, "no component provided"); return; } try { Document document = new Document(new Rectangle(width, height)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file.getAbsolutePath())); document.addAuthor("UJMP v" + UJMP.UJMPVERSION); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2 = new PdfGraphics2D(cb, width, height, new DefaultFontMapper()); if (c instanceof CanRenderGraph) { ((CanRenderGraph) c).renderGraph(g2); } else { c.paint(g2); } g2.dispose(); cb.addTemplate(tp, 0, 0); document.close(); writer.close(); } catch (Exception e) { logger.log(Level.WARNING, "could not save PDF file", e); } }