Question

I'am currently trying to fill out a predefined Form with itextsharp. All works well except adding an image. This worked already before with the FDF toolkit from Adobe, which was compiled into .NET 1.1. This isn't working with .NET 4.0 anymore and I switched to itextsharp. The way the image was added previously was by changing the icon of a button in a predefined form. That rotateted and scaled the image correctly. Unfortunatly I couldn't find any Method to do this with itextsharp. The code was:

FdfAcX_Doc.FDFSetAP("img", 0, "path\\to\\img.pdf", 1);

(Doc: http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/FDFtkRef.pdf)

Now I was experimenting with itextsharp and trying to add the image manually, e.g.:

PdfContentByte pdfContentByte = pdfStamper.GetOverContent(1);
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(picfile);
iTextSharp.text.Rectangle imageRect = new iTextSharp.text.Rectangle(338f, 65f, 250f, 200f, 270);
img.ScaleToFit(imageRect.Width, imageRect.Height);
img.SetAbsolutePosition(65, 250);
pdfContentByte.AddImage(img);

But unfortunatly firstly the image is not rotated corretly and secondly the image is way too big. I tried several settings but couldn't hit the correct one. Does anyone have an idea what I'm doing wrong?

PDF Form can be downloaded here: http://www26.zippyshare.com/v/24914063/file.html

Please help, I've been sitting at this problem for a day now and can't think of anything more. Thanks!

Was it helpful?

Solution

Your code sample isn't consistent with the question in the title.

In your code sample, you're adding an image to the content of a page. In your title, you're asking to replace the icon in a button field. Button fields aren't part of the content. They are visualized using widget annotations.

I would have expected code that looks like this:

AcroFields form = stamper.AcroFields;
PushbuttonField ad = form.GetNewPushbuttonFromField(buttonFieldName);
ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
ad.ProportionalIcon = true;
ad.Image = Image.GetInstance(pathToNewImage);
form.ReplacePushbuttonField("advertisement", ad.Field);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top