1、 /**
* 字符串转图片
* @param base64String
*/
public static boolean Base64ToImage(String imgStr,String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
2、在pdf中显示公章图片
PdfPTable table2 = new PdfPTable(1);
table2.setWidthPercentage(100);
table2.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
table2.setTotalWidth(new float[] { 100f });
String imagePath ="D://"+UUID.randomUUID().toString()+".jpg" ;
String imageBase64=info.getBase64();
//将字符串转化成图片
boolean isSuccess = this.Base64ToImage(imageBase64
, imagePath);
Image jpg=Image.getInstance(imagePath);
jpg.setAlignment(Image.ALIGN_JUSTIFIED_ALL);
document.add(jpg);