翻譯|使用教程|編輯:鮑佳佳|2021-08-26 11:07:32.817|閱讀 141 次
概述:TX Text Control Server for ASP.NET (incl. WPF)是企業(yè)級的服務(wù)器端文字處理控件。它為ASP.NET服務(wù)器環(huán)境提供了一個完整的文本處理引擎,并且包含一個WPF客戶端版本。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
TX Text Control Server for ASP.NET (incl. WPF)是企業(yè)級的服務(wù)器端文字處理控件。它為ASP.NET服務(wù)器環(huán)境提供了一個完整的文本處理引擎,并且包含一個WPF客戶端版本。
點擊下載TX Text Control Server for ASP.NET (incl. WPF)最新試用版
MailMerge 是一個強大的、可擴展的框架,允許您將自定義邏輯注入合并過程。
TXTextControl.DocumentServer.MailMerge.FieldMerged事件可用于處理結(jié)果,但也可以訪問TXTextControl.TableCell類的已實例。這使您根據(jù)特定的過濾器指令來可以實現(xiàn)條件表單元格顏色之類的功能。
在此示例中,如果數(shù)量大于 10,則表單元格“數(shù)量”應(yīng)突出顯示為紅色,否則應(yīng)為紅色。
該示例顯示了兩個有趣的方面:
該示例實現(xiàn)HTML表單元素以設(shè)置表單元格的條件。如果輸入位置在表格單元格內(nèi)部,則以下代碼用于通過將對象序列化為Json字符串將條件存儲在表格單元格中:
// stores the selected conditions in the cell name function setTableCellConditions(empty = false) { TXTextControl.tables.getItem(function (table) { if (table === null) return; // no table // create a cellFilterInstructions object var cellFilterInstructions = { compareValue: document.getElementById("compareValue").value, operator: document.getElementById("operator").value, trueColor: document.getElementById("trueColor").value, falseColor: document.getElementById("falseColor").value } table.cells.getItemAtInputPosition(function (cell) { if (cell === null) return; // no cell if (empty === true) cell.setName(""); // delete instructions else // sel instructions to cell name cell.setName(JSON.stringify(cellFilterInstructions)); }); }) }
如果將輸入位置更改為另一個單元格,則會更新表單元素以反映該單元格的條件:
// check cell status on input position changes TXTextControl.addEventListener("inputPositionChanged", function () { TXTextControl.tables.getItem(function (table) { // table at input pos? if (table === null) { // return if no table available EnableFormElements( ["operator", "compareValue", "trueColor", "falseColor", "enableCondition"], false); return; } // enable form elements EnableFormElements( ["operator", "compareValue", "trueColor", "falseColor", "enableCondition"], true); table.cells.getItemAtInputPosition(function (cell) { // cell at input pos if (cell == null) { // return if more cells are selected enableCellConditions(false); document.getElementById("enableCondition").setAttribute( "disabled", "disabled"); return; } // check the cell name that stores the conditions cell.getName(function (cellName) { if (cellName === "") { enableCellConditions(false); return; } updateSettings(JSON.parse(cellName)); }); }); }) });
最后,當(dāng)單擊合并時,將保存文檔并將其發(fā)送到后端控制器:
function mergeDocument() { TXTextControl.saveDocument(TXTextControl.streamType.InternalUnicodeFormat, function (e) { var serviceURL = "/Home/MergeDocument"; $.ajax({ type: "POST", url: serviceURL, contentType: 'application/json', data: JSON.stringify({ Document: e.data, }), success: successFunc, error: errorFunc }); function successFunc(data, status) { TXTextControl.loadDocument(TXTextControl.streamType.InternalUnicodeFormat, data); } function errorFunc(error) { console.log(error); } }); }
在控制器中,HttpPost方法接受文檔并調(diào)用MergeJsonData方法以將數(shù)據(jù)合并到給定的模板中:
[HttpPost] public string MergeDocument(string Document) { using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { tx.Create(); tx.Load(Convert.FromBase64String(Document), TXTextControl.BinaryStreamType.InternalUnicodeFormat); using (TXTextControl.DocumentServer.MailMerge mailMerge = new TXTextControl.DocumentServer.MailMerge()) { mailMerge.TextComponent = tx; mailMerge.FieldMerged += MailMerge_FieldMerged; string data = System.IO.File.ReadAllText(Server.MapPath("~/App_Data/data.json")); mailMerge.MergeJsonData(data, false); } byte[] results; tx.Save(out results, TXTextControl.BinaryStreamType.InternalUnicodeFormat); return Convert.ToBase64String(results); } }
附加了FieldMerged事件以處理自定義條件。 如果字段在表格單元格內(nèi),則將TableCell.Name屬性反序列化為CellFilterInstructions對象,以便根據(jù)定義的指令應(yīng)用表格單元格的顏色。
private void MailMerge_FieldMerged(object sender, TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs e) { // custom field handling if (e.TableCell == null) return; // if TableCell.Name has instructions, create a CellFilterInstructions object // and evaluate the instructions and set the table cell color if (e.TableCell.Name != "") { CellFilterInstructions instructions = (CellFilterInstructions)JsonConvert.DeserializeObject( e.TableCell.Name, typeof(CellFilterInstructions)); // retrieve the color Color? color = instructions.GetColor(e.MailMergeFieldAdapter.ApplicationField.Text); // apply the color if (color != null) e.TableCell.CellFormat.BackColor = (Color)color; } }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: