1

How to convert byte array to image in itext pdf and display in pdf. This is what i am doing but i am getting nullpointer exception..

    Base64 decoder = new Base64();
    byte[] imageByte = decoder.decode(imageInBase64EncodedString);
        Image image = null;
        try {
            image = Image.getInstance(imageByte);
        } catch (BadElementException e1) {
            e1.printStackTrace();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            image.scalePercent(15f);  <-----Here i am getting NullPointer Exception
            image.scaleAbsoluteWidth(520f);
            image.setAbsolutePosition(40f,725f);
            document.add(image);
       }catch(Exception e){
            e.printStackTrace();
       }

I am not understanding why image is not created even i pass them bytes.

Nicholas
  • 1,676
  • 12
  • 35
Rahul Verma
  • 514
  • 2
  • 8
  • 16
  • Did any of the `printStackTrace` calls above trigger? – mkl Nov 16 '16 at 11:52
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Dan Cornilescu Nov 16 '16 at 12:40
  • Ty for replying guys @mkl no. – Rahul Verma Nov 16 '16 at 13:17
  • @DanCornilescu Ty for commenting but i am not asking for nullpointer exception. I meant why image is null when i pass them byte array. – Rahul Verma Nov 16 '16 at 13:18
  • 1
    Please share the value of `imageInBase64EncodedString`; otherwise we can't answer your question. – Bruno Lowagie Nov 17 '16 at 13:17
  • @BrunoLowagie can i mail you?? – Rahul Verma Nov 17 '16 at 13:25
  • No you can't, unless you are a customer of iText Software (in which case you have access to a closed ticketing system). – Bruno Lowagie Nov 17 '16 at 13:27
  • Though your code may be running in an App Engine application, it doesn't really relate that product so I've proposed an edit to remove that tag. Regarding your actual error, I'm in agreement with @That Guy Lionel that the evidence points to the input string being the source of this problem. I would suggest verifying that this is a proper input string first and retesting your code. – Nicholas Feb 02 '17 at 17:32

1 Answers1

4

The problem is most likely the String value you're passing in, as it's very likely you're running into an exception for bad formatting in the value.

I've tried your example, using a BASE64 String example from PasteBin (http://pastebin.com/bfc1E1NV) that someone was kind enough to post, and was successful in running the scripts as per your code provided.

ItextPdf Example

Screenshot 1 enter image description here

Screenshot 2 (image Object is not null) enter image description here

org.apache.commons.codec.binary.Base64Example enter image description here

  • You Using ItextPdf Image Class. I am using import org.apache.commons.codec.binary.Base64 for decoding and getting values in byte. But at image.scalePercent(15f) i am getting null... – Rahul Verma Nov 16 '16 at 14:15
  • 1
    I've modified the example to include the library you're using and my tests still passed. Can you provide me with the String value you're trying to pass in? – That Guy Lionel Nov 16 '16 at 14:22