使用 Afreechart 和 itextg 将 lineChart 添加到 PDF 文件



我正在尝试使用itext和AFreeChart将折线图添加到Android中的PDF文件中。我已经使用itextpdf和JFreeChart在java中完成了此操作。但是我被困在将图形添加到pdf的部分。请帮忙。

我不知道如何将画布绑定到 PdfTemplate,或者是否有其他方法可以做到这一点,任何建议都值得赞赏。

这是我的安卓代码

AFreeChart chart = ChartFactory.createLineChart("R","Unit1","unit2",dataset, 
                PlotOrientation.HORIZONTAL,true,false,false);
PdfContentByte cb = pdfWriter.getDirectContent();
PdfTemplate tp = cb.createTemplate(100,200);
Canvas canvas = new Canvas();
RectShape rectShape = new RectShape(0,0,100,200);
chart.draw(canvas,rectShape);
cb.addTemplate(tp,280,35);

这是我的Java代码

PdfContentByte cb = pdfWriter.getDirectContent();
PdfTemplate tp = cb.createTemplate(width, height);
Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
chart.draw(g2d, r2d);

该图根本没有在 pdf 中绘制。

我已经使用位图找到了问题的答案。

代码如下

Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
RectShape rectShape = new RectShape(0,0,width,height);
chart.draw(canvas,rectShape);
ByteArrayOutputStream stream3 = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream3);
Image temp = Image.getInstance(stream3.toByteArray());
temp.setAbsolutePosition(0,0);
tp.addImage(temp);
cb.addTemplate(tp,0,100);

最新更新