Search code examples
javaandroidarrayspdfpdfdocument

My images return as blank in PDF document


In my app I upload images to my company server These images are converted to a pdf document and uploaded

I am using the built in PDFdocument for this, now my problem is the pages are dynamic depending on the amount of images the user selects or takes with the camera, the images are put into an ARRAYLIST(String) I then call the position of the photo and use canvas to paint it to the pdf

Now my problem is the pdf document generates with the appropriate page numbers IE. If the user selects 4 images it returns 4 pages BUT there are NO images

I found the problem My second for loop doesn't work correctly as it doesn't give out a number ad I want it to so basicly

Code For The PDF

    PdfDocument document=new PdfDocument();
            // crate a page description
            PdfDocument.PageInfo pageInfo;
            PdfDocument.Page page;
            Canvas canvas;
            int i;

            Bitmap image;

**here I loop for page Creation**

            for (i=0; i < list.size(); i++)  {

                pageInfo=new PdfDocument.PageInfo.Builder(3000, 6000, 1).create();
                page=document.startPage(pageInfo);

Here I loop to change the position of the images in the array list this on is not looping through the arraylist positions like it should

for(int t = 0; t< list.indexOf(0); t+=1){

                    canvas=page.getCanvas();
                    image=BitmapFactory.decodeFile(String.valueOf(list.get (t)));
                    image.setDensity(DENSITY_XHIGH);
                    canvas.drawBitmap(image, 1, 1, null);
                    canvas.setDensity(DENSITY_XHIGH);
                }



            document.finishPage(page);
        }


        String directory_path=Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
            File file=new File(directory_path);
            if (!file.exists()) {
                file.mkdirs();
            }
            String timeStamp=(new SimpleDateFormat("yyyyMMdd_HHmmss")).format(new Date());
            String targetPdf=directory_path + timeStamp + ".pdf";
            File filePath=new File(targetPdf);
            try {
                document.writeTo(new FileOutputStream(filePath));
                Toasty.info(this, "PDF Created", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Log.e("main", "error " + e.toString());
                Toasty.error(this, "Error making PDF" + e.toString(), Toast.LENGTH_LONG).show();
            }
            // close the document
            document.close();

Solution

  • 1.Use PrintAttributes.build() to set attributes of the pdf properly

    1. and pass printattrs to printerpdfDocument(context,/*print attributes obj goes here */)

    2. Create a Rect() object

    3. Using rect() object create pageviews in forloop and add these pageview to document

    4. User FileOutputStream(// your pdf file), pass this to document, then

    fileOutPutstream = new FileOutputStream(pdffilename);
    doucument.writeTo(fileOutPutstream);
    doucument.close();
    fileOutPutstream.close(); // close your stream 
    

    Above is rough implementation, this should give you a idea to proceed.