public async Task <ActionResult> printOutOfStock([FromBody] printOutOfStockRequest printOutOfStockRequest) { try { var items = await(from o in db.TbtOrder where o.DocNum == printOutOfStockRequest.DocNum join g in db.TbmItemGroup on o.ItemCode.Trim().Substring(0, 1) equals g.ItemGrpPrefix.Trim() select new { o, g }).ToListAsync(); List <OutOfStock> outOfStocktems = new List <OutOfStock>(); items.ForEach(x => { OutOfStock l = new OutOfStock { Dscription = x.o.Dscription, ItemCode = x.o.ItemCode, Price = (double)x.o.Price, Qty = (double)x.o.Quantity, ItemGrpCode = x.g.ItemGrpCode, ItemGrpName = x.g.ItemGrpName }; outOfStocktems.Add(l); }); OutOfStockDocumentModel outOfStockDocumentModel = new OutOfStockDocumentModel { Address = items.FirstOrDefault().o.Address, CardName = items.FirstOrDefault().o.CardName, DocDate = items.FirstOrDefault().o.DocDate, DocDueDate = items.FirstOrDefault().o.DocDueDate, DocNum = printOutOfStockRequest.DocNum, OutOfStocks = outOfStocktems, Remark = items.FirstOrDefault().o.Remark, }; OutOfStockDocument pDoc = new OutOfStockDocument(this._hostingEnvironment); Stream resultPDFStream = pDoc.CreatePDF(outOfStockDocumentModel); resultPDFStream.Position = 0; if (resultPDFStream.Length != 0) { FileStreamResult fileStreamResult = new FileStreamResult(resultPDFStream, "application/pdf"); fileStreamResult.FileDownloadName = "OutOfStock_" + "" + ".pdf"; return(fileStreamResult); } else { return(NotFound()); } } catch (Exception ex) { return(StatusCode(500, ex)); } }
public Stream CreatePDF(OutOfStockDocumentModel outOfStockDocumentModel) { System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); Document document = this._document; PdfWriter writer = PdfWriter.GetInstance(document, memoryStream); document.Open(); float[] widthBoxs = new float[] { 15f, 20f, 20f, 15f, 15f, 15f }; PdfPTable tableTitle = new PdfPTable(widthBoxs.Count()); tableTitle.SetWidths(widthBoxs); tableTitle.WidthPercentage = 95f; tableTitle.AddCell(bulidColumnTitleBox(@"ใบแจ้งรายการสินค้าขาดคราว", 20, 6, Element.ALIGN_CENTER, 0)); tableTitle.AddCell(bulidEmptryRows(widthBoxs.Count())); tableTitle.AddCell(bulidColumnTitleBox(@"ลงวันที่ :", 16, 1, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox(outOfStockDocumentModel.DocDueDate == null ? "" : outOfStockDocumentModel.DocDueDate.Value.ToString("dd/MM/yyyy"), 14, 2, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox(@"พนักงานขาย :", 16, 1, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox("", 14, 2, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox(@"ชื่อลูกค้า :", 16, 1, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox(outOfStockDocumentModel.CardName, 14, 2, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox(@"เลขที่ใบสั่งขาย :", 16, 1, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox("", 14, 2, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox(@"ที่อยู่ :", 16, 1, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox(outOfStockDocumentModel.Address, 14, 5, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox(@"สถานที่ส่งของ :", 16, 1, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox(outOfStockDocumentModel.Address, 14, 5, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox(@"หมายเหตุ :", 16, 1, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidColumnTitleBox(outOfStockDocumentModel.Remark, 14, 5, Element.ALIGN_LEFT, 0)); tableTitle.AddCell(bulidEmptryRows(widthBoxs.Count())); document.Add(tableTitle); string[] headers = new string[] { "No", "รหัส", "รายการ", "ค่าส่ง", "ราคา", "จำนวนเงิน" }; float[] withheaders = new float[] { 5f, 20f, 45f, 10f, 10f, 10f }; PdfPTable tableDetail = new PdfPTable(headers.Count()); tableDetail.SetWidths(withheaders); tableDetail.WidthPercentage = 95f; tableDetail.HeaderRows = 1; foreach (string h in headers) { tableDetail.AddCell(bulidColumnHeader(h, 16)); } foreach (var group in outOfStockDocumentModel.OutOfStocks.Select(x => new { x.ItemGrpCode, x.ItemGrpName }).Distinct()) { tableDetail.AddCell(bulidColumnDetail(@"ประเภทสินค้า : " + group.ItemGrpCode + "-" + group.ItemGrpName, 14, 6, Element.ALIGN_CENTER)); int i = 1; foreach (var item in outOfStockDocumentModel.OutOfStocks.Where(x => x.ItemGrpCode == group.ItemGrpCode)) { tableDetail.AddCell(bulidColumnDetail(i.ToString(), 14, 1, Element.ALIGN_CENTER)); tableDetail.AddCell(bulidColumnDetail(item.ItemCode, 14, 1, Element.ALIGN_CENTER)); tableDetail.AddCell(bulidColumnDetail(item.Dscription, 14, 1, Element.ALIGN_LEFT)); tableDetail.AddCell(bulidColumnDetail((item.Qty).ToString(), 14, 1, Element.ALIGN_CENTER)); tableDetail.AddCell(bulidColumnDetail((item.Price).ToString(), 14, 1, Element.ALIGN_CENTER)); tableDetail.AddCell(bulidColumnDetail((item.Price * item.Qty).ToString(), 14, 1, Element.ALIGN_CENTER)); i++; } } tableDetail.AddCell(bulidEmptryRows(6)); document.Add(tableDetail); float[] withfooster = new float[] { 15f, 85f }; PdfPTable tableFooster = new PdfPTable(withfooster.Count()); tableFooster.SetWidths(withfooster); tableFooster.WidthPercentage = 95f; tableFooster.AddCell(bulidColumnFooster("หมายเหตุ :", 14, 1)); tableFooster.AddCell(bulidColumnFooster("สินค้าข้างต้นขาดคราว บริษัทจะจัดส่งให้เมื่อพิมพ์เรียบร้อยแล้ว หากมีข้อสงสัย หรือต้องการยกเลิกรายการขาดคราว", 14, 1)); tableFooster.AddCell(bulidColumnFooster("", 14, 1)); tableFooster.AddCell(bulidColumnFooster("โปรดติดต่อแผนกลูกค้าสัมพัรธ์ โทร 0-2512-0661 ต่อ 2511-15ม2521-25 ภายใน 15 วัน", 14, 1)); document.Add(tableFooster); document.Close(); return(memoryStream); }