Beispiel #1
0
        // Add rows in invoice products gridview.
        private void PopulateProductsGrid(int addRows)
        {
            try
            {
                // Get dataset and datable object from cache.
                DataSet data = QuoteGenerator.GetDataSetForGridView(Session);
                if (data != null)
                {
                    // Remove all rows in collection.
                    data.Tables[0].Rows.Clear();

                    for (int indx = 1; indx <= addRows; indx++)
                    {
                        // Add the temp data row to the tables for each row.
                        data.Tables[0].Rows.Add(indx, "", 0.0, 1, 0.0, 0.0, 0.0, 0.0);
                    }

                    grdInvoiceProducts.DataSource = data;
                    grdInvoiceProducts.DataBind();
                }
            }
            catch (Exception exc)
            {
                lblMessage.Text = exc.Message;
            }
        }
Beispiel #2
0
        // Products gridview row data bound event to populate VAT dropdown list.
        protected void grdInvoiceProducts_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    // Getting object of VAT dropdown list for each row.
                    DropDownList ddlProductVAT = (DropDownList)e.Row.FindControl("ddlProductVAT");

                    if (ddlProductVAT != null)
                    {
                        QuoteGenerator.PopulateVATDropdownList(ref ddlProductVAT, this.Session);
                    }
                }
            }
            catch (Exception exc)
            {
                lblMessage.Text = exc.Message;
            }
        }
Beispiel #3
0
        private void MergeWithWordTemplate(string templatePath, string outputPath, string imagePath)
        {
            try
            {
                lblMessage.Text = "";

                // Check for license and apply if exists.
                string licenseFile = Server.MapPath("~/App_Data/Aspose.Words.lic");
                if (File.Exists(licenseFile))
                {
                    License license = new License();
                    license.SetLicense(licenseFile);
                }

                Document doc = QuoteGenerator.GetUnmergedTemplateObject(templatePath + "MailMerge_Template.doc", Session);
                if (doc != null)
                {
                    // Fill the fields in the document with user data.
                    DataSet data = QuoteGenerator.GetDataSetForGridView(Session);
                    if (data != null)
                    {
                        decimal grandTotalAllItemsAmount = 0;

                        if (grdInvoiceProducts.Rows.Count > 0)
                        {
                            data.Tables[0].Rows.Clear();

                            System.Web.UI.WebControls.TextBox txtProductDescription;
                            System.Web.UI.WebControls.TextBox txtProductPrice;
                            System.Web.UI.WebControls.TextBox txtProductQuantity;
                            DropDownList ddlProductVAT;

                            foreach (GridViewRow gr in grdInvoiceProducts.Rows)
                            {
                                // Find control in each gridview row.
                                txtProductDescription = (System.Web.UI.WebControls.TextBox)gr.FindControl("txtProductDescription");
                                txtProductPrice       = (System.Web.UI.WebControls.TextBox)gr.FindControl("txtProductPrice");
                                txtProductQuantity    = (System.Web.UI.WebControls.TextBox)gr.FindControl("txtProductQuantity");
                                ddlProductVAT         = (DropDownList)gr.FindControl("ddlProductVAT");

                                // Verify the found controls should not be null.
                                if (txtProductDescription != null && txtProductPrice != null && txtProductQuantity != null && ddlProductVAT != null)
                                {
                                    // Verify the found controls should not be empty.
                                    if (txtProductDescription.Text.Trim() != "" && txtProductPrice.Text.Trim() != "" && txtProductQuantity.Text.Trim() != "" && ddlProductVAT.Items.Count > 0)
                                    {
                                        // Actual amount = price * quantity.
                                        decimal itemTotalBeforeVat = decimal.Parse(txtProductPrice.Text.Trim()) * decimal.Parse(txtProductQuantity.Text.Trim());

                                        // VAT amount = (actual amount * VAT) / 100 .
                                        decimal itemTotalVatAmount = (itemTotalBeforeVat * decimal.Parse(ddlProductVAT.SelectedItem.Value.Trim())) / 100;

                                        // Total amount including VAT.
                                        decimal itemTotalAmount = itemTotalBeforeVat + itemTotalVatAmount;
                                        grandTotalAllItemsAmount += itemTotalAmount;

                                        // Add the temp data row to the tables for each row.
                                        data.Tables[0].Rows.Add(gr.Cells[0].Text, txtProductDescription.Text.Trim(), decimal.Parse(txtProductPrice.Text.Trim()), decimal.Parse(txtProductQuantity.Text.Trim()), itemTotalBeforeVat, decimal.Parse(ddlProductVAT.SelectedItem.Value), itemTotalVatAmount, itemTotalAmount);
                                    }
                                }
                            }
                        }

                        if (imagePath != "")
                        {
                            Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
                            if (shape != null)
                            {
                                shape.ImageData.ImageBytes = File.ReadAllBytes(imagePath);
                            }
                        }
                        else
                        {
                            Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
                            if (shape != null)
                            {
                                shape.Remove();
                            }
                        }

                        // Update fields using an Aspose.Words Mail Merge.
                        doc.MailMerge.Execute(
                            new string[] { "CompanyName", "CompanyAddress", "CompanyZipState", "CompanyCountry", "CustomerName", "CustomerAddress", "CustomerZipState", "CustomerCountry", "InvoiceTotalAmount", "DocCaption", "DocDate", "DocNo", "DocDescription", "DocTC" },
                            new object[] { txtCompanyName.Text.Trim(), txtCompanyAddress.Text.Trim(), txtCompanyStateZip.Text.Trim(), txtCompanyCountry.Text.Trim(), txtCustomerName.Text.Trim(), txtCustomerAddress.Text.Trim(), txtCustomerStateZip.Text.Trim(), txtCustomerCountry.Text.Trim(), grandTotalAllItemsAmount, txtDocCaption.Text.Trim(), txtDocDate.Text.Trim(), txtDocNo.Text.Trim(), txtDescription.Text.Trim(), txtTC.Text.Trim() });

                        doc.MailMerge.ExecuteWithRegions(data);

                        // Remove unused fields in template.
                        doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs | MailMergeCleanupOptions.RemoveContainingFields | MailMergeCleanupOptions.RemoveUnusedFields;

                        // Updated document layout, to be cached and re-use.
                        doc.UpdatePageLayout();

                        // Saves the document to disk.
                        string fname = System.Guid.NewGuid().ToString() + "." + QuoteGenerator.GetSaveFormat(ExportTypeDropDown.SelectedValue);
                        doc.Save(outputPath + fname);
                        Response.Clear();
                        Response.Buffer = true;
                        Response.AddHeader("content-disposition", "attachment;filename=ExportedFile_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + "_" + DateTime.Now.Millisecond.ToString() + "." + QuoteGenerator.GetSaveFormat(ExportTypeDropDown.SelectedValue));
                        Response.Charset     = "";
                        Response.ContentType = "application/pdf";
                        Response.Cache.SetCacheability(HttpCacheability.NoCache);

                        Response.ContentType = "Application/" + QuoteGenerator.GetSaveFormat(ExportTypeDropDown.SelectedValue);

                        // Get the physical path to the file.
                        string FilePath = MapPath(GetDataDir_OutputDocs() + fname);

                        // Write the file directly to the HTTP content output stream.
                        Response.WriteFile(FilePath);
                        Response.Flush();

                        // Delete file as its already in stream and available for user to download/save/view.
                        FileInfo file = new FileInfo(FilePath);
                        if (file.Exists)
                        {
                            file.Delete();
                        }
                        file = new FileInfo(imagePath);
                        if (file.Exists)
                        {
                            file.Delete();
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                lblMessage.Text = exc.Message;
                Response.Clear();
                Response.Flush();
            }
        }