hi,
Below is the code that we used to generate the document.
public static void SalesOrderMailMerger(string orderId, string customerId, string fName)
{
try
{
Aspose.Words.License license = new Aspose.Words.License();
license.SetLicense("Aspose.Total.lic");
Aspose.Cells.License Cellslicense = new Aspose.Cells.License();
Cellslicense.SetLicense("Aspose.Total.lic");
SalesOrder salesOrderObj = SalesOrder.GetSalesOrderById(orderId);
CustomerSalesInfoList customerSalesInfoList = CustomerSalesInfoList.GetCustomerSalesOrderItem(orderId, customerId);
CustomerInfo cinfo = CustomerInfo.GetCustomerInfo(customerId);
CisCompanyDetails details = GetCompanyDetails();
CisCompanyAddress primaryAddress = GetPrimaryAddress();
decimal totalCost;
decimal totalWeight;
decimal? _freight = 0;
decimal? _insurance = 0;
decimal? _packingfee = 0;
if (Convert.ToBoolean(salesOrderObj.InvoiceFreight))
_freight = salesOrderObj.Freight;
if (Convert.ToBoolean(salesOrderObj.InvoiceInsurance))
_insurance = salesOrderObj.InsuranceFee;
if (Convert.ToBoolean(salesOrderObj.InvoicePacking))
_packingfee = salesOrderObj.PackingCharges;
IFormatProvider provider = CultureInfo.CurrentUICulture;
Document doc = new Document(HttpContext.Current.Server.MapPath(Resources.FilePathResource.SAO_QuoteTemplateFilePath));
DataTable OrderItems = ConstructSalesItemsDataTable(customerSalesInfoList, out totalCost, out totalWeight);
doc.MailMerge.Execute(new string[] { "CISCOMPANY", "HADDRESS1", "HADDRESS2", "HCITY", "HSTATE", "HZIP", "HPHONE", "HFAX", "HWEBSITE", "HEMAIL",
"CREATEDBY","CREATEDBYEMAIL","SALESORDERID","CREATEDON","COMPANY","ADDRESS","CITY","STATE","ZIP","COUNTRY",
"PHONE","FAX","ATTENTION","EXPIRATION","PAYMENTTERMS","COMPANY","SHIPTO","ATTENTION","SHIPTYPE", "XLBSTOTAL","SUBTOTAL", "FREIGHT","INSURANCE","PACKINGCHARGES","GRANDTOTAL","Notes","TERMSOFSALE","TERMSOFPAYMENT" ,"FOOTERCOMPANY"},
new object[] {details.LegalCompanyName,primaryAddress.Address1,primaryAddress.Address2,primaryAddress.City,primaryAddress.State,primaryAddress.Zip,
details.Phone,details.Fax,details.Website,details.Email, salesOrderObj.CN, salesOrderObj.UserPrincipleName,salesOrderObj.Id,salesOrderObj.CreatedOnString, cinfo.Company,cinfo.Address,cinfo.City,cinfo.State,cinfo.PostalCode,cinfo.CountryAbbreviation,cinfo.Phone,cinfo.Fax,salesOrderObj.Attention,
SalesExpirationExtension.Name(salesOrderObj.ExpiresAfter),salesOrderObj.PaymentTerms,cinfo.Company, salesOrderObj.Address.Replace("","\r\n"),
salesOrderObj.Attention,salesOrderObj.ShipType, totalWeight, string.Format("{0:c}", totalCost), string.Format("{0:c}", _freight),string.Format("{0:c}", _insurance),string.Format("{0:c}", _packingfee),
string.Format("{0:c}", totalCost+_freight+_insurance+_packingfee),salesOrderObj.Notes.Replace("","\r\n"),salesOrderObj.TermsofSale,salesOrderObj.TermsofPayment,details.LegalCompanyName });
doc.MailMerge.MergeField += new MergeFieldEventHandler(HandleRemoveParagraphs);
doc.MailMerge.ExecuteWithRegions(OrderItems);
string imagePath = HttpContext.Current.Server.MapPath(Resources.FilePathResource.Cis_Logo + details.CompanyLogo);
string[] keys = { "MyImageFieldName" };
string[] values = { imagePath };
doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImage);
doc.MailMerge.Execute(keys, values);
doc.Save(HttpContext.Current.Server.MapPath(Resources.FilePathResource.SAO_MergeHTMLOutputPath) + fName + ".html", SaveFormat.Html);
doc.Save(HttpContext.Current.Server.MapPath(Resources.FilePathResource.SAO_MergePDFOutputPath) + fName + ".pdf", SaveFormat.Pdf);
}
catch (Exception ex)
{
throw ex;
}
}
In ConstructSalesItemsDataTable
public static DataTable ConstructSalesItemsDataTable(CustomerSalesInfoList customerSalesInfoList, out decimal totalCost, out decimal totalWeight)
{
DataTable dtStockItem = new DataTable("OrderItems");
totalCost = 0;
totalWeight = 0;
try
{
dtStockItem.Columns.Add("QTY");
dtStockItem.Columns.Add("QTYSHIPPED");
dtStockItem.Columns.Add("PARTNUMBER");
dtStockItem.Columns.Add("DESCRIPTION");
dtStockItem.Columns.Add("CONDITION");
dtStockItem.Columns.Add("PRICE");
dtStockItem.Columns.Add("XPRICE");
dtStockItem.Columns.Add("XLBS");
dtStockItem.Columns.Add("EXTLBS");
dtStockItem.Columns.Add("NOTE");
dtStockItem.Columns.Add("DEL");
foreach(CustomerSalesInfo customerSlaesInfo in customerSalesInfoList)
{
DataRow drStockItem = dtStockItem.NewRow();
drStockItem["QTY"] = customerSlaesInfo.Quantity;
drStockItem["QTYSHIPPED"] = customerSlaesInfo.Quantity;
drStockItem["PARTNUMBER"] = customerSlaesInfo.PartNumber;
drStockItem["DESCRIPTION"] = customerSlaesInfo.DescriptionWA;
drStockItem["CONDITION"] = customerSlaesInfo.Condition;
drStockItem["PRICE"] = customerSlaesInfo.CostPrice;
drStockItem["XPRICE"] = customerSlaesInfo.XPrice;
drStockItem["XLBS"] = customerSlaesInfo.XWeight;
drStockItem["EXTLBS"] = Convert.ToDecimal(customerSlaesInfo.Quantity * customerSlaesInfo.XWeight);
drStockItem["NOTE"] = customerSlaesInfo.ItemNotes;
drStockItem["DEL"] = customerSlaesInfo.DeliveryDays;
dtStockItem.Rows.Add(drStockItem);
totalCost = totalCost + Convert.ToDecimal(customerSlaesInfo.XPrice);
// totalWeight = totalWeight + Convert.ToDecimal(customerSlaesInfo.Quantity * customerSlaesInfo.XWeight);
totalWeight = totalWeight + Convert.ToDecimal(customerSlaesInfo.XWeight);
}
}
catch (Exception ex)
{
throw ex;
}
return dtStockItem;
}
In HandleRemoveParagraphs
private static void HandleRemoveParagraphs(object sender, MergeFieldEventArgs e)
{
if (e.FieldName == "NOTE")
{
// Get parent Table Aspose.Words.Tables.Table myTable = (e.Field.Start.ParentParagraph.ParentNode.ParentNode.ParentNode as Aspose.Words.Tables.Table);
// Remove empty peragraph after table
myTable.NextSibling.Remove();
// Remove empty paragraph before table
myTable.PreviousSibling.Remove();
Aspose.Words.Tables.Row row = (e.Field.Start.ParentNode.ParentNode.ParentNode as Aspose.Words.Tables.Row);
if (e.FieldValue.ToString().Length <= 0)
{
row.Remove();
}
}
}
In HandleRemoveParagraphs
private static void HandleMergeImage(object sender, MergeImageFieldEventArgs e)
{
if (e.FieldName == "MyImageFieldName")
{
string fileName = e.FieldValue as string;
e.ImageFileName = fileName;
}
}
Thanks.