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:sim.util.media.PDFEncoder.java
License:Academic Free License
public static void generatePDF(Component component, File file) { int width = component.getWidth(); int height = component.getHeight(); try {// ww w . j a v a2 s. co m Document document = new Document(new com.lowagie.text.Rectangle(width, height)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); document.addAuthor("MASON"); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics g2 = tp.createGraphics(width, height, new DefaultFontMapper()); component.paint(g2); g2.dispose(); cb.addTemplate(tp, 0, 0); document.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:sim.util.media.PDFEncoder.java
License:Academic Free License
public static void generatePDF(JFreeChart chart, int width, int height, File file) { try {//from w ww . j a v a 2 s . c om Document document = new Document(new com.lowagie.text.Rectangle(width, height)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); document.addAuthor("MASON"); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper()); Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2, rectangle2D); g2.dispose(); cb.addTemplate(tp, 0, 0); document.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:userinterface.graph.Graph.java
License:Open Source License
/** * Exports the given chart to a pdf format file * @param file the file that has to be saved in pdf format * @param chart the chart that needs to be exported into pdf *///from w ww. j a v a 2s. c o m public static void exportToPDF(File file, JFreeChart chart) { PdfWriter out = null; com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4.rotate()); int width = 800, height = 500; try { out = PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); PdfContentByte contentByte = out.getDirectContent(); PdfTemplate template = contentByte.createTemplate(width, height); @SuppressWarnings("deprecation") 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) { JOptionPane.showMessageDialog(GUIPrism.getGUI(), e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); return; } document.close(); }
From source file:userinterface.graph.Graph3D.java
License:Open Source License
/** * Exports the 3d graph as a pdf/*w w w . j a v a2 s . co m*/ * * @param file The pdf file to which the data should be written * @param panel The chart panel that has to be exported */ public static void exportToPDF(File file, Chart3DPanel panel) { PdfWriter out = null; Document document = new com.itextpdf.text.Document(PageSize.A4.rotate()); int width = 800, height = 500; try { out = PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); PdfContentByte contentByte = out.getDirectContent(); PdfTemplate template = contentByte.createTemplate(width, height); @SuppressWarnings("deprecation") Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height); panel.getChart().draw(graphics2d, rectangle2d); graphics2d.dispose(); contentByte.addTemplate(template, 0, 0); } catch (Exception e) { // in case any error occurs tell the user what the error is (sometimes useful if there is a problem of writing rights) JOptionPane.showMessageDialog(GUIPrism.getGUI(), e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); return; } document.close(); }
From source file:WeeklyReport.Sections.Bookings.java
public Image bookingsByPODImage(PdfWriter writer) throws BadElementException { JFreeChart chart = new Bookings().bookingsByPODChart(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(600f, 400f); Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f); chart.draw(graphics2d, rectangle2d); graphics2d.dispose();//from w ww .j a v a 2 s.c om Image chartImage = Image.getInstance(template); return chartImage; }
From source file:WeeklyReport.Sections.Bookings.java
public Image bookingsByTradelaneImage(PdfWriter writer) throws BadElementException { JFreeChart chart = new Bookings().bookingsByTradelaneChart(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(600f, 400f); Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f); chart.draw(graphics2d, rectangle2d); graphics2d.dispose();/* ww w . j a v a 2 s . c o m*/ Image chartImage = Image.getInstance(template); return chartImage; }
From source file:WeeklyReport.Sections.Commodities.java
public Image commodityChartImage(PdfWriter writer) throws BadElementException { JFreeChart chart = commodityChart(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(525f, 475f); Graphics2D graphics2d = template.createGraphics(525f, 475f, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Float(0, 0, 525f, 475f); chart.draw(graphics2d, rectangle2d); graphics2d.dispose();//from w w w . j a v a 2 s. c o m Image chartImage = Image.getInstance(template); return chartImage; }
From source file:WeeklyReport.Sections.Declines.java
public Image declinesByReasonImage(PdfWriter writer) throws BadElementException { JFreeChart chart = declinesByReasonChart(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(600f, 400f); Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f); chart.draw(graphics2d, rectangle2d); graphics2d.dispose();/*from w ww . jav a2s. c o m*/ Image chartImage = Image.getInstance(template); return chartImage; }
From source file:WeeklyReport.Sections.Declines.java
public Image declinesByCommodityImage(PdfWriter writer) throws BadElementException { JFreeChart chart = new Declines().declinesByCommodityClassChart(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createAppearance(600f, 400f); Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f); chart.draw(graphics2d, rectangle2d); graphics2d.dispose();//ww w .j a v a2s . co m Image chartImage = Image.getInstance(template); return chartImage; }
From source file:WeeklyReport.WeeklyPDF.java
public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) { PdfWriter writer = null;//from w w w . j a v a2 s.c om Document document = new Document(); try { writer = PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); document.add(new Paragraph("Here is the recommendation")); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(width, height); Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Double(5, -100, width, height); chart.draw(graphics2d, rectangle2d); graphics2d.dispose(); contentByte.addTemplate(template, 0, 0); } catch (FileNotFoundException | DocumentException ex) { ex.getMessage(); } document.close(); }