【问题标题】:Add lineChart to Pdf file using Afreechart and itextg使用 Afreechart 和 itextg 将 lineChart 添加到 Pdf 文件
【发布时间】:2019-04-24 14:15:16
【问题描述】:

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

【问题讨论】:

    标签: android itextg


    【解决方案1】:

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

    代码如下

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-19
      • 2015-05-07
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 2014-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多