Esempio n. 1
0
        public IActionResult Index()
        {
            string userName = ((string)Request.Query["username"] != null) ? (string)Request.Query["username"] : "******";

            EditorView view = new EditorView()
            {
                Username = userName
            };

            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();
                tx.Load("App_Data/document.tx", StreamType.InternalUnicodeFormat);

                int i = 100;

                foreach (EditableRegion region in tx.EditableRegions)
                {
                    region.ID = i++;
                }

                tx.Save("App_Data/document_final.tx", StreamType.InternalUnicodeFormat);
            }

            return(View(view));
        }
Esempio n. 2
0
        private byte[] processFormCheckBoxFields(byte[] document, DataRow dataRow)
        {
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();
                tx.Load(document, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

                foreach (IFormattedText textPart in tx.TextParts)
                {
                    foreach (ApplicationField appField in textPart.ApplicationFields)
                    {
                        if (appField.TypeName == "FORMCHECKBOX")
                        {
                            FormCheckBox checkbox = new FormCheckBox(appField);

                            if (dataRow.Table.Columns[checkbox.Name] != null)
                            {
                                checkbox.Checked = Convert.ToBoolean(dataRow[checkbox.Name]);
                            }
                        }
                    }
                }

                byte[] data;
                tx.Save(out data, BinaryStreamType.InternalUnicodeFormat);
                return(data);
            }
        }
        public ActionResult GenerateForm(int Id)
        {
            Customer customer;

            byte[] baCreatedFormDocument;

            // get customer by Id from database
            using (var db = new LiteDatabase(Server.MapPath("~/App_Data/customers.db")))
            {
                var col = db.GetCollection <Customer>("customers");

                col.EnsureIndex(x => x.Id);
                customer = col.Query()
                           .Where(x => x.Id == Id)
                           .SingleOrDefault();
            }

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

                // the form template
                tx.Load(Server.MapPath("~/App_data/form_template.tx"), TXTextControl.StreamType.InternalUnicodeFormat);

                // loop through all form fields of each text part
                foreach (IFormattedText textPart in tx.TextParts)
                {
                    foreach (FormField formField in textPart.FormFields)
                    {
                        // get associated value of property; check for null!
                        var propertyValue = typeof(Customer).GetProperty(formField.Name)?.GetValue(customer);

                        // cast values for specific form field types
                        switch (formField.GetType().ToString())
                        {
                        case "TXTextControl.TextFormField":     // accepts strings
                        case "TXTextControl.SelectionFormField":
                            if (propertyValue != null)
                            {
                                ((TextFormField)formField).Text = (string)propertyValue;
                            }
                            break;

                        case "TXTextControl.CheckFormField":     // accepts bool
                            if (propertyValue != null)
                            {
                                ((CheckFormField)formField).Checked = (bool)propertyValue;
                            }
                            break;

                        case "TXTextControl.DateFormField":     // accepts Win32 file dates
                            if (propertyValue != null)
                            {
                                if (Convert.ToDateTime(propertyValue.ToString()) != DateTime.MinValue)
                                {
                                    ((DateFormField)formField).Date =
                                        Convert.ToDateTime(propertyValue.ToString());
                                }
                            }
                            break;
                        }
                    }
                }

                // export form
                tx.Save(out baCreatedFormDocument, BinaryStreamType.AdobePDF);
            }

            // return the PDF as an attachment
            MemoryStream pdfStream = new MemoryStream();

            pdfStream.Write(baCreatedFormDocument, 0, baCreatedFormDocument.Length);
            pdfStream.Position = 0;

            Response.AppendHeader("content-disposition", "attachment; filename=form_" + Id + ".pdf");
            return(new FileStreamResult(pdfStream, "application/pdf"));
        }
        /********************************************************
         * ProcessCheckboxFields method
         * desc:        Sets the Unicode characters for all
         *              checkbox fields
         * parameter:   document - the document in the internal
         *              Text Control format
        ********************************************************/
        private byte[] ProcessCheckboxFields(byte[] document)
        {
            // create a new temporary ServerTextControl
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                // load the document
                tx.Create();
                tx.Load(document, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

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

                        // create a new adapter field
                        FormCheckBox checkboxField = new FormCheckBox(field);

                        // select the field to change the font name
                        textPart.Selection.Start = checkboxField.Start - 1;
                        textPart.Selection.Length = checkboxField.Length;

                        textPart.Selection.FontName = "Arial Unicode MS";

                        checkboxField.Text = checkboxField.Checked == true ? CHECKED : UNCHECKED;
                    }
                }

                tx.Save(out document, BinaryStreamType.InternalUnicodeFormat);
                return document;
            }
        }