我使用的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文件,在我的形象应该是该地区是全黑的。
你有什么可能是它的原因的想法?
它看起来像在转换过程中被颠倒的图像。 试试下面的代码
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
希望你会得到相同的
我已经对你的代码follwing变化
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呼吁类图片。