0

I want to create multiple page pdf from Android listview. I have successfully achieved converting all the listview items into pdf even if they are not all visible using itextg. The problem I faced is that if the items in the listview or the captured image of the listview exceeds the length of the page in the pdf it cuts the the rest of the items.

I want to create a multiple page pdf if the first page is full then write the rest of the image part in the second image.

This is the code which creates the pdf by capturing all the listview item:

//method which generate pdf of the attendance data
    private void save_as_pdf() {
        //First Check if the external storage is writable
        String state = Environment.getExternalStorageState();
        if (!Environment.MEDIA_MOUNTED.equals(state)) {
            // Toast.makeText(context,"")
        }

        //Create a directory for your PDF
        final File pdfDir = new File(Environment.getExternalStorageDirectory() + "/Documents", "attendance_report");
        if (!pdfDir.exists()) {

            pdfDir.mkdir();
        }

        //take  screen shoot of the entire listview of the attendance report
        ListView listview = studentlist;
        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);
        bigcanvas.drawColor(getResources().getColor(R.color.white));
        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;
        }

        //Now create the name of your PDF file that you will generate
        File pdfFile = new File(pdfDir, "report_for( " + course + "," + semister + section + "," + date + ").pdf");


        try {
            com.itextpdf.text.Document document = new com.itextpdf.text.Document();

            PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
            document.addTitle("Attendance Report Generated For course:" + course + "  Semester:" + semister + "  Section:" + section + "  Date:" + date);
            document.addHeader("Header", "Department Of Information Science");
            document.addCreator("Department Of Information Science");
            document.addCreationDate();
            document.bottomMargin();
            document.setPageCount(3);
            document.open();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bigbitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            addImage(document, byteArray);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void addImage(com.itextpdf.text.Document document, byte[] byteArray) {
        Image image = null;
        try {
            image = Image.getInstance(byteArray);
        } catch (BadElementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // image.scaleAbsolute(150f, 150f);
        try {
            document.add(image);

        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Olana Kenea
  • 39
  • 2
  • 11
  • 1
    Yuck: are you saying that you create a PDF with text in a raster image instead of creating a PDF where the text is added properly using a font? That's a terrible idea. If you were a student, you should get an F for your code. It boils down to "how do I distribute an image over different pages) but even if you get an answer, the result will always be a bad PDF. My advise: throw away your code and start anew with a better design. Why is the attendance data stored in an image? And why do you combine all these small bitmaps into one large image? Please throw away your code! Please start anew! – Bruno Lowagie Apr 15 '18 at 10:56
  • you didn't even give me any solution just talking can you give me a bit of code to work what you sayed rather than just talking – Olana Kenea Apr 15 '18 at 11:09
  • Nope. Stack Overflow isn't made for people to ask other people to do their work for them. I told you what you were doing wrong. Learn from those mistakes. Start anew. If you have a problem with your new code, post a new question. – Bruno Lowagie Apr 15 '18 at 12:06

1 Answers1

0
//take screen shot all records from listview
    for (int i = 0; i < itemscount; i++) {
        listview.setSelection(i);
        View childView = adapter.getView(i, null, listview);
        ViewGroup.LayoutParams lp = childView.getLayoutParams();
        if (lp == null) {
            lp = new ViewGroup.LayoutParams(-2,-2);
        }
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        childView.setLayoutParams(lp);
        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();
}
  • You can try this code that I had surfed from other web page. – Sari Ratanak Aug 03 '18 at 15:29
  • Please provide some explanation to your code, how it is solving the problem and why it's a good way to do so. Additionally, if you take code from another website/page/book/etc, it's always nice to give credits and include a source link! – Capricorn Aug 03 '18 at 15:38