Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带iTextSharp的PDF表格_C#_.net_Pdf Generation_Itextsharp_Pdf Form - Fatal编程技术网

C# 带iTextSharp的PDF表格

C# 带iTextSharp的PDF表格,c#,.net,pdf-generation,itextsharp,pdf-form,C#,.net,Pdf Generation,Itextsharp,Pdf Form,如何在iTextSharp中创建可填充的PDF文件。到目前为止,我可以创建一个包含文本的pdf文件,但是我在创建可填充字段方面遇到了困难。任何帮助或示例代码都将不胜感激。请尝试以下方法: AcroFields fields = pdf.AcroFields; fields.SetField("field_1", "1"); fields.SetField("field_2", "2"); 使用第三方软件(如AdobeLiveCycle Designer)制作pdf表单比使用iTextsharp

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

请尝试以下方法:

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

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

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

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

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