用动态值填充PDF表单 [英] Fill PDF Form with dynamic values

查看:141
本文介绍了用动态值填充PDF表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我被困在这里.实际上,我正在尝试使用asp.net填写PDF表单.我得到了一些帮助,并编写了以下代码:

hi all,

I am stuck here. actually I am trying to fill a PDF form using asp.net. I get some help and write the following code:

private void fillForm()
    {
        try
        {
            string formFile = Server.MapPath("") + @"\Forms\fw4.pdf";
            string savepath = Server.MapPath("") + @"\Forms\new_fw4.pdf";
            PdfReader pdfReader = new PdfReader(formFile);
            using (FileStream stream = new FileStream(savepath, FileMode.Create))
            {
                PdfStamper pdfStamper = new PdfStamper(pdfReader, stream);
                AcroFields formFields = pdfStamper.AcroFields;

                foreach (DictionaryEntry de in formFields.Fields)
                 {
                     formFields.SetField("field name", "field value");
                 }            
                pdfStamper.FormFlattening = true;
                pdfStamper.Close();
            }
        }
        catch
        {
        }
    }



我希望程序显示列表中的所有字段.
我无法使用foreach循环来迭代所有可用字段.它给了我这个错误:



I want the program to show all fields in a List.
I am unable to iterate all available fields using the foreach loop. Its giving me this error:

Cannot convert type 'System.Collections.Generic.KeyValuePair<string,iTextSharp.text.pdf.AcroFields.Item>' to 'System.Collections.DictionaryEntry'





any help would be greatly appreciated.

推荐答案

该错误表示AcroFields类未实现IDictionary-它是KeyValuePairs的通用列表.

DictionaryEntry替换为KeyValuePair<string,iTextSharp.text.pdf.AcroFields.Item>KeyValuePair<string, object>或什至var,您应该没问题.

但是,您可能对foreach循环的内容有疑问-您无法在foreach循环中修改IEnumerable!
The error implies that the AcroFields class does not implement IDictionary - it is a generic List of KeyValuePairs instead.

Replace DictionaryEntry with KeyValuePair<string,iTextSharp.text.pdf.AcroFields.Item> or KeyValuePair<string, object> or even a var and you should be fine.

You may have a problem with the content of the foreach loop though - you cannot modify an IEnumerable from within a foreach loop on it!


这篇关于用动态值填充PDF表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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