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 InsertSubTextPart(string Name, string BinaryDocument, bool Protected) { byte[] data = null; // create a temporary ServerTextControl to create the // SubTextPart using (ServerTextControl tx = new ServerTextControl()) { tx.Create(); // load the Selection from the Web.TextControl tx.Load(Convert.FromBase64String(BinaryDocument), BinaryStreamType.InternalUnicodeFormat); tx.SelectAll(); int iTextLength = tx.Selection.Length; // create a new SubTextPart over the complete text SubTextPart part = new SubTextPart("txmb_" + Name, Convert.ToInt32(Protected), 1, iTextLength); tx.SubTextParts.Add(part); // save the complete document tx.SelectAll(); tx.Selection.Save(out data, BinaryStreamType.InternalUnicodeFormat); } // return the Selection as a Base64 encoded string return(Convert.ToBase64String(data)); }
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)); }
private void MergeTemplate(string templateName, string JsonData) { // creating a new ServerTextControl and MailMerge class using (ServerTextControl tx = new ServerTextControl()) { tx.Create(); MailMerge mailMerge = new MailMerge(); mailMerge.TextComponent = tx; // load the template mailMerge.LoadTemplate(Server.MapPath("templates/" + templateName), FileFormat.InternalUnicodeFormat); // create a new DataSet object DataSet ds = new DataSet(); XmlDocument doc; // convert the JSON string to an XML document // object using Newtonsoft.Json try { doc = JsonConvert.DeserializeXmlNode(JsonData); lblError.Visible = false; } catch (Exception exc) { lblError.Visible = true; lblError.Text = exc.Message; DocumentViewer1.Visible = false; return; } // convert the XML to a DataSet XmlNodeReader reader = new XmlNodeReader(doc); ds.ReadXml(reader, XmlReadMode.Auto); // merge the template with the DataSet mailMerge.Merge(ds.Tables[0]); // load the resulting document into the DocumentViewer byte[] data; mailMerge.SaveDocumentToMemory(out data, BinaryStreamType.InternalUnicodeFormat, null); DocumentViewer1.LoadDocumentFromMemory(data, FileFormat.InternalUnicodeFormat); } }
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); }