我正在尝试将图像添加到报告模板中。我正在使用以下代码添加String值:
form.setField("name", name);
但是我无法使用类似的代码将图像添加到表单中:
form.setField("photo", photo);
我在网上搜索但找不到解决方案。整个代码是:
public static ByteArrayOutputStream personelSicilFormOlustur(String sablonDir, String name, byte[] photo) {
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
BaseFont fontTimes = BaseFont.createFont(sablonDir + "\\" + fontName, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
BaseFont fontTimesBold = BaseFont.createFont(sablonDir + "\\" + fontNameBold, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
BaseFont fontTimesItalic = BaseFont.createFont(sablonDir + "\\" + fontNameItalic, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
PdfReader reader = new PdfReader(sablonDir + "\\" + tmSablonForSicilKaydiName);
PdfStamper stamper = new PdfStamper(reader, baos);
AcroFields form = stamper.getAcroFields();
form.setField("name", name);
form.setField("photo", photo);
stamper.setFormFlattening(true);
stamper.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
return baos;
}
}
答案 0 :(得分:0)
我用这段代码解决了这个问题:
InputStream photoImage = new ByteArrayInputStream(photo);
BufferedImage bImageFromConvert = ImageIO.read(photoImage);
int type = bImageFromConvert.getType() == 0? BufferedImage.TYPE_INT_ARGB : bImageFromConvert.getType();
BufferedImage dimensionedImage = resizeImage(bImageFromConvert, type);
ImageIO.write(dimensionedImage, "jpg", new File("c:/new-darksouls.jpg"));
Image image1 = Image.getInstance("c:/new-darksouls.jpg");
image1.setAbsolutePosition(450f, 650f);
canvas.addImage(image1);