Package polyofdm.chart

Source Code of polyofdm.chart.PdfGenerator

package polyofdm.chart;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.jfree.chart.JFreeChart;

import Jama.Matrix;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.DefaultFontMapper;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfGenerator {
 
  public static void generatePDF(String filename, Matrix matrix, String titlePart1, String titlePart2) throws IOException {
    List<JFreeChart> charts = getCharts(matrix, titlePart1, titlePart2);
   
    createPDF(filename, titlePart1, charts);
  }
 
  public static void createCombinedPDF(String filename,
      String title1Part1,
      String title1Part2,
      String title2Part1,
      String title2Part2,
      String titleCombined,
      Matrix matrix1, Matrix matrix2) throws IOException {

    List<JFreeChart> charts1 = getCharts(matrix1, title1Part1, title1Part2);
    List<JFreeChart> charts2 = getCharts(matrix2, title2Part1, title2Part2);
   
    createCombinedPDF(filename, titleCombined, charts1, charts2);
  }
 

  public static List<JFreeChart> getCharts(Matrix matrix, String titlePart1, String titlePart2) {
    List<JFreeChart> charts = new ArrayList<JFreeChart>();

    charts.add(ChartGenerator.createChartFourier(matrix, titlePart1, titlePart2));
    charts.add(ChartGenerator.createChartReal(matrix,true, titlePart1, titlePart2));
    charts.add(ChartGenerator.createChartReal(matrix,false, titlePart1, titlePart2));
    charts.add(ChartGenerator.createChartMaxAmp(matrix, titlePart1, titlePart2));
    charts.add(ChartGenerator.createChartSpektrum(matrix, true, false, titlePart1, titlePart2));
    charts.add(ChartGenerator.createChartSpektrum(matrix, true, true, titlePart1, titlePart2));
    charts.add(ChartGenerator.createChartSpektrum(matrix, false, false, titlePart1, titlePart2));
    charts.add(ChartGenerator.createChartSpektrum(matrix, false, true, titlePart1, titlePart2));
   
    return charts;
  }

  private static void createPDF(String filename, String title,
      List<JFreeChart> charts) throws IOException {
    Rectangle rect = PageSize.A4.rotate();
    Document document = new Document(rect);
    OutputStream out = new FileOutputStream(filename);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, out);
      document.addAuthor("Daniel Velten");
      document.addTitle(title);
      document.open();
     
      for (JFreeChart chart : charts) {
        addChartToPDF(chart, rect, writer);       
        document.newPage();
      }
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    }
    document.close();
    out.close();
  }

  private static void createCombinedPDF(String filename, String title,
      List<JFreeChart> charts1, List<JFreeChart> charts2) throws IOException {
    Rectangle rect = PageSize.A4.rotate();
    Document document = new Document(rect);
    OutputStream out = new FileOutputStream(filename);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, out);
      document.addAuthor("Daniel Velten");
      document.addTitle(title);
      document.open();
     
      for (int i=0; i<2;i++) {
        addChartToPDF2(charts1.get(i), writer, 1-i, 0);       
        addChartToPDF2(charts2.get(i), writer, 1-i, 1);       
      }
      document.newPage();
      for (int i=0; i<2;i++) {
        addChartToPDF2(charts1.get(i+2), writer, 1-i, 0);       
        addChartToPDF2(charts2.get(i+2), writer, 1-i, 1);       
      }
      document.newPage();
      for (int i=0; i<2;i++) {
        addChartToPDF2(charts1.get(i+4), writer, 1-i, 0);       
        addChartToPDF2(charts2.get(i+4), writer, 1-i, 1);       
      }
      document.newPage();
      for (int i=0; i<2;i++) {
        addChartToPDF2(charts1.get(i+6), writer, 1-i, 0);       
        addChartToPDF2(charts2.get(i+6), writer, 1-i, 1);       
      }
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    }
    document.close();
    out.close();
  }

  public static void addChartToPDF(JFreeChart chart, Rectangle rect,
      PdfWriter writer) {
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(rect.getWidth(), rect.getHeight());
    Graphics2D g2 = tp.createGraphics(rect.getWidth(), rect.getHeight(), new DefaultFontMapper());
    Rectangle2D r2D = new Rectangle2D.Double(0, 0, rect.getWidth(), rect.getHeight());
    chart.draw(g2, r2D, null);
    g2.dispose();
    cb.addTemplate(tp, 0, 0);
  }
 
  public static void addChartToPDF2(JFreeChart chart,
      PdfWriter writer, int row, int column) {
    Rectangle rect = PageSize.A6.rotate();
    Rectangle rect2 = PageSize.A4.rotate();
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(rect.getWidth(), rect.getHeight());
    Graphics2D g2 = tp.createGraphics(rect.getWidth(), rect.getHeight(), new DefaultFontMapper());
    Rectangle2D r2D = new Rectangle2D.Double(0, 0, rect2.getWidth(), rect2.getHeight());
    g2.setTransform(AffineTransform.getScaleInstance(rect.getWidth()/rect2.getWidth(), rect.getHeight()/rect2.getHeight()));
    chart.draw(g2, r2D, null);
    g2.dispose();
    cb.addTemplate(tp, column * rect.getWidth(), row * rect.getHeight());
  }


TOP

Related Classes of polyofdm.chart.PdfGenerator

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.