1

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 2

5
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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
on this line InputStream inputStream = MainActivity.this.getAssets().open( "launchscreen.png"); its showing me error and saying to add cast to inputStream. Any idea?
just changed to java.io.InputStream inputStream = MainActivity.this.getAssets().open( "radiobuttonoff.png");
1

You can use

Image image = Image.getInstance(yourImageByteArray); document.add(image); 

where document is an object of Document

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.