I am trying to add a Line Chart to Pdf file in android using itext and AFreeChart. I have done this in java using itextpdf and JFreeChart. But i am stuck at the part where graph is added to pdf. Please help.
I don't know how to bind the canvas to PdfTemplate, or is there a different way of doing this, any suggestions are appreciated.
This is my android code
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);
This is my Java code
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);
The graph is not being plotted in the pdf at all.
I have found the answer for my question using Bitmap.
The code is as follows
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);