使用iText将base64编码的字符串显示为PDF文件中的图像

时间:2015-04-08 08:05:58

标签: itext apache-commons-codec

我有一个base64编码的字符串。我想将其显示为PDF文件中的图像。我正在使用iText来实现这一目标。我使用apache commons codec将Base64转换为byteArray。以下是代码 -

    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();

此代码执行时没有任何错误,但是当我打开生成的PDF文件时,它会打开一个"内部错误"并且不显示图像。有什么问题?

完整的Base64字符串是 -

_9j_4AAQSkZJRgABAQAAAQABAAD_2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj_2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj_wAARCAIVAUADASIAAhEBAxEB_8QAHQAAAwADAQEBAQAAAAAAAAAAAAECAwYHBQQICf_EAFkQAAEDAgQDBAYFBggKCQIHAAEAAhEDBAUSITEGQVEHEyJhFDJxgZGhF5Ox0dIVI0JSVcEIFiQzYnLh8CU0NUVWgpKVsrM3RlNzdYSiw_FEYyY2VIOUo8L_xAAZAQEBAQEBAQAAAAAAAAAAAAAAAQIDBAX_xAApEQEBAAIBBQACAQQCAwAAAAAAAQIREgMTITFRIkFxBDJhoYGRQlLw_9oADAMBAAIRAxEAPwD9OoTQgSE0IEhNCBITQgSE0IEhNCBITQgSE0IEhNCBITQgSE0IEhNCBITQgSE0IEhNCBITQgSE0IEhNCBITQgSE0IGhNCBL5sSvrXDLCve39enb2tBhfUq1DDWtHMoxO_tcLsK99iFxTt7Sgwvq1ahhrWjmVw17sR7asX7

谢谢!

0 个答案:

没有答案