【问题标题】:Adding an Image to a PDF File将图像添加到 PDF 文件
【发布时间】:2012-06-25 14:55:05
【问题描述】:

我正在尝试将图像添加到报告模板。我正在使用此代码添加字符串值:

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

【问题讨论】:

    标签: java itext acrofields


    【解决方案1】:

    我用这段代码解决了这个问题:

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

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 1970-01-01
      • 2017-08-27
      • 2013-04-04
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      相关资源
      最近更新 更多