0

I am using android pdf document library to convert image into pdf but it is generating very large size pdf.

PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo =new 
PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), 1).create();                            
PdfDocument.Page  page = document.startPage(pageInfo);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), 
bitmap.getHeight(),false);                        

Canvas canvas = page.getCanvas();
canvas.drawBitmap(scaledBitmap, 0f, 0f, null);
document.finishPage(page);

document.writeTo(new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+"/"+newPDFNameSingle));
document.close();

here is the apache pdf box implementation but it is cutting image in output pdf

PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);

PDPageContentStream contentStream = new PDPageContentStream(document, page);
InputStream inputStream = new FileInputStream(tempFile);
PDImageXObject ximage = JPEGFactory.createFromStream(document,inputStream);

contentStream.drawImage(ximage, 20, 20);
contentStream.close();

document.save(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+"/"+newPDFNameSingle);
                            document.close();

How can I achieve regular size pdf generation? My image in 100 kb in size but pdf generating 1 mb file.

James Z
  • 12,209
  • 10
  • 24
  • 44
Muhammad
  • 127
  • 1
  • 2
  • 13
  • `PdfDocument` is for creating PDFs to send to Android's print services. PDF size is not a concern for that scenario. If you want to create PDFs for other purposes, do not use `PdfDocument`. – CommonsWare Oct 29 '19 at 11:34
  • I also tried apache pdfbox library but it is cutting down the image after generation image cropped – Muhammad Oct 29 '19 at 11:43
  • @CommonsWare I have edited question in which I have tried apache pdfbox library – Muhammad Oct 29 '19 at 11:55
  • 1
    In your PDFBox code you should scale the image to fit the page, e.g. by setting the current transformation matrix accordingly before drawing the image, or you should create larger pages. – mkl Oct 29 '19 at 11:58
  • Thank you @mkl could you please give me some example – Muhammad Oct 29 '19 at 12:06
  • To scale: `contentStream.drawImage(pdImage, 20, 20, pdImage.getWidth() * scale, pdImage.getHeight() * scale);` with scale being a float value < 1. Please link to the result file and the source JPEG if you're not satisfied with the result. – Tilman Hausherr Oct 29 '19 at 12:28
  • What is scale here? I meant what value (float value < 1) is representing – Muhammad Oct 29 '19 at 12:42
  • `1` would be as currently, i.e. 100%. Try values like `0.5f` or `0.25f` and look at the result. You need to try out things to see what happens :-) – Tilman Hausherr Oct 29 '19 at 12:55

1 Answers1

2

In your android pdf document library code you set the page size to the image height and width values

PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), 1).create();                            

and draw the image at the origin:

canvas.drawBitmap(scaledBitmap, 0f, 0f, null);

You can do the same in your PDFBox code:

PDDocument document = new PDDocument();

PDImageXObject ximage = JPEGFactory.createFromStream(document,imageResource);

PDPage page = new PDPage(new PDRectangle(ximage.getWidth(), ximage.getHeight()));
document.addPage(page);

PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawImage(ximage, 0, 0);
contentStream.close();

(DrawImage test testDrawImageToFitPage)

Alternatively, as discussed in comments, you can set the current transformation matrix before drawing the image to scale it down to fit the page.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • You nailed it bro (Y). By the way Im now using your pdfbox solution. Thank you bro – Muhammad Oct 30 '19 at 04:45
  • @mkl the canvas.drawBitmap in api 28+ gives this error: `Software rendering doesn't support hardware bitmaps`. Do you know to solve? – RGS Jun 26 '20 at 10:18
  • Sorry, no, I have not dealt with this recently anymore. – mkl Jun 26 '20 at 13:33