1

I have a JPEG image on the device which I want to draw on a canvas to compose a simple PDF (just the image) using (PdfDocument).

The only way that the Canvas api seems to offer to draw an image, is to give it a Bitmap, to decode my JPEG into a Birmap and have all its date accessible the image size will largly increase. (Would look like this)

val imageStream = contentResolver.openInputStream(it)
val selectedImage = BitmapFactory.decodeStream(imageStream, null, options)!!
pdfDocPage.canvas.drawBitmap(scaledImage, 0f, 0f, Paint())

Since the only way to use PdfDocument is through a Canvas, is there a way to draw an image on a Canvas without decoding it, so the PDF won't end up being 10x larger than the image's size as a JPEG?

John Sardinha
  • 3,566
  • 6
  • 25
  • 55
  • `PdfDocument` is designed for printing, where the size is not that much of an issue. If you wish to create PDF files, use a better API (e.g., iText). By definition, a `Canvas` needs to decode the image, because `Canvas` is not set up for creating PDFs, but rather for on-screen rendering. – CommonsWare Apr 01 '20 at 15:56
  • I don't think so, a canvas is just a bunch of pixels and therefore you need to have an uncompressed array of bytes. In any case unless you're worrying about the memory consumption while drawing onto the canvas, the main problem is that when PdfDocument writes the image in the PDF file, it doesn't compress it. The PDF format stores the image information inside the PDF in a specific way and therefore unless the Android library you use allows you to specify a compression format (like DCTDecode), you won't get a smaller PDF size. As suggested before, you will need to use iText or a similar library – Jorge Apr 01 '20 at 19:08

0 Answers0