// adding 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) { // removing 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; } }
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 itemtotalBeforeVAT = 0; decimal itemtotalVATAmount = 0; decimal itemtotalAmount = 0; decimal grandTotalAllItemsAmount = 0; if (grdInvoiceProducts.Rows.Count > 0) { // removing all rows in collection 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 rows 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"); // varify the found controls should not be null if (txtProductDescription != null && txtProductPrice != null && txtProductQuantity != null && ddlProductVAT != null) { // varify the found controls should not be empty if (txtProductDescription.Text.Trim() != "" && txtProductPrice.Text.Trim() != "" && txtProductQuantity.Text.Trim() != "" && ddlProductVAT.Items.Count > 0) { // actual amount price X quantity itemtotalBeforeVAT = (decimal.Parse(txtProductPrice.Text.Trim()) * decimal.Parse(txtProductQuantity.Text.Trim())); // VAT amount = (actual X VAT)/100 itemtotalVATAmount = ((itemtotalBeforeVAT * decimal.Parse(ddlProductVAT.SelectedItem.Value.Trim())) / 100); // Total amount including VAT 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(); } } // updating fix fields using simple aspose 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); // removing unused fields in template doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs | MailMergeCleanupOptions.RemoveContainingFields | MailMergeCleanupOptions.RemoveUnusedFields; // updating 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)//check file exsit or not { file.Delete(); } file = new FileInfo(imagePath); if (file.Exists)//check file exsit or not { file.Delete(); } } } } catch (Exception exc) { lblMessage.Text = exc.Message; Response.Clear(); Response.Flush(); } }