{
try
{
object missing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc";
object start = 0; object end = 0; object oNull = System.Reflection.Missing.Value;
ApplicationClass WordApp = new ApplicationClass();
Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
IEnumerable
if (quoteDetails.Count() > 0)
{
int itemCount = quoteDetails.Count();
int itemPerPage = 1;
if (itemCount > 5)
{
itemPerPage = itemCount / 5;
}
else
{
itemPerPage = itemCount;
}
///Logic to add header table in pdf document
Microsoft.Office.Interop.Word.Table headerTable, headerTotable, headerToAddress;
Microsoft.Office.Interop.Word.Range header = aDoc.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerTable = aDoc.Tables.Add(header, 1, 3, ref missing, ref missing);
headerTable.LeftPadding = WordApp.InchesToPoints((float)0.0);
headerTable.Range.Font.Size = 7;
headerTable.Range.Font.Bold = 3;
headerTable.Cell(1, 1).Range.InlineShapes.AddPicture((System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) + @"\folder\logo.png", ref missing, ref missing, ref missing);
headerTable.Cell(1, 2).Range.Font.Bold = 3;
headerTable.Cell(1, 2).WordWrap = true;
headerTable.Cell(1, 2).Width = WordApp.InchesToPoints((float)2.7);
headerTable.Cell(1, 2).Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly;
headerTable.Cell(1, 2).Range.ParagraphFormat.LineSpacing = 8.0f;
headerTable.Cell(1, 2).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
headerTable.Cell(1, 2).Range.ParagraphFormat.LineUnitAfter = 1.0f;
headerTable.Cell(1, 2).Range.Text = "H E A D E R S Y S T E M " +
"P.O Box 004,,CANADA 4506-60150 " + " PHONE:(937)746-6841" + "," + "(000)457-9687 " +
" "
+
" O R D E R C O N F I R M A T I ON";
headerTable.Cell(1, 3).Width = WordApp.InchesToPoints((float)1.5);
headerTable.Cell(1, 3).WordWrap = true;
headerTable.Cell(1, 3).Range.Font.Bold = 3;
headerTable.Cell(1, 3).Range.Font.Size = 6;
headerTable.Cell(1, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
headerTable.Cell(1, 3).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
headerTable.Cell(1, 3).Range.Text = "CONFIRMATION" + " QUOTATION:NO "
+ quoteHeader.Quote_Number + " " +
"PLEASE REFER TO THIS NUMBER WHEN ORDERING OR INQUIRING" +
" Revision No" + "|" + "Current Review " + quoteHeader.Rev_Number + " " + "|" + " " + quoteHeader.CurrentRev_Number + " ";
//To Write TO Address
Customer_Address_Override_ overrideCustomer = Quotes.SelectOverridCustomerByID(quoteHeader.Cust_Number, quoteHeader.Quote_Number);
if (overrideCustomer != null)
{
if (overrideCustomer.Cust_Name != string.Empty)
{
Microsoft.Office.Interop.Word.Range rangeTo = aDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
WordApp.Selection.TypeParagraph();
Microsoft.Office.Interop.Word.Table tblTo = aDoc.Tables.Add(rangeTo, 5, 3, ref missing, ref missing);
tblTo.Range.Font.Size = 8;
tblTo.Cell(1, 1).Range.Text = "To ," + overrideCustomer.Cust_Name;
tblTo.Cell(2, 1).Range.Text = " Attn :" + overrideCustomer.Cust_ContactName;
tblTo.Cell(3, 1).Range.Text = " " + overrideCustomer.Cust_Address1;
tblTo.Cell(4, 1).Range.Text = " " + overrideCustomer.Cust_Address2;
tblTo.Cell(5, 1).Range.Text = " " + overrideCustomer.Cust_Address3;
tblTo.Cell(5, 1).Range.Text = " " + overrideCustomer.Cust_City;
tblTo.Cell(1, 3).Range.Text = "Date :" + quoteHeader.Entry_Date;
tblTo.Cell(2, 3).Range.Text = "Account NO:" + overrideCustomer.Cust_Name;
tblTo.Cell(3, 3).Range.Text = "Page :" + "1 of 4";
tblTo.Cell(4, 3).Range.Text = "Phone :" + overrideCustomer.Cust_Phone;
//tblTo.Cell(5, 3).Range.Text = "FAX :" + overrideCustomer.Cust_Fax;
tblTo.Cell(5, 3).Range.Text = "Email :" + overrideCustomer.Cust_EmailId;
}
}
else
{
Microsoft.Office.Interop.Word.Range rangeTo = aDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
WordApp.Selection.TypeParagraph();
Microsoft.Office.Interop.Word.Table tblTo = aDoc.Tables.Add(rangeTo, 5, 3, ref missing, ref missing);
tblTo.Range.Font.Size = 8;
tblTo.Cell(1, 1).Range.Text = "To ," + customer.Cust_Name;
tblTo.Cell(2, 1).Range.Text = " " + "Attn :" + customer.Cust_ContactName;
tblTo.Cell(3, 1).Range.Text = " " + customer.Cust_Address1;
tblTo.Cell(4, 1).Range.Text = " " + customer.Cust_Address2;
tblTo.Cell(5, 1).Range.Text = " " + customer.Cust_Address3;
tblTo.Cell(5, 1).Range.Text = " " + customer.Cust_City;
tblTo.Cell(1, 3).Range.Text = "Date :" + quoteHeader.Entry_Date;
tblTo.Cell(2, 3).Range.Text = "Account NO:" + customer.Cust_Name;
tblTo.Cell(3, 3).Range.Text = "Page :" + "1 of 4";
tblTo.Cell(4, 3).Range.Text = "Phone :" + customer.Cust_Phone;
tblTo.Cell(5, 3).Range.Text = "FAX :" + customer.Cust_Fax;
tblTo.Cell(5, 3).Range.Text = "Email :" + customer.Cust_EmailId;
}
///Logic to add content tables
Microsoft.Office.Interop.Word.Range rangeItem = aDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
Microsoft.Office.Interop.Word.Table tbl = aDoc.Tables.Add(rangeItem, itemPerPage + 1, 6, ref missing, ref missing);
tbl.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;
tbl.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleNone;
tbl.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
tbl.Range.Font.Size = 7;
tbl.Columns[1].Width = WordApp.InchesToPoints((float)0.80);
tbl.Columns[2].Width = WordApp.InchesToPoints((float)2.48);
tbl.Columns[3].Width = WordApp.InchesToPoints((float).80);
tbl.Columns[4].Width = WordApp.InchesToPoints((float).80);
tbl.Columns[5].Width = WordApp.InchesToPoints((float).60);
tbl.Columns[6].Width = WordApp.InchesToPoints((float).60);
tbl.Columns[1].Cells[1].Height = WordApp.InchesToPoints((float)0.20);
tbl.Columns[2].Cells[1].Height = WordApp.InchesToPoints((float)0.28);
tbl.Columns[3].Cells[1].Height = WordApp.InchesToPoints((float).20);
tbl.Columns[4].Cells[1].Height = WordApp.InchesToPoints((float).20);
tbl.Columns[5].Cells[1].Height = WordApp.InchesToPoints((float).20);
tbl.Columns[6].Cells[1].Height = WordApp.InchesToPoints((float).20);
tbl.Cell(1, 1).WordWrap = true;
tbl.Cell(1, 2).WordWrap = true;
tbl.Cell(1, 5).WordWrap = true;
tbl.Cell(1, 4).WordWrap = true;
tbl.Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
tbl.Cell(1, 1).Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;
tbl.Cell(1, 1).Range.Text = "NUMBER";
tbl.Cell(1, 2).Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;
tbl.Cell(1, 2).Range.Text = "ITEM DESCRIPTION\n" + "ADDITIONAL ITEM SPEECIFICATION";
tbl.Cell(1, 3).Range.Text = " " + "MINIMUM" + Environment.NewLine + "Rolls (PKG/CTN)" + " " + "CTN (Wt)";
tbl.Cell(1, 3).Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;
tbl.Cell(1, 3).Range.Text = " " + "MINIMUM UNITS";
tbl.Cell(1, 4).Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;
tbl.Cell(1, 4).Range.Text = "UOM";
tbl.Cell(1, 5).Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;
tbl.Cell(1, 5).Range.Text = "PRICE" + Environment.NewLine + "PER UNIT";
int i = 2;
///Loginc to add item details
foreach (Quote_Detail item in quoteDetails)
{
tbl.Cell(i, 1).Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;
tbl.Cell(i, 1).Range.Text = item.Quote_Number.ToString();
tbl.Cell(i, 2).Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;
tbl.Cell(i, 2).Range.Text = "15 / 32 Wll 118Dorrls 30mm\n" + "Ocource there are no minimums of this per order. What ever you want goes lacoloa microsoft Cells";
tbl.Cell(i, 3).Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;
tbl.Cell(i, 3).Range.Text = item.Min_Cases.ToString();
tbl.Cell(i, 4).Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;
tbl.Cell(i, 4).Range.Text = "CS";
tbl.Cell(i, 5).Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;
tbl.Cell(i, 5).Range.Text = "$" + item.Item_Price.ToString();
i = i + 1;
}
//Footer Text
Microsoft.Office.Interop.Word.Table footerTable;
Microsoft.Office.Interop.Word.Range footer = aDoc.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerTable = aDoc.Tables.Add(footer, 7, 3, ref missing, ref missing);
footerTable.Range.Font.Size = 10;
footerTable.Range.Font.Bold = 2;
footerTable.Cell(1, 1).Range.Text = "Quote Expires :" + quoteHeader.Expiration_Date;
SalesPerson_Master salesPerson = Quotes.SelectSalesPersonByID((int)customer.SalesPerson_Id);
if (salesPerson != null)
{
footerTable.Cell(1, 2).Range.Text = "Sales Representative :" + salesPerson.SalesPerson_Name;
}
footerTable.Cell(1, 3).WordWrap = true;
footerTable.Cell(1, 3).Range.Text = "Thank you for this opportunity to Submit Quote on your requirement .We look forward to serving you.";
footerTable.Cell(2, 2).Range.Text = "Phone :" + salesPerson.SalesPerson_Phone;
footerTable.Cell(2, 3).Range.Text = " Very Truly Yours ,";
footerTable.Cell(3, 1).Range.Text = " FOB : SPRINGBORO, OH";
footerTable.Cell(3, 2).Range.Text = "Email" + salesPerson.SalesPerson_EmailId;
footerTable.Cell(3, 3).Range.Text = "PAPER SYSTEMS,INC.";
footerTable.Cell(5, 3).Range.Text = "Signature";
footerTable.Cell(5, 1).Range.Text = "ESTIMATED SHIP DATE :" + quoteHeader.WorkDays + "\n APPROVAL OF ART WORK";
Cell cell7 = footerTable.Rows[7].Cells[2];
footerTable.Rows[7].Cells[3].Merge(cell7);
footerTable.Rows[7].Cells[1].Merge(cell7);
footerTable.Cell(7, 1).Range.Font.Size = 10;
footerTable.Cell(7, 1).Range.Font.Bold = 5;
footerTable.Cell(7, 1).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
footerTable.Cell(7, 1).Range.Text = "PLACE ORDERS FREE! JUST DIAL : 1-888-564-6774 OR 1-888-329-9774";
// Get current date and time
this.Filename = customer.Cust_Name + " " + customer.Cust_Number + " " + "Quote" + quoteHeader.Quote_Number;
object folderName = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DirectoryInfo directory = Directory.CreateDirectory(folderName + "\\" + salesPerson.SalesPerson_Name + " " + salesPerson.SalesPerson_Number);
this.FolderName = directory.FullName;
object newwordFileName = directory.FullName + "\\" + this.Filename + ".docx";// @"D:\" + fileName + ".docx";
string tmpFilename = directory.FullName + "\\" + this.Filename + ".docx";//@"D:\" + fileName + ".docx";
if (directory != null)
{
if (File.Exists(tmpFilename))
{
File.Delete(tmpFilename);
}
}
aDoc.SaveAs(ref newwordFileName, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
aDoc.Close(ref missing, ref missing, ref missing);
if (aDoc != null)
{
object visible = true;
object readOnly = true;
object sourceFilename = newwordFileName;
string targetFilename = directory.FullName + "\\" + this.Filename + ".pdf";
if (File.Exists(targetFilename))
{
string[] fileCount = Directory.GetFiles(directory.FullName);
this.Filename = this.Filename + "_" + fileCount.Count().ToString();
targetFilename = directory.FullName + "\\" + this.Filename + ".pdf";
}
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
bool paramOpenAfterExport = false;
WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
object paramMissing = Type.Missing;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref sourceFilename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref visible, ref missing, ref missing,
ref missing, ref missing);
doc.ExportAsFixedFormat(targetFilename,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
doc.Close(ref missing, ref missing, ref missing);
if (File.Exists(tmpFilename))
{
File.Delete(tmpFilename);
}
wordApp.Quit(ref missing, ref missing, ref missing);
//System.Diagnostics.Process.Start(targetFilename);
// MessageBox.Show("The quote has been successfully saved");
}
}
else
{
MessageBox.Show("Please add atleast one items to quote");
}
}
catch (Exception ex)
{
MessageBox.Show("Cannot Create PDF");
}
}
No comments:
Post a Comment