How to change a Button Icon of a PDF Formular with itextsharp?

柔情痞子 提交于 2020-01-04 07:01:33

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/13026451/how-to-change-a-button-icon-of-a-pdf-formular-with-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!