How to create pdfformfields using iTextSharp?

I am using iTextSharp and CSharp for creating the pdf. I need to add formfields like checkbox, radiobutton and dropdown which can not be edited.

I used this..

FileStream pdffile = new FileStream(path + "/Pdf/tes.pdf",FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(doc, pdffile);
doc.Open(); 
Rectangle rect = new Rectangle(100, 100, 100, 100);
RadioCheckField checkbox = new RadioCheckField(writer, rect, "bhjabsdf", "on");
checkbox.CheckType = RadioCheckField.TYPE_CHECK;
PdfFormField field = checkbox.CheckField;
writer.AddAnnotation(field);
doc.Close();

But it’s not working. I also read about PdfStamper. But I am creating a new pdf, not changing the existing one.So I don’t know whether I can use PdfStamper?

Thanks..

Edit:

private void CreateRadioButton(PdfWriter writer, PdfContentByte cb,Font font)
    {
        Rectangle rect;
        PdfFormField field;
        PdfFormField radiogroup = PdfFormField.CreateRadioButton(writer, true);
        radiogroup.FieldName = "language";
        RadioCheckField radio;
        int x = 20;
        for (int i = 0; i < Petrol.Length; i++)
        {
            rect = new Rectangle(440 + i * x, 692, 450 + i * x, 682);
            radio = new RadioCheckField(writer, rect, null, LANGUAGES[i]);
            radio.BorderColor = GrayColor.GRAYBLACK;
            radio.BackgroundColor = BaseColor.WHITE;
            radio.CheckType = RadioCheckField.TYPE_CIRCLE; 

            if (Petrol[i] == "F")
                radio.Checked = true;
            field = radio.RadioField;
            //Here i am setting readonly..

            field.SetFieldFlags(PdfFormField.FF_READ_ONLY);
            radiogroup.AddKid(field);                
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT,
              new Phrase(Petrol[i], font), 451 + i * x, 684, 0);
            if (i >= 1) x = 25;
        }
        writer.AddAnnotation(radiogroup);
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You’re creating a field ‘the hard way’. There’s a class named RadioCheckField that makes it much easier for you to create a field.

Please take a look at the book examples from Chapter 8. You can find C# versions of the examples here, for instance an example named Buttons.

checkbox = new RadioCheckField(writer, rect, LANGUAGES[i], "on");
checkbox.CheckType = RadioCheckField.TYPE_CHECK;
PdfFormField field = checkbox.CheckField;

Method 2

You can create your custom form template using LiveCycle and then data bind the form fields using iTextSharp like this

string randomFileName = Helpers.GetRandomFileName();
string formTemplate = Server.MapPath("~/FormTemplate.pdf");
 string formOutput = Server.MapPath(string.Format("~/downloads/Forms/Form-{0}.pdf",    randomFileName));

PdfReader reader = new PdfReader(formTemplate);
PdfStamper stamper = new PdfStamper(reader, new System.IO.FileStream(formOutput,   System.IO.FileMode.Create));
AcroFields fields = stamper.AcroFields;

// set form fields
fields.SetField("Date", DateTime.Now.ToShortDateString());
fields.SetField("FirstName", user.FirstName);
fields.SetField("LastName", user.LastName);
fields.SetField("Address1", user.Address1);
fields.SetField("Address2", user.Address2);
fields.SetField("City", user.City);
fields.SetField("State", user.State);
fields.SetField("Zip", user.Zip);
fields.SetField("Email", user.Email);
fields.SetField("Phone", user.Phone);

// set document info
System.Collections.Hashtable info = new System.Collections.Hashtable();
info["Title"] = "User Information Form";
info["Author"] = "My Client";
info["Creator"] = "My Company";
stamper.MoreInfo = info;

 // flatten form fields and close document
stamper.FormFlattening = true;
stamper.Close();

Method 3

You do not need to use a PdfStamper to create AcroForm form fields in a PDF, PdfWriter also allows you to.

Unfortunately you neither said in which way your code didn’t work nor what exact requirements you have; still some sample code might bring you on track:

Have a look at chapter 8 of iText in Action, 2nd edition; especially the sample Buttons will give you numerous hints on how to create radio buttons and check boxes. The sample ChoiceFields will show you how to create list and combo boxes.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x