我有一个 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
谢谢!