[英]Adding an Image to a PDF File
我正在嘗試將圖像添加到報告模板。 我使用以下代碼添加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;
}
}
我用以下代碼解決了問題:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.