iTextSharp – Creating Form fields.

Some of my readers have requested for iTextSharp checkbox, radiobutton C# samples. I basically used the Chapter8 as my reference. So, here they are.

Create RadioButtonList:

Radios[1]

public string P_InputStream = "~/pdfs/k2.pdf";
public string P_OutputStream = "~/MyPDFOutputs/ex1_1.pdf";
public static String[] LANGUAGES_gc = { "English", "German", "Spanish" };
public void CreateRadioList()
{
Document _document = new Document();
PdfReader reader = new PdfReader(Server.MapPath(P_InputStream));
using (Document document = new Document())
{
    using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath(P_OutputStream), FileMode.Create)))
    {
        document.Open();
        PdfContentByte cb = writer.DirectContent;
        Font _bf = new Font(Font.FontFamily.HELVETICA, 9);
        PdfFormField _radioGroup = PdfFormField.CreateRadioButton(writer, true);
        _radioGroup.FieldName = "language_gc";
        Rectangle _rect;
        RadioCheckField _radioG;
        PdfFormField _radioField1;

        for (int i = 0; i < LANGUAGES_gc.Length; i++)
        {
            _rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40);
            _radioG = new RadioCheckField(writer, _rect, null, LANGUAGES_gc[i]);
            _radioG.BackgroundColor = new GrayColor(0.8f);
            _radioG.BorderColor = GrayColor.BLACK;
            _radioG.CheckType = RadioCheckField.TYPE_CIRCLE;
            _radioField1 = _radioG.RadioField;
            _radioGroup.AddKid(_radioField1);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(LANGUAGES_gc[i], new Font(Font.FontFamily.HELVETICA, 18)), 70, 790 - i * 40, 0);
        }
        writer.AddAnnotation(_radioGroup);
        cb = writer.DirectContent;
        document.Close();
    }
}
}

Create one RadioButton per page:

//Add one radio per page 
public void CreateARadio()
{
    Document _document = new Document();
    PdfReader reader = new PdfReader(Server.MapPath(P_InputStream));
    using (Document document = new Document())
    {
        using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath(P_OutputStream), FileMode.Create)))
        {
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            BaseFont _bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
            PdfFormField _radioGroup = PdfFormField.CreateRadioButton(writer, true);
            _radioGroup.FieldName = "language_gc";
            Rectangle _rect = new Rectangle(40, 806, 60, 788);
            RadioCheckField _radioG;
            PdfFormField _radioField1;
                
            for (int i = 0; i < LANGUAGES_gc.Length; i++)
            {
                _radioG = new RadioCheckField(writer, _rect, null, LANGUAGES_gc[i]);
                _radioG.BackgroundColor = new GrayColor(0.8f);
                _radioField1 = _radioG.RadioField;
                _radioField1.PlaceInPage = i + 1;
                _radioGroup.AddKid(_radioField1);
            }

            writer.AddAnnotation(_radioGroup);
            for (int i = 0; i < LANGUAGES_gc.Length; i++)
            {
                cb.BeginText();
                cb.SetFontAndSize(_bf, 18);
                cb.ShowTextAligned(Element.ALIGN_LEFT, LANGUAGES_gc[i], 70, 790, 0);
                cb.EndText();
                document.NewPage();
            }
            document.Close();                           
        }
    }
}

Create CheckBoxList:

CheckList[1]

public void CreateCheckBoxList()
{
    Document _document = new Document();
    PdfReader reader = new PdfReader(Server.MapPath(P_InputStream));
    using (Document document = new Document())
    {
        using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath(P_OutputStream), FileMode.Create)))
        {
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            Rectangle _rect;
            PdfFormField _Field1;

            PdfAppearance[] onOff = new PdfAppearance[2];
            onOff[0] = cb.CreateAppearance(20, 20);
            onOff[0].Rectangle(1, 1, 18, 18);
            onOff[0].Stroke();

            onOff[1] = cb.CreateAppearance(20, 20);
            onOff[1].SetRGBColorFill(255, 128, 128);
            onOff[1].Rectangle(1, 1, 18, 18);
            onOff[1].FillStroke();
            onOff[1].MoveTo(1, 1);
            onOff[1].LineTo(19, 19);
            onOff[1].MoveTo(1, 19);
            onOff[1].LineTo(19, 1);
            onOff[1].Stroke();
            RadioCheckField _checkbox1;

            for (int i = 0; i < LANGUAGES_gc.Length; i++)
            {
                _rect = new Rectangle(180, 806 - i * 40, 200, 788 - i * 40);
                _checkbox1 = new RadioCheckField(writer, _rect, LANGUAGES_gc[i], "on");
                _Field1 = _checkbox1.CheckField;
                _Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
                _Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1]);
                writer.AddAnnotation(_Field1);

                ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(LANGUAGES_gc[i], new Font(Font.FontFamily.HELVETICA, 18)), 210, 790 - i * 40, 0);
            }
                
            cb = writer.DirectContent;
            document.Close();
        }
    }
}

Create PushButton:

Button[1]

public void AddButton()
{
    Document _document = new Document();
    PdfReader reader = new PdfReader(Server.MapPath(P_InputStream));
    using (Document document = new Document())
    {
        using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath(P_OutputStream), FileMode.Create)))
        {
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            Rectangle _rect;
            PdfFormField _Field1;

            _rect = new Rectangle(100, 806, 170, 788);
            PushbuttonField button = new PushbuttonField(writer, _rect, "Buttons");
            button.BackgroundColor = new GrayColor(0.75f);
            button.BorderColor = GrayColor.GRAYBLACK;
            button.BorderWidth = 1;
            button.BorderStyle = PdfBorderDictionary.STYLE_BEVELED;
            button.TextColor = GrayColor.GRAYBLACK;
            button.FontSize = 12;
            button.Text = "Push me";
            button.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT;
            button.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
            button.ProportionalIcon = true;
            button.IconHorizontalAdjustment = 0;
            _Field1 = button.Field;
            writer.AddAnnotation(_Field1);
            cb = writer.DirectContent;
            document.Close();
        }
    }
}

Create TextBox:

txtBox[1]

public void CreateTextBoxMethod1()
{
    Document _document = new Document();
    PdfReader reader = new PdfReader(Server.MapPath(P_InputStream));
    using (Document document = new Document())
    {
        using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath(P_OutputStream), FileMode.Create)))
        {
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            TextField _text = new TextField(writer, new Rectangle(40, 806, 160, 788), "g1");
            _text.Alignment = Element.ALIGN_CENTER;
            _text.Options = TextField.MULTILINE;
            _text.Text = "abc";
            writer.AddAnnotation(_text.GetTextField());
            cb = writer.DirectContent;
            document.Close();
        }
    }
}

 

public void CreateTextBoxMethod2()
{
    Document _document = new Document();
    PdfReader reader = new PdfReader(Server.MapPath(P_InputStream));
    using (Document document = new Document())
    {
        using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath(P_OutputStream), FileMode.Create)))
        {
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfFormField _Field1 = PdfFormField.CreateTextField(writer, false, false, 50);
            _Field1.SetWidget(new iTextSharp.text.Rectangle(40, 100, 160, 130),PdfAnnotation.APPEARANCE_NORMAL);
            _Field1.SetFieldFlags(PdfAnnotation.FLAGS_PRINT);
            _Field1.FieldName = "t1";
            writer.AddAnnotation(_Field1);
            cb = writer.DirectContent;
            document.Close();
        }
    }
}

Thanks for reading.

Advertisements
This entry was posted in External Open Source APIs and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s