Ejemplo n.º 1
0
        // Create New Invoice
        public void InvoiceCreate(string OrderID)
        {
            MemoryStream rtfStream = new MemoryStream();
            MemoryStream pdfStream = new MemoryStream();

            RichEditDocumentServer docServer = new RichEditDocumentServer();

            docServer.Document.LoadDocument(Server.MapPath("~/App_Data/WorkDirectory/Invoice.rtf"), DocumentFormat.Rtf);

            // Rtf file
            Document rtf = docServer.Document;

            // Customer's Data
            var arrayCustomer = SelectOrder(OrderID);

            var arrayOrderDetail = SelectOrderDetails(OrderID);

            // Replace Header String
            SubDocument header = docServer.Document.Sections[0].BeginUpdateHeader();

            header.ReplaceAll("#invoiceid", arrayCustomer[7].ToString(), SearchOptions.WholeWord);

            // Replace Fields
            rtf.ReplaceAll("#orderid", arrayCustomer[0].ToString(), SearchOptions.WholeWord);
            rtf.ReplaceAll("#customerid", arrayCustomer[1].ToString(), SearchOptions.WholeWord);
            rtf.ReplaceAll("#date", Convert.ToDateTime(arrayCustomer[2]).ToShortDateString(), SearchOptions.WholeWord);
            rtf.ReplaceAll("#customer", arrayCustomer[3].ToString(), SearchOptions.WholeWord);
            rtf.ReplaceAll("#address", arrayCustomer[4].ToString().Trim(), SearchOptions.WholeWord);
            rtf.ReplaceAll("#email", arrayCustomer[5].ToString().Trim(), SearchOptions.WholeWord);
            rtf.ReplaceAll("#phone", arrayCustomer[6].ToString(), SearchOptions.WholeWord);

            docServer.SaveDocument(rtfStream, DocumentFormat.Rtf);
            docServer.ExportToPdf(pdfStream);

            using (SqlConnection con = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection  = con;

                    cmd.CommandText = @"insert into Invoices (OrderID, InvoiceRtf, InvoicePdf) 
                                        values (@OrderID, @InvoiceRtf, @InvoicePdf)";

                    cmd.Parameters.AddWithValue("@OrderID", OrderID);
                    cmd.Parameters.AddWithValue("@InvoiceRtf", SqlDbType.VarBinary).Value = rtfStream.ToArray();
                    cmd.Parameters.AddWithValue("@InvoicePdf", SqlDbType.VarBinary).Value = pdfStream.ToArray();

                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }