Skip to content

Instantly share code, notes, and snippets.

@javiergamarra
Created September 1, 2015 11:18
Show Gist options
  • Save javiergamarra/485e8af27775b7fd7834 to your computer and use it in GitHub Desktop.
Save javiergamarra/485e8af27775b7fd7834 to your computer and use it in GitHub Desktop.
Add image with 200dpi in PDFBox (for a friend)
double resolution = 200;
double points = 72;
double ratio = resolution / points;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawable);
Double width = bitmap.getWidth() / ratio;
Double height = bitmap.getHeight() / ratio;
PDDocument document = new PDDocument();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
InputStream imageStream = new ByteArrayInputStream(baos.toByteArray());
PDImageXObject image = JPEGFactory.createFromStream(document, imageStream);
PDPage page = new PDPage(PDRectangle.A0);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawImage(image, 0, 0, width.intValue(), height.intValue());
contentStream.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment