【问题标题】:Binding SQL Server table row to Editable PDF form C#将 SQL Server 表行绑定到可编辑的 PDF 表单 C#
【发布时间】:2018-04-10 16:40:33
【问题描述】:

我有一个可编辑的 pdf 表单。我创建了一个与 PDF 中的列相同的表。我的要求是将这些列数据(单行)绑定到现有 PDF 的文本框。

在这里,通过我的一段代码,我可以获取 PDF 表单字段的 ID,并将这些字段与数据绑定。

{
MemoryStream ms = new MemoryStream();
PdfReader reader = new PdfReader(Request.MapPath("~/test.pdf"));
PdfStamper formFiller = new PdfStamper(reader, ms);
AcroFields formFields = formFiller.AcroFields;

        ArrayList aa = new ArrayList();



        foreach (DictionaryEntry de in reader.AcroFields.Fields)
        {


            aa.Add(de.Key.ToString());

        }



        SqlCommand cmd = new SqlCommand("select Gname ,addr1 ,addr2,hno,gender ,postcode ,
        Fname,color,driving ,country ,city,height from example where Gname='pramod'", conn);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        conn.Open();
        int counter = 0;
        int colcount = ds.Tables[0].Columns.Count;
        if (!object.Equals(ds.Tables[0], null))
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int j = 0; j < colcount; j++)
                {

                        foreach (var item in aa)
                        {
                            if (item.ToString() == ds.Tables[0].Columns[j].ToString())                                
                            {
                                formFields.SetField(item.ToString(), ds.Tables[0].Rows[0][j].ToString());
                                var it = item.ToString();
                                counter++;
                                if (counter > 0)
                                {
                                    aa.Remove(it);
                                    break;
                                }

                            }
                }
            }
        }

    formFiller.Close();
    reader.Close();


    Response.ContentType = "application/pdf";
    Response.WriteFile("Default4.aspx");
    Response.BinaryWrite(ms.ToArray());

但问题是我随机获取这些 id,即与 pdf 中的顺序不同。所以我不能继续使用基于索引的循环绑定,因为 id 没有按顺序出现。而且我不知道哪个 Id 与哪个文本/复选框一起使用?

对于下图中的 ex,第一个 AADHAR NUMBER id 来自 pdf 的第 10 页。而且,你可以看到一些不规则的名字,比如 checkbox1,2 我不知道它是哪个文本框 id..

【问题讨论】:

    标签: c# .net pdf itext


    【解决方案1】:

    您不应该依赖表单中字段的顺序。这是不好的做法,因为任何人都可以随时更改模板中字段的顺序,然后由于顺序更改,您将不得不更改代码。这是在自找麻烦。

    您应该为 PDF 中的字段与数据库表中的字段名称相同(根据您编写的内容,我假设您已经这样做了)。如果名称相同,则可以这样做:

    ds.Tables[0].Rows[0][item.ToString()].ToString()
    

    请注意,您的循环很尴尬。你在浪费CPU!你为什么不这样做:

    if (ds.Tables[0].Rows.Count > 0)
    {
        foreach (var item in aa)
        {
            var it = item.ToString();
            formFields.SetField(it, ds.Tables[0].Rows[0][it].ToString());
        }
    }
    

    免责声明:我不是 C# 开发人员,我只是在 Google 上搜索,直到找到答案。我没有测试答案,但我希望你能明白。

    我很难阅读和理解你的代码,因为你把它弄得太复杂了。我想这就是为什么没有人回答你的问题。有句话"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."我建议你以后也这样做;-)

    您还说您看到了一些不规则的名称,例如 checkbox1,2,我无法弄清楚它是哪个文本框 ID。 如果您不这样做,我们无法帮助您解决这个问题'不共享 PDF,但给定名称 checkbox1checkbox2、... 我会假设这些不是 文本框,而是它们是复选框。请不要混淆文本框(可以有任何字符串值)和复选框(可以有两个可能值之一,其中之一是off)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多