使用itextsharp在现有的PDF中添加图像 [英] Add image in an existing PDF with itextsharp

查看:570
本文介绍了使用itextsharp在现有的PDF中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我创建了一个带有文本框的PDF模板表单,这些文本框是可编辑的字段.我可以通过MVC 4.0应用程序将数据库中的值生成预填充的PDF到模板中.哪个工作正常.现在,我想将文件夹中的图像添加到PDF中,以将一种形式与另一种形式区分开.图像将取决于用户输入.图像将位于PDF的底部.我没有看到任何图像框或图像容器作为归档选项.我只能看到的是文本框,复选框,单选框,列表框等,但没有像iimage持有人那样的东西.

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.

有人知道如何将图像动态添加到PDF吗?

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

推荐答案

您可以在官方文档中找到问题的答案,更具体地在第8章中.在标题为按钮"的8.2.3节中,我解释了我们通常将按钮用作图像的占位符,因为按钮可以具有 icon .

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.

ReplaceIcon 示例显示了如何替换现有按钮的图标.使用C#时,您可能需要看一下

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

请注意,ad.ProportionalIcon = true;行将缩放图像,使其适合按钮.

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

这篇关于使用itextsharp在现有的PDF中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆