0

I am trying to add an image to a report template. I am adding the String values with this code:

form.setField("name", name); 

But I can not add an image to the form using a similar code:

 form.setField("photo", photo);

I searched the web but couldn't find a solution. The whole code is:

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;
        }
    }
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
lamostreta
  • 2,359
  • 7
  • 44
  • 61

1 Answers1

0

I solved the problem with this code:

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);
lamostreta
  • 2,359
  • 7
  • 44
  • 61