1

I have a base64 encoded string. I want to display this as an image in a PDF file. I am using iText to achieve this. I am using apache commons codec to convert Base64 to byteArray.Below is the code -

    Document document = new Document();
    PdfWriter.getInstance(document,new FileOutputStream("C:\\Path\\Path\\example.pdf"));
    document.open();
    String example = "...base64..String";
    byte[] decoded = org.apache.commons.codec.binary.Base64.decodeBase64(example.getBytes());
    Image image1 = Image.getInstance(decoded);
    document.add(image1);
    document.close();

This code executes without any error but when I open the generated PDF file it opens with an "internal error" and no image is displayed. What is the problem?

The complete Base64 string is -

_9j_4AAQSkZJRgABAQAAAQABAAD_2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj_2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj_wAARCAIVAUADASIAAhEBAxEB_8QAHQAAAwADAQEBAQAAAAAAAAAAAAECAwYHBQQICf_EAFkQAAEDAgQDBAYFBggKCQIHAAEAAhEDBAUSITEGQVEHEyJhFDJxgZGhF5Ox0dIVI0JSVcEIFiQzYnLh8CU0NUVWgpKVsrM3RlNzdYSiw_FEYyY2VIOUo8L_xAAZAQEBAQEBAQAAAAAAAAAAAAAAAQIDBAX_xAApEQEBAAIBBQACAQQCAwAAAAAAAQIREgMTITFRIkFxBDJhoYGRQlLw_9oADAMBAAIRAxEAPwD9OoTQgSE0IEhNCBITQgSE0IEhNCBITQgSE0IEhNCBITQgSE0IEhNCBITQgSE0IEhNCBITQgSE0IEhNCBITQgSE0IGhNCBL5sSvrXDLCve39enb2tBhfUq1DDWtHMoxO_tcLsK99iFxTt7Sgwvq1ahhrWjmVw17sR7asX7

Thanks!

Archit Arora
  • 2,508
  • 7
  • 43
  • 69
  • Hi! Have you tried looking around stackoverflow? I have found something that might help you. First you need to `var imagedata = Convert.FromBase64String(base64Data);` and afther that you might want to get the instance of image. Basically it is correct what you have written, but there are many guys trying to do the same as you. Hope [this](http://stackoverflow.com/questions/13843736/parsing-html-to-pdf-with-itext-image-base64) link might help you. – jPO Apr 08 '15 at 08:08
  • Can you please post the link here? – Archit Arora Apr 08 '15 at 08:10
  • Sorry, I pressed enter before I pasted it into text :D It is there now. – jPO Apr 08 '15 at 08:11

0 Answers0