我正在尝试添加一个线图,在安卓的Pdf文件使用它的文本和AFreeChart。我已经在java中使用itextpdf和JFreeChart实现了这一点。但是我被困在了将图形添加到pdf的部分。请帮帮忙。
我不知道如何将画布绑定到PdfTemplate,或者有其他方法可以做到这一点,任何建议都是值得的。
这是我的android代码
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中绘制。
发布于 2019-04-25 22:25:57
我已经使用位图找到了我的问题的答案。
代码如下
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);https://stackoverflow.com/questions/55832232
复制相似问题