// Fourth stage. For each Order ID create a detailed document that will be inserted in place of the DOCVARIABLE field. // This is the final stage and the Product.Orders template does not contain DOCVARIABLE fields. So, further processing is not required. void richServerDetailProcessor_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) { int currentProductID = GetID(e.Arguments[0].Value); if (currentProductID == -1) { return; } if (productID != currentProductID) { // Get data source that contains orders for the specified product and supplier. dataDetailedForOrders = (List <OrderDetail>)GetOrderDataFilteredbyProductAndSupplier(supplierID, currentProductID); productID = currentProductID; } if (e.VariableName == "OrderDetails") { IRichEditDocumentServer richServerOrdersTemplate = mainRichEdit.CreateDocumentServer(); RtfLoadHelper.Load("detaildetail.rtf", richServerOrdersTemplate); richServerOrdersTemplate.Options.MailMerge.DataSource = dataDetailedForOrders; IRichEditDocumentServer richServerDetailDetailProcessor = mainRichEdit.CreateDocumentServer(); MailMergeOptions options = richServerOrdersTemplate.CreateMailMergeOptions(); options.MergeMode = MergeMode.JoinTables; richServerOrdersTemplate.MailMerge(options, richServerDetailDetailProcessor); e.Value = richServerDetailDetailProcessor; e.Handled = true; } }
void OnOrderChanged() { if (Order == null || richEdit == null) { return; } using (var stream = MailMergeTemplatesHelper.GetTemplateStream("Sales Order Follow-Up.rtf")) { masterTemplate.LoadDocument(stream, DocumentFormat.Rtf); var options = masterTemplate.CreateMailMergeOptions(); options.DataSource = new List <Order>() { Order }; masterTemplate.MailMerge(options, richEdit.Document); } }
// Third stage. For each Product ID create a detailed document that will be inserted in place of the DOCVARIABLE field. void richServerMasterProcessor_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) { int currentSupplierID = GetID(e.Arguments[0].Value); if (currentSupplierID == -1) { return; } if (supplierID != currentSupplierID) { // Get data source that contains products for the specified supplier. dataDetailedForProducts = (List <Product>)GetProductsDataFilteredbySupplier(currentSupplierID); supplierID = currentSupplierID; } if (e.VariableName == "Product") { // Create a document container and load a document containing the Product header section (detail section) IRichEditDocumentServer richServerProductsTemplate = mainRichEdit.CreateDocumentServer(); RtfLoadHelper.Load("detail.rtf", richServerProductsTemplate); // Create a text engine to process a document after the mail merge. IRichEditDocumentServer richServerDetailProcessor = mainRichEdit.CreateDocumentServer(); // Specify data source for mail merge. richServerProductsTemplate.Options.MailMerge.DataSource = dataDetailedForProducts; // Specify that the resulting table should be joined with the header table. // Do not specify this option if calculated fields are not within table cells. MailMergeOptions options = richServerProductsTemplate.CreateMailMergeOptions(); options.MergeMode = MergeMode.JoinTables; // Provide a procedure for further processing. richServerDetailProcessor.CalculateDocumentVariable += new CalculateDocumentVariableEventHandler(richServerDetailProcessor_CalculateDocumentVariable); // Create a merged document using the Product template. The document will contain DOCVARIABLE fields with OrderID arguments. // The CalculateDocumentVariable event for the richServerDetail fires. richServerProductsTemplate.MailMerge(options, richServerDetailProcessor); richServerDetailProcessor.CalculateDocumentVariable -= richServerDetailProcessor_CalculateDocumentVariable; // Return the document to insert. e.Value = richServerDetailProcessor; // This setting is required for inserting e.Value into the source document. Otherwise it will be ignored. e.Handled = true; } }