Package com.lowagie.geoforge.utils

Source Code of com.lowagie.geoforge.utils.LwgUtilJFreeChartToPdf

/*
* $Id$
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* itext-questions@lists.sourceforge.net
*/
package com.lowagie.geoforge.utils;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.FileNotFoundException;

import org.jfree.chart.JFreeChart;

import com.lowagie.text.LwgDocument;
import com.lowagie.text.DocumentException;
import com.lowagie.text.LwgRectangle;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;

/**
* JFreeChart example.
*/
public class LwgUtilJFreeChartToPdf
{

   /**
    * Converts a JFreeChart to PDF syntax.
    * @param strPathAbsoluteFile  the path 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 strPathAbsoluteFile)
   {
      // step 1
      LwgDocument document = new LwgDocument(new LwgRectangle(width, height));
      try
      {
         // step 2
         PdfWriter writer;
         writer = PdfWriter.getInstance(document, new FileOutputStream(strPathAbsoluteFile));
         // 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();
   }
}
TOP

Related Classes of com.lowagie.geoforge.utils.LwgUtilJFreeChartToPdf

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.