2

我正在使用 JFreeChart 1.0.9 创建图表,并使用 iText 1.3.1 在 PDF 文件中显示此图表。为此,我正在创建一个 JFreeChart,然后将其转换为字节数组:

final TimeSeriesCollection dataset = new TimeSeriesCollection();
final TimeSeries s1 = new TimeSeries("Series 1", Minute.class);
s1.add(new Minute(0, 0, 7, 12, 2003), 1.2);
s1.add(new Minute(30, 12, 7, 12, 2003), 3.0);   // 12h30  => value = 3
s1.add(new Minute(15, 14, 7, 12, 2003), 8.0);

final TimeSeries s2 = new TimeSeries("Series 2", Minute.class);
s2.add(new Minute(0, 3, 7, 12, 2003), 0.0);
s2.add(new Minute(30, 9, 7, 12, 2003), 0.0);
s2.add(new Minute(15, 10, 7, 12, 2003), 0.0);

dataset.addSeries(s1);
dataset.addSeries(s2);
XYDataset dataset2 = dataset;

final JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Sample Chart",
            "Date", 
            "Value",
            dataset2,
            true,
            true,
            false
);
chart.setBackgroundPaint(Color.white);
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

BufferedImage originalImage = chart.createBufferedImage(500, 300, info);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( originalImage, "jpg", baos );
byte[] imageInByte = baos.toByteArray();

然后我只是使用 iText 将此字节数组放入 PDF 文件中,如下所示:

Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("hello.pdf"));
document.open(); 

Image image = Image.getInstance(imageInByte);
document.add(image);

document.close();

但是当我打开我的 PDF 文件时,我的图像应该是全黑的区域。

你知道可能是什么原因吗?

4

1 回答 1

3

看起来图像在转换过程中被反转了。试试下面的代码

package demo;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Minute;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;


public class PDF {

    public static void main(String[] args) {
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        final TimeSeries s1 = new TimeSeries("Series 1", Minute.class);
        s1.add(new Minute(0, 0, 7, 12, 2003), 1.2);
        s1.add(new Minute(30, 12, 7, 12, 2003), 3.0);   // 12h30  => value = 3
        s1.add(new Minute(15, 14, 7, 12, 2003), 8.0);

        final TimeSeries s2 = new TimeSeries("Series 2", Minute.class);
        s2.add(new Minute(0, 3, 7, 12, 2003), 0.0);
        s2.add(new Minute(30, 9, 7, 12, 2003), 0.0);
        s2.add(new Minute(15, 10, 7, 12, 2003), 0.0);

        dataset.addSeries(s1);
        dataset.addSeries(s2);
        XYDataset dataset2 = dataset;

        final JFreeChart chart = ChartFactory.createTimeSeriesChart(
                    "Sample Chart",
                    "Date", 
                    "Value",
                    dataset2,
                    true,
                    true,
                    false
        );
        chart.setBackgroundPaint(Color.white);
        java.awt.Image originalImage = chart.createBufferedImage(500, 300);
        try {           
            Document document=new Document();
            PdfWriter.getInstance(document,new FileOutputStream("hello.pdf"));
            document.open(); 
            com.lowagie.text.Image image1 = com.lowagie.text.Image.getInstance(originalImage,Color.white);
            document.add(image1);
            document.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

使用最新版本的 iText 你我得到这个 PDF

在此处输入图像描述

希望你会得到同样的结果

我已对您的代码进行了以下更改

java.awt.Image originalImage = chart.createBufferedImage(500, 300);

已删除

ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

因为它不需要。我也删除了ByteArrayOutputStream并替换为

   com.lowagie.text.Image image1 = com.lowagie.text.Image.getInstance(originalImage,Color.white);

com.lowagie.text.Imag请注意e使用全名,java.awt.Image因为 AWT 和 Lowagie 都有名为 Image 的类。

于 2012-06-27T12:07:04.163 回答