protected void Page_Load(object sender, EventArgs e) { if (!IsCallback && Request["Template"] != null && Request["ID"] != null) { var TemplateDoc = Request["Template"].ToString(); var ID = Request["ID"].ToString(); RichEditDocumentServer documentServer = new RichEditDocumentServer(); documentServer.LoadDocument(Path.Combine(DirectoryManagmentUtils.CurrentDataDirectory, TemplateDoc)); IEnumerable <CompanyVM> items = GetData(ID); string FileName = "RegisterForm_"; FileName += items.FirstOrDefault()?.Id; FileName += @".docx"; documentServer.Options.MailMerge.DataSource = items; using (MemoryStream stream = new MemoryStream()) { documentServer.MailMerge(stream, DocumentFormat.OpenXml); stream.Position = 0; DocumentManager.CloseDocument(documentId); SaveStreamToFile(stream, Path.Combine(DirectoryManagmentUtils.CurrentDataDirectory, FileName)); stream.Position = 0; SendFiletoClientBrowser(Path.Combine(DirectoryManagmentUtils.CurrentDataDirectory, FileName)); DemoRichEdit.Open(documentId, DocumentFormat.OpenXml, () => { return(stream); }); } } }
// 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 richServerDetail_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. // The data source is obtained from the data already filtered by supplier. dataDetailedForOrders = GetOrderDataFilteredbyProductAndSupplier(currentProductID); productID = currentProductID; } if (e.VariableName == "OrderDetails") { RichEditDocumentServer richServerDetailDetail = new RichEditDocumentServer(); MailMergeOptions options = ordersRichEdit.CreateMailMergeOptions(); options.DataSource = dataDetailedForOrders; options.MergeMode = MergeMode.JoinTables; ordersRichEdit.MailMerge(options, richServerDetailDetail); e.Value = richServerDetailDetail; e.Handled = true; } }
// Start the process by merging main template into the document contained within the resultRichEdit server. public void Start() { // Since the main template contains no merge fields requiring no merge data, provide a fake data source. // Otherwise mailmerge will not start. mainRichEdit.Options.MailMerge.DataSource = fakeDataSource; // Trigger the multistage process. After the first mailmerge the CalculateDocumentVariable event //for the resultRichEdit server fires. mainRichEdit.MailMerge(resultRichEdit.Document); resultRichEdit.SaveDocument("result.docx", DocumentFormat.OpenXml); }
private Stream ExecuteMerge(string templateName, DocumentFormat documentFormat) { Stream result = new MemoryStream(); MailMergeOptions mailMergeOptions = documentServer.CreateMailMergeOptions(); if (templateName == "preview1") { documentServer.LoadDocument(Page.MapPath("~/App_Data/InvoicesDetail.rtf")); List <Invoice> invoices = new List <Invoice>(10); invoices.Add(new Invoice(0, "Invoice1", 10.0m)); invoices.Add(new Invoice(1, "Invoice2", 15.0m)); invoices.Add(new Invoice(2, "Invoice3", 20.0m)); mailMergeOptions.DataSource = invoices; } else if (templateName == "preview2") { documentServer.LoadDocument(Page.MapPath("~/App_Data/SamplesDetail.rtf")); mailMergeOptions.DataSource = ManualDataSet.CreateData().Tables[0]; } else if (templateName == "all") { Stream part1 = ExecuteMerge("preview1", documentFormat); Stream part2 = ExecuteMerge("preview2", documentFormat); part1.Seek(0, SeekOrigin.Begin); part2.Seek(0, SeekOrigin.Begin); documentServer.LoadDocument(part1, documentFormat); documentServer.Document.AppendDocumentContent(part2, documentFormat); documentServer.SaveDocument(result, documentFormat); return(result); } documentServer.Options.MailMerge.ViewMergedData = true; documentServer.Options.Export.Html.EmbedImages = true; mailMergeOptions.MergeMode = MergeMode.JoinTables; documentServer.MailMerge(mailMergeOptions, result, documentFormat); return(result); }
// Second stage. For each Supplier ID create a detailed document that will be inserted in place of the DOCVARIABLE field. void resultRichEdit_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) { if (e.VariableName == "Supplier") { // Create a text engine to process a document after the mail merge. RichEditDocumentServer richServerMaster = new RichEditDocumentServer(); // Provide a procedure for further processing richServerMaster.CalculateDocumentVariable += richServerMaster_CalculateDocumentVariable; // Create a merged document using the Supplier template. The document will contain DOCVARIABLE fields with ProductID arguments. // The CalculateDocumentVariable event for the richServerMaster fires. suppllierRichEdit.MailMerge(richServerMaster); richServerMaster.CalculateDocumentVariable -= richServerMaster_CalculateDocumentVariable; // Return the document to insert. e.Value = richServerMaster; // Required to use e.Value. Otherwise it will be ignored. e.Handled = true; } }
void OnCalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) { if (Order == null || e.VariableName != "Products") { return; } using (var server = new RichEditDocumentServer()) { using (var stream = MailMergeTemplatesHelper.GetTemplateStream("Sales Order Follow-Up Detail.rtf")) { server.LoadDocument(stream, DocumentFormat.Rtf); var options = server.CreateMailMergeOptions(); options.DataSource = Order.OrderItems; server.MailMerge(options, detailTemplate.Document); } } e.Value = detailTemplate; e.KeepLastParagraph = true; e.Handled = true; }
protected void Page_Load(object sender, EventArgs e) { if (!IsCallback) { RichEditDocumentServer documentServer = new RichEditDocumentServer(); documentServer.LoadDocument(Path.Combine(DirectoryManagmentUtils.CurrentDataDirectory, @"MailMergeTemplate.docx")); documentServer.Options.MailMerge.DataSource = GetData(); using (MemoryStream stream = new MemoryStream()) { documentServer.MailMerge(stream, DocumentFormat.OpenXml); stream.Position = 0; DocumentManager.CloseDocument(documentId); DemoRichEdit.Open(documentId, DocumentFormat.OpenXml, () => { return(stream); }); } } }
// Third stage. For each Product ID create a detailed document that will be inserted in place of the DOCVARIABLE field. void richServerMaster_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 = GetProductsDataFilteredbySupplier(currentSupplierID); supplierID = currentSupplierID; } if (e.VariableName == "Product") { // Create a text engine to process a document after the mail merge. RichEditDocumentServer richServerDetail = new RichEditDocumentServer(); // Specify data source for mail merge. MailMergeOptions options = productRichEdit.CreateMailMergeOptions(); options.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. options.MergeMode = MergeMode.JoinTables; // Provide a procedure for further processing. richServerDetail.CalculateDocumentVariable += richServerDetail_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. productRichEdit.MailMerge(options, richServerDetail); richServerDetail.CalculateDocumentVariable -= richServerDetail_CalculateDocumentVariable; // Return the document to insert. e.Value = richServerDetail; // This setting is required for inserting e.Value into the source document. Otherwise it will be ignored. e.Handled = true; } }