Created
September 1, 2015 11:18
-
-
Save javiergamarra/485e8af27775b7fd7834 to your computer and use it in GitHub Desktop.
Add image with 200dpi in PDFBox (for a friend)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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