0

我正在尝试使用 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 中。

4

1 回答 1

0

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

代码如下

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);
于 2019-04-25T14:25:56.973 回答