Exemplo n.º 1
0
        // 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;
            }
        }
    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);
    }
Exemplo n.º 3
0
 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;
 }
Exemplo n.º 4
0
        // 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;
            }
        }