我们可以使用itext Android以PDF格式绘制PieChart吗? [英] Can we draw PieChart in PDF using itext Android?

查看:207
本文介绍了我们可以使用itext Android以PDF格式绘制PieChart吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android中使用itext在我的PDF中添加饼图。
但是与piechart相关的所有示例似乎都使用以下2D类:

I want to add Pie chart in my PDF using itext in Android. But all the example related to piechart seems to use the following 2D classes:

Graphics2D graphics2d = template.createGraphics(width, height,
                    new DefaultFontMapper());   
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
                    height);

android版本中实际上是awt类而不可用 的itext-5。
使用2D分类的任何替代方法?

that are actually awt classes and not available in android version of itext-5. Any alternate to using 2D classses ?

已编辑:我正在使用itextg版本的android。它缺少上面提到的2D类。

Edited: I am using itextg version for android. Its missing 2D classes mentioned above.

推荐答案

所以我找到了解决方案。
我使用 MPAndroidChart 制作PieChart。
这个库有一个从绘制的饼图中获取位图的方法。

So i have found a solution. I was using MPAndroidChart for making the PieChart. This library has a method for getting the bitmap from the drawn Piechart.

pieChart.getChartBitmap();

因此,一旦获得位图,iText将允许您将位图添加到您的pdf中。
最终代码可能如下所示:

So, once you get the bitmap, iText will let you add the bitmap into your pdf. The final code may looks like this:

 ByteArrayOutputStream stream = new ByteArrayOutputStream();
 Bitmap bitmap = pieChart.getChartBitmap();
 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
 Image myImg = Image.getInstance(stream.toByteArray());
 myImg.setAlignment(Image.MIDDLE);
 document.add(myImg);

干杯!

这篇关于我们可以使用itext Android以PDF格式绘制PieChart吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆