Listview to pdf in android

后端 未结 1 751
北海茫月
北海茫月 2021-01-07 02:12

I have a custom listview and I want to make pdf from the whole listview. I refered many posts and implemented below code which converts my listview to pdf. But prob

相关标签:
1条回答
  • 2021-01-07 02:40

    You can get the number of items displayed on screen in recyclerView, so create bitmap for first set of views, after that you can dynamically scroll to item below the last item displayed (i.e noOfItemDisplayed+1), again get bitmap, likewise all get all bitmaps into ArrayList<Bitmap> from which you can create pdf file, refer to code below to create pdf from arraylist of bitmaps:

    @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();
    
    }
    
    0 讨论(0)
提交回复
热议问题