2

itext 2では、以下のコードはうまく機能しましたが、itext5では失敗します。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();
    }
} 
4

1 に答える 1

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);
于 2012-12-07T07:46:07.813 に答える