Android PdfDocument file size

后端 未结 5 1620
予麋鹿
予麋鹿 2021-01-05 02:03

I want to generate a PDF File from a View using the PdfDocument android class introduced in KitKat. I managed to do it, and the file is so far generated ok, end

5条回答
  •  滥情空心
    2021-01-05 02:33

    Using PDFDocument, be sure to downscale your images prior to drawing them in the canvas.

    When drawing to the screen, this is enough to scale the bitmap :

    canvas.drawBitmap(bmp, src, dst, paint);
    

    However, when using the canvas from PdfDocument.Page.getCanvas, this canvas will not downscale the bitmap, it will just squeeze it into a smaller zone. Instead you should do something like this:

    // Scale bitmap : filter = false since we are always downSampling
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp, dstWidth, dstHeight,
        false); // filter=false if downscaling, true if upscaling
    
    canvas.drawBitmap(scaledBitmap, null, dst, paint);
    
    scaledBitmap.recycle();
    

    This is embedded in Android so it is much easier than using a third-party library. (The above was tested on a Marshmallow platform)

提交回复
热议问题