private void PrepareSchedule(ScheduleReportModel ScheduleReportModel)
        {
            IEnumerable <GetPrintScheduleByDID_Result> results = db.GetPrintScheduleByDID(
                Convert.ToInt16(ScheduleReportModel.BranchId),
                Convert.ToInt16(ScheduleReportModel.ClientId),
                Convert.ToInt16(ScheduleReportModel.LoanApplicationId),
                Convert.ToInt16(ScheduleReportModel.DisbursementId),
                Convert.ToInt16(ScheduleReportModel.LoanCycleId));


            List <GetPrintScheduleByDID_Result> resultsFinalList = results.ToList <GetPrintScheduleByDID_Result>();

            GetPrintScheduleByDID_Result row = new GetPrintScheduleByDID_Result();

            foreach (var item in resultsFinalList)
            {
                row = item;

                break;
            }

            PrintCollectionSchedule(resultsFinalList, new ReportBuilderModel {
                ReportHeading = "Collection report", ActualPaid = row.ActualPaidAmount.Value.ToString(), ScheduleDate = row.ScheduleDate.Value.ToShortDateString(), BranchName = row.BranchCode + " / " + row.BranchName
            });
        }
        public void PrintCollectionSchedule(List <GetPrintScheduleByDID_Result> results, ReportBuilderModel model)
        {
            MemoryStream workStream = new MemoryStream();
            Document     document   = new Document();

            PdfWriter.GetInstance(document, workStream).CloseStream = false;
            PdfPTable table = new PdfPTable(8);

            table.TotalWidth = 550f;
            //fix the absolute width of the table
            table.LockedWidth = true;
            //relative col widths in proportions - 1/3 and 2/3
            float[] widths = new float[] { 7f, 4f, 2f, 3f, 3f, 3f, 3f, 4f };
            table.SetWidths(widths);
            table.HorizontalAlignment = 0;
            //leave a gap before and after the table
            table.SpacingBefore = 10f;
            table.SpacingAfter  = 10f;
            Font     bold = new Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
            Phrase   p    = new Phrase(model.ReportHeading, bold);
            PdfPCell cell = new PdfPCell(p);

            cell.BackgroundColor     = new iTextSharp.text.BaseColor(51, 192, 192);
            cell.Colspan             = 8;
            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            cell.VerticalAlignment   = 1;
            table.AddCell(cell);

            Font     b             = new Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);
            Phrase   reportSummary = new Phrase(string.Format("Collection Schedule on : {0}", DateTime.Now.Date.ToShortDateString()), b);
            PdfPCell cellHeading   = new PdfPCell(reportSummary);

            cellHeading.BackgroundColor     = new iTextSharp.text.BaseColor(192, 192, 192);
            cellHeading.Colspan             = 8;
            cellHeading.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
            cellHeading.VerticalAlignment   = 1;
            table.AddCell(cellHeading);

            //Add Branch and Total Amount
            Phrase   branchSummary = new Phrase(string.Format("Branch: {0} Schedule Date : {1} Actual Paid : {2}", model.BranchName, model.ScheduleDate, model.ActualPaid), b);
            PdfPCell cellHeading1  = new PdfPCell(branchSummary);

            cellHeading1.BackgroundColor     = new iTextSharp.text.BaseColor(192, 192, 192);
            cellHeading1.Colspan             = 8;
            cellHeading1.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
            cellHeading1.VerticalAlignment   = 1;
            table.AddCell(cellHeading1);

            PdfPCell cellBranchName = new PdfPCell(new Phrase("CL Code/Name"));

            cellBranchName.BackgroundColor = new iTextSharp.text.BaseColor(192, 192, 192);
            table.AddCell(cellBranchName);

            PdfPCell cellClientName = new PdfPCell(new Phrase("EMI Date"));

            cellClientName.BackgroundColor = new iTextSharp.text.BaseColor(192, 192, 192);
            table.AddCell(cellClientName);

            PdfPCell cellAmountPaid = new PdfPCell(new Phrase("EMI"));

            cellAmountPaid.BackgroundColor = new iTextSharp.text.BaseColor(192, 192, 192);
            table.AddCell(cellAmountPaid);

            PdfPCell cellPaymentDate = new PdfPCell(new Phrase("InstallNo"));

            cellPaymentDate.BackgroundColor = new iTextSharp.text.BaseColor(192, 192, 192);
            table.AddCell(cellPaymentDate);

            PdfPCell cellPrincipleAmount = new PdfPCell(new Phrase("Amt Paid"));

            cellPrincipleAmount.BackgroundColor = new iTextSharp.text.BaseColor(192, 192, 192);
            table.AddCell(cellPrincipleAmount);

            PdfPCell cellInterestAmount = new PdfPCell(new Phrase("Balance"));

            cellInterestAmount.BackgroundColor = new iTextSharp.text.BaseColor(192, 192, 192);
            table.AddCell(cellInterestAmount);


            PdfPCell cellSignature = new PdfPCell(new Phrase("Sign"));

            cellSignature.BackgroundColor = new iTextSharp.text.BaseColor(192, 192, 192);
            table.AddCell(cellSignature);

            PdfPCell cellAdditonalRemark = new PdfPCell(new Phrase("Remark"));

            cellAdditonalRemark.BackgroundColor = new iTextSharp.text.BaseColor(192, 192, 192);
            table.AddCell(cellAdditonalRemark);


            long?totalInstallment            = results.Max(x => x.EMINumber);
            GetPrintScheduleByDID_Result row = results.FirstOrDefault(x => x.ActualPaidAmount != 0);
            decimal?balance           = row.ActualPaidAmount;
            decimal?amountPaidTillNow = 0;
            decimal remainBalanceAll  = 0;

            foreach (Data.GetPrintScheduleByDID_Result item in results.ToList())
            {
                table.AddCell(item.ClientCode + " / " + item.ClientName);

                table.AddCell(item.EMIDate.Value.ToShortDateString());

                table.AddCell(item.EMI.ToString());

                table.AddCell(item.EMINumber.ToString() + " / " + totalInstallment);

                table.AddCell(item.AmountPaid.ToString());

                amountPaidTillNow += item.AmountPaid;
                decimal remainBalance = balance.Value - amountPaidTillNow.Value;
                remainBalanceAll = remainBalance;
                table.AddCell(remainBalance.ToString());

                table.AddCell(".............");

                table.AddCell(".............");
            }


            //Final Row summary
            table.AddCell(" ");

            table.AddCell(" ");

            table.AddCell("Total");

            table.AddCell(" - ");

            table.AddCell(amountPaidTillNow.ToString());

            table.AddCell(remainBalanceAll.ToString());

            table.AddCell(" ");

            table.AddCell(" ");


            document.Open();
            document.Add(table);
            document.Add(new Paragraph(DateTime.Now.ToString()));
            document.Close();

            byte[] byteInfo = workStream.ToArray();
            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;
            Response.Buffer     = true;
            Response.AddHeader("Content-Disposition", "attachment; filename= " + Server.HtmlEncode(string.Format("CollectionSchedule_{0}.pdf", DateTime.Now.Date.ToShortDateString())));
            Response.ContentType = "APPLICATION/pdf";
            Response.BinaryWrite(byteInfo);
        }