using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;
using Telerik.Windows.Documents.Fixed.Model;
using Telerik.Windows.Documents.Fixed.Model.InteractiveForms;
using Telerik.Web.UI;
using System.Web.UI;
//using System.Web.UI.WebControls;

public partial class Default : System.Web.UI.Page
{
    public static readonly string RootDirectory = AppDomain.CurrentDomain.BaseDirectory;
    public static readonly string InputFileWithInteractiveForms = RootDirectory + "Modified.pdf";

    protected void Page_Load(object sender, EventArgs e)
    {
        PdfFormatProvider provider = new PdfFormatProvider();

        RadFixedDocument document = provider.Import(File.ReadAllBytes(InputFileWithInteractiveForms));

        CreateWebFormsControls(document);
    }

    protected void CreateWebFormsControls(RadFixedDocument document)
    {
        int textBoxCounter = 1;
        int listBoxCounter = 1;
        int radioButtonCounter = 1;
        int checkBoxCounter = 1;
        foreach (FormField field in document.AcroForm.FormFields)
        {
            switch (field.FieldType)
            {
                case FormFieldType.TextBox:
                    RadTextBox rTextBox = new RadTextBox();
                    rTextBox.ID = string.Format("RadTextBox{0}", textBoxCounter++);
                    rTextBox.Text = (field as TextBoxField).Value;
                    PlaceHolder1.Controls.Add(rTextBox);
                    break;
                case FormFieldType.ListBox:
                    RadListBox rListBox = new RadListBox();
                    rListBox.ID = string.Format("RadListBox{0}", listBoxCounter++);
                    rListBox.AutoPostBack = false;

                    var lbField = (field as ListBoxField);
                    foreach (ChoiceOption option in lbField.Options)
                    {
                        RadListBoxItem item = new RadListBoxItem();
                        item.Text = option.Value;
                        if (option == lbField.Value.First())
                        {
                            item.Selected = true;
                        }
                        rListBox.Items.Add(item);
                    }

                    rListBox.SelectedValue = lbField.Value.First().Value;

                    PlaceHolder1.Controls.Add(rListBox);
                    break;
                case FormFieldType.RadioButton:
                    RadRadioButtonList rRadioButtonList = new RadRadioButtonList();
                    rRadioButtonList.ID = string.Format("RadRadioButtonList{0}", radioButtonCounter++);
                    rRadioButtonList.AutoPostBack = false;

                    var rbField = (field as RadioButtonField);
                    foreach (RadioOption option in rbField.Options)
                    {
                        ButtonListItem buttonOption = new ButtonListItem();
                        buttonOption.Text = option.Value;
                        rRadioButtonList.Items.Add(buttonOption);
                        if (option == rbField.Value)
                        {
                            buttonOption.Selected = true;
                        }
                    }
                    PlaceHolder1.Controls.Add(rRadioButtonList);
                    break;
                case FormFieldType.CheckBox:
                    var chkbField = (field as CheckBoxField);
                    RadCheckBox rCheckBox = new RadCheckBox();
                    rCheckBox.ID = string.Format("RadCheckBox{0}", checkBoxCounter++);
                    rCheckBox.Text = chkbField.Name;
                    rCheckBox.Checked = chkbField.IsChecked;
                    rCheckBox.AutoPostBack = false;
                    PlaceHolder1.Controls.Add(rCheckBox);
                    break;
            }
            PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
        }

        RadButton saveBtn = new RadButton();
        saveBtn.ID = "SaveButton1";
        saveBtn.Text = "Save Changes";
        saveBtn.Click += SaveBtn_Click;
        PlaceHolder1.Controls.Add(saveBtn);
    }

    private void SaveBtn_Click(object sender, EventArgs e)
    {
        PdfFormatProvider provider = new PdfFormatProvider();
        RadFixedDocument document = provider.Import(File.ReadAllBytes(InputFileWithInteractiveForms));
        ModifyFormFieldValues(document);

        string modifiedFileName = "Modified.pdf";

        if (File.Exists(modifiedFileName))
        {
            File.Delete(modifiedFileName);
        }

        File.WriteAllBytes(Server.MapPath(string.Format("~/{0}", modifiedFileName)), provider.Export(document));
    }

    protected void ModifyFormFieldValues(RadFixedDocument document)
    {
        foreach (FormField field in document.AcroForm.FormFields)
        {
            switch (field.FieldType)
            {
                case FormFieldType.TextBox:

                    RadTextBox rTextBox = PlaceHolder1.FindControl("RadTextBox1") as RadTextBox;
                    ModifyTextBox((TextBoxField)field, rTextBox.Text);
                    break;
                case FormFieldType.ListBox:
                    RadListBox rListBox = PlaceHolder1.FindControl("RadListBox1") as RadListBox;

                    List<string> listboxItems = new List<string>();

                    foreach (RadListBoxItem item in rListBox.Items)
                    {
                        listboxItems.Add(item.Text);
                    }

                    ModifyListBox((ListBoxField)field, listboxItems.ToArray(), rListBox.SelectedIndex);
                    break;
                case FormFieldType.RadioButton:
                    RadRadioButtonList rRadioButtonList = PlaceHolder1.FindControl("RadRadioButtonList1") as RadRadioButtonList;
                    foreach (ButtonListItem option in rRadioButtonList.Items)
                    {
                        if (option.Selected)
                            ModifyRadioButtons((RadioButtonField)field, option.Text);
                    }
                    break;
                case FormFieldType.CheckBox:
                    switch (field.Name)
                    {
                        case "Check Box1":
                            RadCheckBox rCheckBox1 = PlaceHolder1.FindControl("RadCheckBox1") as RadCheckBox;
                            ModifyCheckBox((CheckBoxField)field, rCheckBox1.Checked.Value);
                            break;
                        case "Check Box2":
                            RadCheckBox rCheckBox2 = PlaceHolder1.FindControl("RadCheckBox2") as RadCheckBox;
                            ModifyCheckBox((CheckBoxField)field, rCheckBox2.Checked.Value);
                            break;
                        case "Check Box3":
                            RadCheckBox rCheckBox3 = PlaceHolder1.FindControl("RadCheckBox3") as RadCheckBox;
                            ModifyCheckBox((CheckBoxField)field, rCheckBox3.Checked.Value);
                            break;
                        default:
                            break;
                    }
                    break;
            }
        }
    }

    private static void ModifyTextBox(TextBoxField textBoxField, string newTextValue)
    {
        textBoxField.Value = newTextValue;
    }

    private static void ModifyListBox(ListBoxField listField, IEnumerable<string> options, int selectedIndex)
    {
        listField.Options.Clear();
        int index = 0;

        foreach (string text in options)
        {
            ChoiceOption option = new ChoiceOption(text);

            listField.Options.Add(option);

            if (index == selectedIndex)
            {
                listField.Value = new ChoiceOption[] { option };
            }

            index++;
        }
    }

    private static void ModifyRadioButtons(RadioButtonField radioField, string selectedOption)
    {
        foreach (RadioOption option in radioField.Options)
        {
            if (option.Value == selectedOption)
            {
                radioField.Value = option;
            }
        }
    }

    private static void ModifyCheckBox(CheckBoxField checkBoxField, bool IsChecked)
    {
        checkBoxField.IsChecked = IsChecked;
    }

    protected void RadButton1_Click(object sender, EventArgs e)
    {

    }
}
