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); } }
/// <summary> /// Gathers merge blocks in current document. /// </summary> /// <returns>Merge blocks in current document.</returns> public static List <MergeBlock> GetMergeBlocksFlattened(ServerTextControl textControl) { ValidateMergeBlockNesting(textControl); var result = new List <MergeBlock>(); // Get a list of all block start and end markers ordered by their start position: var blockMarkers = GetBlockMarkersOrdered(textControl); // Gather blocks: var startMarkers = new Stack <TXTextControl.DocumentTarget>(); foreach (var marker in blockMarkers) { if (marker.TargetName.StartsWith(MergeBlock.BlockStartPrefix, StringComparison.OrdinalIgnoreCase)) { // Start marker: startMarkers.Push(marker); } else if (marker.TargetName.StartsWith(MergeBlock.BlockEndPrefix, StringComparison.OrdinalIgnoreCase)) { // End marker: TXTextControl.DocumentTarget startMarker = startMarkers.Pop(); // Get currently open start marker var blockNew = new MergeBlock(startMarker, marker); result.Add(blockNew); } } result.Sort(MergeBlock.SortPosition); return(result); }
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)); }
private void GetFields(ServerTextControl textControl) { var fields = new List <MergeField>(); foreach (ApplicationField appFld in textControl.ApplicationFields) { int nFldEnd = appFld.Start + appFld.Length; if ((appFld.Start >= StartMarker.Start) && (nFldEnd <= EndMarker.Start)) { MergeField adap = MakeAdapterFrom(appFld); if (adap != null) { fields.Add(adap); } } } foreach (MergeField fld in fields) { if (!IsInside(fld.ApplicationField, _children)) { _fields.Add(fld); } } }
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 InitializeComponent() { this.components = new System.ComponentModel.Container(); this.documentController1 = new TXTextControl.DocumentServer.DocumentController(this.components); this.serverTextControl1 = new TXTextControl.ServerTextControl(); // // documentController1 // this.documentController1.TextComponent = this.serverTextControl1; // // serverTextControl1 // this.serverTextControl1.SpellChecker = null; }
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); } }
/// <summary> /// Constructor. /// </summary> public MergeBlock(List <DocumentTarget> blockMarkers, ref int i, ServerTextControl textControl, MergeBlock parent) { if (!blockMarkers[i].TargetName.StartsWith(BlockStartPrefix)) { throw new Exception( "Invalid merge block structure. Block start marker doesn't start with the block start prefix (Marker name: \"" + blockMarkers[i].TargetName + "\")"); } _fields = new List <MergeField>(); _children = new List <MergeBlock>(); _parent = parent; StartMarker = blockMarkers[i++]; // Gather child blocks if any while ((i < blockMarkers.Count) && blockMarkers[i].TargetName.StartsWith(BlockStartPrefix)) { _children.Add(new MergeBlock(blockMarkers, ref i, textControl, this)); ++i; } if (i >= blockMarkers.Count) { // Not possible throw new Exception("Invalid merge block structure."); } // End marker. string endMarkerName = blockMarkers[i].TargetName.Substring(BlockEndPrefix.Length); if (endMarkerName != Name) { throw new Exception( "Invalid merge block structure. End marker name is not the same as the start marker name. (End marker name: \"" + blockMarkers[i].TargetName + "\")"); } EndMarker = blockMarkers[i]; // Get fields inside this block but not in child blocks GetFields(textControl); }
/// <summary> /// Validates merge block nesting /// </summary> public static void ValidateMergeBlockNesting(ServerTextControl textControl) { // Get a list of all block start and end markers ordered by their start position: var blockMarkers = GetBlockMarkersOrdered(textControl); var startMarkers = new Stack <TXTextControl.DocumentTarget>(); foreach (var marker in blockMarkers) { if (marker.TargetName.StartsWith(MergeBlock.BlockStartPrefix, StringComparison.OrdinalIgnoreCase)) { // Start marker: startMarkers.Push(marker); } else if (marker.TargetName.StartsWith(MergeBlock.BlockEndPrefix, StringComparison.OrdinalIgnoreCase)) { // End marker: var endMarkerName = marker.TargetName.Substring(MergeBlock.BlockEndPrefix.Length); // Get block name TXTextControl.DocumentTarget startMarker = (startMarkers.Count > 0) ? startMarkers.Pop() : null; // Get currently open start marker var startMarkerName = (startMarker != null) ? startMarker.TargetName.Substring(MergeBlock.BlockStartPrefix.Length) : string.Empty; if (endMarkerName.ToLower() != startMarkerName.ToLower()) { if (startMarkerName != string.Empty) { throw new Exception( "Block nesting invalid somewhere after block start marker “" + MergeBlock.BlockStartPrefix + startMarkerName + "”."); } else { throw new Exception( "Invalid block end marker “" + MergeBlock.BlockEndPrefix + endMarkerName + "”."); } } } } }
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); }
/// <summary> /// Returns a list of all block start and end markers ordered by their start position. /// </summary> /// <returns>Sorted list of block markers.</returns> public static List <TXTextControl.DocumentTarget> GetBlockMarkersOrdered(ServerTextControl textControl) { var targets = new List <TXTextControl.DocumentTarget>(); foreach (TXTextControl.DocumentTarget tgt in textControl.DocumentTargets) { targets.Add(tgt); } var blockMarkers = new List <TXTextControl.DocumentTarget>(); foreach (TXTextControl.DocumentTarget tgt in targets) { if (tgt.TargetName.StartsWith(MergeBlock.BlockStartPrefix, StringComparison.OrdinalIgnoreCase) || tgt.TargetName.StartsWith(MergeBlock.BlockEndPrefix, StringComparison.OrdinalIgnoreCase)) { blockMarkers.Add(tgt); } } blockMarkers.Sort(new DocumentTargetComparer()); return(blockMarkers); }
/// <summary> /// Makes merge blocks from a valid and sorted list of block markers. (Check /// with ValidateMergeBlockNesting beforehand and get list with GetBlockMarkersOrdered) /// </summary> public static List <MergeBlock> GetMergeBlocks(List <DocumentTarget> blockMarkers, ServerTextControl textControl) { var result = new List <MergeBlock>(); if (blockMarkers.Count == 0) { return(result); } if (!blockMarkers[0].TargetName.StartsWith(BlockStartPrefix)) { throw new Exception("MergeBlock.GetMergeBlocks: invalid merge block structure."); } for (int i = 0; i < blockMarkers.Count; ++i) { result.Add(new MergeBlock(blockMarkers, ref i, textControl, null)); } return(result); }