在Android中将Listview转换为pdf

Vis*_*akh 2 pdf android listview itext

我有一个自定义列表视图,我想从整个列表视图制作pdf。我引用了许多文章,并在下面的代码中实现了将我的listview转换为pdf的代码。但是问题是它不包含整个listview项目。pdf中仅提供前几项。

我将listview转换为pdf的功能

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String state = Environment.getExternalStorageState();
            if (!Environment.MEDIA_MOUNTED.equals(state)) {

            }
            File pdfDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOCUMENTS), "MyProject");
            if (!pdfDir.exists()){
                pdfDir.mkdir();
            }
            Bitmap screen =  getWholeListViewItemsToBitmap();
            File pdfFile = new File(pdfDir, "myPdfFile_new.pdf");

            try {
                Document document = new Document();
                PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
                document.open();
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                screen.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                addImage(document,byteArray);
                document.close();
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

addImage方法

private static void addImage(Document document,byte[] byteArray)
{
    Image image = null;
    try
    {
        image = Image.getInstance(byteArray);
    }
    catch (BadElementException e)
    {
        e.printStackTrace();
    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    try
    {
        document.add(image);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

获取列表项的方法

public Bitmap getWholeListViewItemsToBitmap() {

    ListView listview    = StockReportActivity.category_list;
    ListAdapter adapter  = listview.getAdapter();
    int itemscount       = adapter.getCount();
    int allitemsheight   = 0;
    List<Bitmap> bmps    = new ArrayList<Bitmap>();

    for (int i = 0; i < itemscount; i++) {

        View childView      = adapter.getView(i, null, listview);
        childView.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
        childView.setDrawingCacheEnabled(true);
        childView.buildDrawingCache();
        bmps.add(childView.getDrawingCache());
        allitemsheight+=childView.getMeasuredHeight();
    }
    Bitmap bigbitmap    = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
    Canvas bigcanvas    = new Canvas(bigbitmap);
    Paint paint = new Paint();
    int iHeight = 0;
    for (int i = 0; i < bmps.size(); i++) {
        Bitmap bmp = bmps.get(i);
        bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
        iHeight+=bmp.getHeight();
        bmp.recycle();
        bmp=null;
    }
     return bigbitmap;
    }
Run Code Online (Sandbox Code Playgroud)

我的列表视图中包含大量项目,因此如何将整个列表视图转换为pdf。感谢您的所有建议

pro*_*erX 5

您可以在中获取显示在屏幕上的项目数recyclerView,因此为第一组视图创建位图,然后您可以动态滚动到最后显示的项目(即noOfItemDisplayed + 1)下方的项目,再次获取位图,同样全部获取所有位图为ArrayList<Bitmap>从中可以创建PDF文件,请参阅下面从位图的ArrayList中创建PDF的代码:

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void createPdf() throws IOException {

    PdfDocument document = new PdfDocument();
    PdfDocument.Page page = null;
    // crate a page description
    for (int i = 0; i < bitmaps.size(); i++) {
        Bitmap bitmap = bitmaps.get(i);
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(1400, 1979, i).create();

        // start a page
        page = document.startPage(pageInfo);
        if (page == null) {
            return;
        }
        Canvas canvas = page.getCanvas();
        canvas.drawBitmap(bitmap, 0, 0, null);
        document.finishPage(page);
    }

    // finish the page


    // write the document content
    fileHandler = new FileHandler(this, getString(R.string.app_name));
    File pdf = fileHandler.getNewFileToWrite(AppConstants.FileExtensions.PDF); //crete and get file

    try {
        document.writeTo(new FileOutputStream(pdf));
    } catch (IOException e) {
        logger.error(e);
    }

    // close the document
    document.close();

}
Run Code Online (Sandbox Code Playgroud)