可填写形式的itext图像字段

时间:2012-12-07 01:21:33

标签: java pdf itext

在itext 2中,下面的代码运行良好,在itext 5中它失败了。 有没有人有一个如何将图像添加到PDF上的图像字段的示例?

这是我的代码......

public static void atsSetFieldImage(AcroFields form, PdfStamper stamper, String fieldName, Image img) 
{
    try 
    {
        float[] photograph = form.getFieldPositions(fieldName);
        Rectangle rect = new Rectangle(photograph[1]
                           , photograph[2]
                           , photograph[3]
                           , photograph[4]);

        //Image img = Image.getInstance(imageName);
              image1.scaleToFit(rect.getWidth(), rect.getHeight());
              image1.setAbsolutePosition(
                                    photograph[1] + (rect.getWidth() - image1.getScaledWidth()) / 2
                                  , photograph[2] + (rect.getHeight() - image1.getScaledHeight()) / 2);

        PdfContentByte cb = stamper.getOverContent((int)photograph[0]);
                       cb.addImage(image1);

    }
    catch(Exception e) 
    {
        //e.printStackTrace();
    }
} 

1 个答案:

答案 0 :(得分:1)

如何将图像添加到acroForm字段:

Rectangle rect = form.getFieldPositions(fieldName)[0].position;
int page = form.getFieldPositions(fieldName)[0].page;
//Scale it
image1.scaleAbsolute(rect.getWidth(), rect.getHeight());
//Position it
img.setAbsolutePosition(rect.getLeft(), rect.getBottom());
//Add it to the correct page
stamper.getOverContent(page).addImage(img);