dadasda
dadasda

Reputation: 41

PDF form with iTextSharp

How can i create a fillable PDF file in iTextSharp. As of right now I can create a pdf file with text in it, however I'm struggling in creating fillable fields. Any help or sample code would be greatly appreciated.

Upvotes: 4

Views: 1995

Answers (2)

Soham
Soham

Reputation: 1281

It's more easy to make pdf form from third party softwares like Adobe LiveCycle Designer than coding with iTextsharp. You just have to put some textbox, checkbox or radio buttons as required with your form, setting its fonts, font size & its datatype.

Mark down all the field names(like "TextField1" etc). Create a new webform that contains some textboxes which feeds data to that fields.

You can then fill up that form easily with the code like below:

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

Upvotes: 2

Valentin V
Valentin V

Reputation: 25739

Try this:

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

Upvotes: 1

Related Questions