Add image in an existing PDF with itextsharp

孤者浪人 提交于 2019-12-11 10:36:33

问题


SO i have a created a PDF template form with textbox which are editable fields. I am able to generate the pre-populated PDF with values from my database into the template through MVC 4.0 Application. Which works fine. Now i want to add a image from a folder into the PDF which will distinguished one form with another form. The image will depend on the user in-put. Image will go at the bottom of the PDF. I don't see any image-box or image container as a filed option. Only one i can see are text-box,checkbox,radio,list box etc but nothing like a iimage holder.

Dose any one know how to add image dynamically into PDF?


回答1:


You can find the answer to your question in the official documentation, more specifically in chapter 8. In section 8.2.3, entitled "Pushbuttons", I explain that we usually use buttons as placeholders for images, because buttons can have an icon.

The ReplaceIcon example shows how you can replace the icon of an exising button. As you are using C#, you may want to take a look at ReplaceIcon.cs:

PdfReader reader = new PdfReader(aPdf);
using (MemoryStream ms = new MemoryStream()) {
    using (PdfStamper stamper = new PdfStamper(reader, ms)) {
        AcroFields form = stamper.AcroFields;
        PushbuttonField ad = form.GetNewPushbuttonFromField("button_name");
        ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
        ad.ProportionalIcon = true;
        ad.Image = Image.GetInstance(yourImage);
        form.ReplacePushbuttonField("button_name", ad.Field);
      }
    }
    // ms will contain your PDF in memory
}
reader.Close();

Note that the line ad.ProportionalIcon = true; will scale your image so that it fits the button.



来源:https://stackoverflow.com/questions/29683667/add-image-in-an-existing-pdf-with-itextsharp

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