我使用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文件时,我的图像应该是全黑的。
你知道它可能是什么原因吗?
答案 0 :(得分:2)
看起来图像在转换过程中被反转。请尝试以下代码
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的类。