I have a png image in drawable folder and I want to use that png image in the pdf . I am using itextpdf.jar for creating the pdf. I searched for solution ,but I am not able to figure it out.
2 Answers
try { InputStream inputStream = MainActivity.this.getAssets().open( "launchscreen.png"); Bitmap bmp = BitmapFactory.decodeStream(inputStream); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); Image signature; signature = Image.getInstance(stream.toByteArray()); signature.setAbsolutePosition(400f, 150f); signature.scalePercent(100f); document.add(signature); document.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } I hope its useful to you.
3 Comments
user2699728
its showing me this error. 05-15 14:04:09.008: E/AndroidRuntime(28222): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tag.texttopdf/com.tag.texttopdf.MainActivity}: java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream cannot be cast to com.itextpdf.text.pdf.codec.Base64$InputStream
user2699728
on this line InputStream inputStream = MainActivity.this.getAssets().open( "launchscreen.png"); its showing me error and saying to add cast to inputStream. Any idea?
user2699728
just changed to java.io.InputStream inputStream = MainActivity.this.getAssets().open( "radiobuttonoff.png");