JFreechart和iText结合使用的问题
用于项目需要,我在项目中使用了JfreeChart生成报表图片,使用了如下代码
/**
* 拿到饼图的JFreeChart
* @return
*/
public static JFreeChart getPieJFreeChart(){
// 创建主题样式
StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
// 设置标题字体
standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));
// 设置图例的字体
standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));
// 设置轴向的字体
standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
// 应用主题样式
ChartFactory.setChartTheme(standardChartTheme);
return ChartFactory.createPieChart3D("图1.1测试通过情况",piedataset, true, false, true);
}
/**
* 生成饼图
*
* @return
* @throws IOException
*/
public static String getView() throws IOException {
// 通过工厂类生成JFreeChart对象
JFreeChart chart = getPieJFreeChart();
PiePlot pieplot = (PiePlot) chart.getPlot();
pieplot.setLabelFont(new Font("宋体", 0, 14));
// 没有数据的时候显示的内容
pieplot.setNoDataMessage("无数据显示");
// 设置背景颜色
pieplot.setBackgroundPaint(null);
// 在饼图的各块中显示百分比
pieplot.setCircular(true);
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} = {2},{1}例", NumberFormat.getNumberInstance(),NumberFormat.getPercentInstance()));
pieplot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
HttpServletResponse response = ServletActionContext.getResponse();
ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 550,400, info);
response.getOutputStream().flush();
response.getOutputStream().close();
return null;
}
以上是生成一个饼图报表的代码,然后我使用了iText来生成word文档,我想在word文档生成时,把以上的报表图片插入到word文档中。要怎么做??我写了如下代码,但是不行,
public String exportOffice() throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Image histogramImage = Image.getInstance(getView());
createWord(outputStream,histogramImage,null);
HttpServletResponse response = ServletActionContext.getResponse();
response.addHeader("Content-Disposition", "attachment; filename=testReport.doc");
response.setContentType("application/octet-stream");
response.setContentLength(outputStream.size());
ServletOutputStream out = response.getOutputStream();
outputStream.writeTo(out);
out.flush();
out.close();
return null;
}
我要怎么才能在创建word的时候拿到Jfreechart生成的报表图片捏?求大神解救啊! Jfreechart itextword jfreechart itext word --------------------编程问答-------------------- itext结合jfreechart,我以前用过,不过我输出的是pdf,应该是一样的。
我是先把jfreechart图片输出到磁盘,然后itext从磁盘加载。 --------------------编程问答-------------------- 哎。木有找到能用的方法。只能先把图片写在磁盘上,导出word后在删除。。但是这样频繁的操作磁盘始终是不好的。。 --------------------编程问答--------------------
JFreeChart chart=...;
BufferedImage bufferedImage = chart.createBufferedImage(550, 400);
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean flag = ImageIO.write(bufferedImage, "jpeg", out);
byte[] bytes = out.toByteArray();
Image img = Image.getInstance(bytes);
img.setAbsolutePosition(0, 0);
img.setAlignment(Image.LEFT);// 设置图片显示位置
document.add(img);
此法可行? --------------------编程问答-------------------- 这个问题已经解决了。
补充:Java , Web 开发