翻譯|使用教程|編輯:況魚杰|2020-10-09 14:19:52.380|閱讀 522 次
概述:MailMerge是一個強大的,可擴展的框架,允許您將自定義邏輯注入合并過程。TXTextControl.DocumentServer.MailMerge.FieldMerged事件可用于處理結果,但也可以訪問TXTextControl.TableCell類的周圍實例。這使您可以根據特定的過濾器指令來實現諸如條件表單元格顏色之類的功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
TX Text Control Server for ASP.NET (incl. WPF)是一個企業級的服務器端文字處理控件。它為用于ASP.NET服務器環境提供一個完全可編程的文字處理引擎,并且包含一個WPF客戶端版本。
	點擊下載TX Text Control Server for ASP.NET (incl. WPF)最新試用版
 
MailMerge是一個強大的,可擴展的框架,允許您將自定義邏輯注入合并過程。
TXTextControl.DocumentServer.MailMerge.FieldMerged事件可用于處理結果,但也可以訪問TXTextControl.TableCell類的周圍實例。這使您可以根據特定的過濾器指令來實現諸如條件表單元格顏色之類的功能。
在此示例中,如果數量大于10,則表單元格“數量”應突出顯示為紅色,否則應為綠色。
	
 
	該示例顯示了兩個有趣的方面:
 
該示例實現HTML表單元素以設置表單元格的條件。如果輸入位置在表格單元格內部,則以下代碼用于通過將對象序列化為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));
            });
        });
    })
});
最后,當單擊合并時,將保存文檔并將其發送到后端控制器:
	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方法接受文檔并調用MergeJsonData方法以將數據合并到給定的模板中:
	[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事件以處理自定義條件。 如果字段在表格單元格內,則將TableCell.Name屬性反序列化為CellFilterInstructions對象,以便根據定義的指令應用表格單元格的顏色。
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;
    }
}
關注慧聚IT微信公眾號 ???,了解產品的最新動態及最新資訊。
	 
 
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自: