2012-06-25 157 views
0

我正在嘗試將圖像添加到報告模板。我這個代碼添加字符串值:將圖像添加到PDF文件

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

我解決了這個代碼的問題:

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