【问题标题】:PDF form with iTextSharp带有 iTextSharp 的 PDF 表单
【发布时间】:2011-09-02 15:11:52
【问题描述】:

如何在 iTextSharp 中创建可填写的 PDF 文件。截至目前,我可以创建一个包含文本的 pdf 文件,但是我在创建可填充字段时遇到了困难。任何帮助或示例代码将不胜感激。

【问题讨论】:

    标签: c# .net pdf-generation itextsharp pdf-form


    【解决方案1】:

    使用 Adobe LiveCycle Designer 等第三方软件制作 pdf 表单比使用 iTextsharp 编码更容易。您只需根据需要在表单中放置一些文本框、复选框或单选按钮,设置其字体、字体大小和数据类型。

    标记所有字段名称(如“TextField1”等)。创建一个新的 Web 表单,其中包含一些向该字段提供数据的文本框。

    然后您可以使用如下代码轻松填写该表格:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            var reader = new PdfReader(Server.MapPath("~/Emptyform.pdf"));
            var output = new MemoryStream();
            var stamper = new PdfStamper(reader, output);
            stamper.AcroFields.SetField("TextField1", TextBox1.text);
            stamper.FormFlattening = true;
            stamper.Close();
            reader.Close();
            File.WriteAllBytes(Server.MapPath("~/Filledform.pdf"),output.ToArray());        
        }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

      AcroFields fields = pdf.AcroFields;
      fields.SetField("field_1", "1");
      fields.SetField("field_2", "2");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-08
        • 2011-08-22
        • 1970-01-01
        • 2016-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多