protected void Page_Load(object sender, EventArgs e)
        {
            // handle the AJAX postback
            string eventTarget = Convert.ToString(Request.Params.Get("__EVENTTARGET"));
            string eventArgument = Convert.ToString(Request.Params.Get("__EVENTARGUMENT"));

            // if the event argument is set, toggle the checkbox
            if (eventArgument != null)
            {
                ToggleCheckBox(eventArgument);
            }

            // load a document at the first start
            if (!IsPostBack)
            {
                byte[] data;

                using (TXTextControl.ServerTextControl tx = new ServerTextControl())
                {
                    tx.Create();
                    TXTextControl.LoadSettings ls = new LoadSettings();
                    ls.ApplicationFieldFormat = ApplicationFieldFormat.MSWord;
                    tx.Load(Server.MapPath("template.docx"), StreamType.WordprocessingML, ls);
                    tx.Save(out data, BinaryStreamType.InternalUnicodeFormat);

                    data = ProcessCheckboxFields(data);
                }

                TextControl1.LoadTextAsync(data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat);
            }
        }
        public string SignDocument(Document model)
        {
            byte[] document  = Convert.FromBase64String(model.BinaryDocument);
            string signature = model.BinarySignature;

            byte[] signatureDocument = null;

            using (ServerTextControl tx = new ServerTextControl())
            {
                tx.Create();

                // use MailMerge to merge the signature template
                using (MailMerge mailMerge = new MailMerge())
                {
                    mailMerge.TextComponent = tx;
                    mailMerge.LoadTemplate(Server.MapPath("/App_Data/signature_template.tx"), FileFormat.InternalUnicodeFormat);

                    // create a new signature object
                    Signature sign = new Signature();
                    sign.Name           = model.Name;
                    sign.SignatureImage = signature;

                    // merge and save the resulting document
                    mailMerge.MergeObject(sign);
                    mailMerge.SaveDocumentToMemory(out signatureDocument, BinaryStreamType.InternalUnicodeFormat, null);
                }

                // load the original document from the editor
                tx.Load(document, BinaryStreamType.InternalUnicodeFormat);

                // find the "signature" text frame with the name "txsig"
                foreach (TextFrame frame in tx.TextFrames)
                {
                    if (frame.Name == "txsig")
                    {
                        frame.Tables.Clear();
                        frame.Selection.Start  = 0;
                        frame.Selection.Length = -1;

                        // load the merged signature template into the
                        // text frame and save the complete document
                        frame.Selection.Load(signatureDocument, BinaryStreamType.InternalUnicodeFormat);
                        tx.Save(out document, BinaryStreamType.InternalUnicodeFormat);
                        break;
                    }
                }
            }

            // finally, return the signed document
            return(Convert.ToBase64String(document));
        }
        public void SetFields()
        {
            byte[] binaryDocument = null;
            // LoadSettings must be adjusted to load the MS Word fields
            TXTextControl.LoadSettings ls = new LoadSettings();
            ls.ApplicationFieldFormat = ApplicationFieldFormat.MSWord;
            // save the document to the variable
            textControl1.Save(out binaryDocument, BinaryStreamType.InternalUnicodeFormat);
            // create a ServerTextControl instance to convert the checkboxes
            using( TXTextControl.ServerTextControl serverTextControl = new ServerTextControl() )
            {
                serverTextControl.Create();
            // load the document from the variable
                serverTextControl.Load(binaryDocument, TXTextControl.BinaryStreamType.InternalUnicodeFormat, ls);

            // loop through all fields to activate the checkbox fields
                foreach (TXTextControl.ApplicationField appfield in serverTextControl.ApplicationFields)
                {
                    if ((appfield.TypeName == "FORMCHECKBOX"))
                    {
                        // create a new adapter field
                        FormCheckBox ChkBoxField = new FormCheckBox(appfield);

                        // select the field to change the font name
                        serverTextControl.Select(ChkBoxField.Start - 1, ChkBoxField.Length);
                        serverTextControl.Selection.FontName = "Arial Unicode MS";

                        // set the text (state)
                        if (ChkBoxField.Checked == true)
                            ChkBoxField.Text = CHECKED;
                        else
                            ChkBoxField.Text = UNCHECKED;
                    }
                }
            // save the document back to the variable
                serverTextControl.Save(out binaryDocument, BinaryStreamType.InternalUnicodeFormat);
            // load the document back into the TextControl to show it to the user
                textControl1.Load(binaryDocument, BinaryStreamType.InternalUnicodeFormat, ls);
            }
        }
        /********************************************************
         * ToggleCheckBox method
         * desc:        toggles a specific checkbox field
         * parameter:   fieldname - the name of the field
        ********************************************************/
        private void ToggleCheckBox(string fieldName)
        {
            byte[] data;

            // create a new temporary ServerTextControl
            using (TXTextControl.ServerTextControl tx = new ServerTextControl())
            {
                tx.Create();

                // save current document from the editor and load
                // it into the ServerTextControl
                TextControl1.SaveText(out data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat);
                tx.Load(data, BinaryStreamType.InternalUnicodeFormat);

                // loop through all ApplicationFields in each TextPart
                foreach (IFormattedText textPart in tx.TextParts)
                {
                    foreach (ApplicationField field in textPart.ApplicationFields)
                    {
                        if ((field.TypeName != "FORMCHECKBOX"))
                            continue;

                        // if the field is a checkbox and the name matches
                        // toggle the Checked property
                        if (field.Name == fieldName)
                        {
                            // create a new adapter field
                            FormCheckBox checkboxField = new FormCheckBox(field);
                            checkboxField.Checked = !checkboxField.Checked;
                        }
                    }
                }

                tx.Save(out data, BinaryStreamType.InternalUnicodeFormat);
            }

            // process the fields and load the document back into the editor
            TextControl1.LoadTextAsync(ProcessCheckboxFields(data), TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat);
        }