C#使用iTextSharp打印PDF

打印PDF

using iTextSharp.text.pdf;
using iTextSharp.text;
 //字體
        private BaseFont bfHei = BaseFont.CreateFont(@"C:\Windows\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //表格寬度
        private float _tablewidth = 500f;

        //將PDF通過瀏覽器下載到客戶端
        private void PDFToLocal(string PdfPath ,string docName)
        {
            base.Response.Clear();
            byte[] data = File.ReadAllBytes(PdfPath + docName + ".pdf");
            MemoryStream stream = new MemoryStream(data);
            base.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.pdf", docName));
            base.Response.BinaryWrite(stream.ToArray());
            stream.Close();
            stream.Dispose();
            File.Delete(PdfPath + docName + ".pdf");
            base.Response.End();
        }


        ///<summary>
        ///客戶賬單模板
        /// PDFType 1:對賬單  2:欠款單
        /// </summary>
        private void PDFCustomer(int PDFType)
        {
            #region PDF設置
            string PdfPath = Server.MapPath("/Upload/Temp/");
            string docType = "";
            if (PDFType==1) 
            {
                docType = "客戶對賬單";
            }
            else if (PDFType == 2) 
            {
                docType = "客戶欠款單";
            }
            string docName = string.Format("{0}{1:yyyyMMddHHmmssffff}", docType, DateTime.Now);
            //設置中文字體
            //BaseFont bfHei = BaseFont.CreateFont(@"C:\Windows\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font font = new iTextSharp.text.Font(bfHei, 11);
            iTextSharp.text.Font font2 = new iTextSharp.text.Font(bfHei, 10);
            //設置居中
            int aligncenter = iTextSharp.text.Rectangle.ALIGN_CENTER;

            //設置紙張尺寸
            int pagenum = int.Parse(this.ddlPaperSize.SelectedValue);
            iTextSharp.text.Rectangle pagesize = PageSize.A4;
            switch (pagenum)
            {
                case 0: pagesize = PageSize.A0; break;
                case 1: pagesize = PageSize.A1; break;
                case 2: pagesize = PageSize.A2; break;
                case 3: pagesize = PageSize.A3; break;
                case 4: pagesize = PageSize.A4; break;
                case 5: pagesize = PageSize.A5; break;
            }
            iTextSharp.text.Document document = new iTextSharp.text.Document(pagesize);

            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PdfPath + docName + ".pdf", FileMode.Create));


            #endregion


            #region 數據來源
            //獲取數據
            StringBuilder builder = new StringBuilder();

            //付款狀態
            if (PDFType == 2)
            {
                if (builder.Length > 1)
                {
                    builder.Append(" and ");
                }
                builder.Append(" PaymentStatus = 0 ");
            }


            if (PageValidate.IsDateTime(this.txtCreatedDateEnd.Text.Trim()) && PageValidate.IsDateTime(this.txtCreatedDateStart.Text.Trim()))
            {
                if (builder.Length > 1)
                {
                    builder.Append(" and ");
                }
                builder.AppendFormat(" CreatedDate between  '{0}' and  '{1}' ", InjectionFilter.QuoteFilter(this.txtCreatedDateStart.Text.Trim()), InjectionFilter.QuoteFilter(this.txtCreatedDateEnd.Text.Trim()));
            }
            //值查看印刷訂單
            if (builder.Length > 1)
            {
                builder.Append(" and ");
            }
            builder.AppendFormat(" OrderType in ({0},{1}) ", (int)V5print.Model.Shop.Order.EnumHelper.OrderItemType.Print, (int)V5print.Model.Shop.Order.EnumHelper.OrderItemType.Template);
            //已發貨的訂單
            if (builder.Length > 1)
            {
                builder.Append(" and ");
            }
            builder.Append(" ShippingStatus>1 ");
            DataSet dataSetSource = this.orderBll.GetListWithEmployee(-1, builder.ToString(), "CreatedDate desc");




            DateTime orderStartTime = new DateTime();
            DateTime orderEndTime = new DateTime();
            int TimeCountFlag = 0;
            //BuyerID去重、EmployeeID去重
            HashSet<int> BuyerIDs = new HashSet<int>();
            HashSet<int> EmployeeIDs = new HashSet<int>();
            foreach (DataRow row0 in dataSetSource.Tables[0].Rows)
            {
                TimeCountFlag++;
                if (TimeCountFlag == 1)
                {
                    orderEndTime = row0.Field<DateTime>("CreatedDate");
                }
                if (TimeCountFlag == dataSetSource.Tables[0].Rows.Count)
                {
                    orderStartTime = row0.Field<DateTime>("CreatedDate");
                }
                BuyerIDs.Add(row0.Field<int>("BuyerID"));
                EmployeeIDs.Add(row0.Field<int>("UserID"));
            }


            string startTime = "";
            string endTime = "";
            if (PageValidate.IsDateTime(this.txtCreatedDateEnd.Text.Trim()) && PageValidate.IsDateTime(this.txtCreatedDateStart.Text.Trim()))
            {
                startTime = this.txtCreatedDateEnd.Text.Trim();
                endTime = this.txtCreatedDateStart.Text.Trim();
            }
            else
            {
                startTime = orderStartTime.ToString("yyyy-MM-dd hh:mm:ss");
                endTime = orderEndTime.ToString("yyyy-MM-dd hh:mm:ss");
            }
            #endregion

            //this.BindData();
            //DataSet dataSetSource = this.gridView.DataSetSource;
            HashSet<int> hs = new HashSet<int>();
            hs = BuyerIDs;

            if (hs.Count == 0)
            {
                MessageBox.ShowFailTip(this, "抱歉,暫無數據!");
                return;
            }
            else 
            {
                document.Open();
            }

            foreach (var hsNow in hs)
            {


                #region BuyerID
                int countflag = 1;
                StringBuilder sbBuyerID = new StringBuilder();

                //按客戶分頁
                sbBuyerID.AppendFormat(" and BuyerID={0} ", hsNow.ToString());
                DataSet dataSetSource2 = this.orderBll.GetListWithEmployee(-1, builder.ToString() + sbBuyerID.ToString(), "CreatedDate desc");

                //彙總數據
                //總金額
                decimal AllPageAmount = 0;
                //總已付款
                decimal AllPagePaid = 0;
                //總欠款
                decimal AllPageDebt = 0;

                #region 打印數據
                int rowCount = 1;
                foreach (DataRow row2 in dataSetSource2.Tables[0].Rows)
                {

                    iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(11);//11列
                    table.TotalWidth = _tablewidth;
                    table.LockedWidth = false;
                    #region 表頭
                    if (countflag == 1)
                    {
                        //按客戶分頁
                        //row["收貨地址"] 
                        string Address = row2.Field<String>("ShipRegion") + row2.Field<String>("ShipAddress");
                        //row["收貨人"]
                        string ShipName = row2.Field<String>("ShipName");
                        //row["電話"] 
                        string CellPhone = row2.Field<String>("ShipCellPhone");
                        //手機ShipTelPhone
                        string Phone = row2.Field<String>("ShipTelPhone");
                        //發貨方式ShippingModeName
                        string ShipWay = row2.Field<String>("ShippingModeName");
                        //客戶名稱
                        V5print.Model.Members.Users user = userBll.GetModel(row2.Field<int>("BuyerId"));
                        string UserNickName = user.NickName;

                        //取出當前用戶的所有的用信用支付且未支付的訂單信息,包括本次下單的信息
                        string sql = string.Format(" BuyerID={0} and PaymentStatus=0 and PaymentGateway='credit'", user.UserID);
                        List<V5print.Model.Shop.Order.OrderInfo> orderList = orderBll.GetModelList(sql);
                        decimal debt = 0;//欠款金額
                        if (orderList.Count > 0)
                        {
                            foreach (V5print.Model.Shop.Order.OrderInfo order in orderList)
                            {
                                debt += order.Amount;
                            }
                        }
                        //row["欠款"] = Math.Round(debt, 2);
                        string AllDebt = Math.Round(debt, 2).ToString();

                        V5print.BLL.Members.UsersExp userexpbLL = new V5print.BLL.Members.UsersExp();
                        V5print.Model.Members.UsersExpModel userexp = userexpbLL.GetUsersExpModel(user.UserID);
                        decimal bal = 0;
                        if (userexp != null)
                        {
                            bal = Math.Round(decimal.Parse(userexp.Balance.ToString()), 2);
                        }
                        string balance = bal.ToString();

                        PdfPCell header1 = new PdfPCell(new Phrase("地址:" + Address, font));
                        PdfPCell header2 = new PdfPCell(new Phrase("聯繫人:" + ShipName, font));
                        PdfPCell header3 = new PdfPCell(new Phrase("電話:" + CellPhone, font));
                        PdfPCell header4 = new PdfPCell(new Phrase("手機:" + Phone, font));
                        PdfPCell header5 = new PdfPCell(new Phrase("發貨方式:" + ShipWay, font));
                        PdfPCell header6 = new PdfPCell(new Phrase("客戶名稱:" + UserNickName, font));
                        //PdfPCell header7 = new PdfPCell(new Phrase("總欠款:" + AllDebt, font));

                        //Phrase h8phrase = new Phrase();
                        //if (this.cbShowBalance.Checked)
                        //{
                        //    h8phrase = new Phrase("預存餘額:" + balance, font);
                        //}
                        //PdfPCell header8 = new PdfPCell(h8phrase);

                        //設置每個單元格所佔列數
                        header1.Colspan = 6;
                        header2.Colspan = 5;
                        header3.Colspan = 3;
                        header4.Colspan = 3;
                        header5.Colspan = 5;
                        header6.Colspan = 11;
                        //header7.Colspan = 6;
                        //header8.Colspan = 5;

                        //添加單元格到表格中
                        table.AddCell(header1);
                        table.AddCell(header2);
                        table.AddCell(header3);
                        table.AddCell(header4);
                        table.AddCell(header5);
                        table.AddCell(header6);
                        //table.AddCell(header7);
                        //table.AddCell(header8);


                        PdfPCell header9 = new PdfPCell(new Phrase("訂單詳情", font2));
                        header9.Colspan = 11;
                        header9.HorizontalAlignment = aligncenter;
                        table.AddCell(header9);

                        PdfPCell t1 = new PdfPCell(new Phrase("序號", font2));
                        PdfPCell t2 = new PdfPCell(new Phrase("訂單號", font2));

                        Phrase t3Cell = new Phrase();
                        //if (this.cbIsDetail.Checked)
                        //{
                        //    t3Cell = new Phrase("交易內容", font2);
                        //}
                        //else
                        //{
                        //    t3Cell = new Phrase("客戶名稱", font2);
                        //}

                        t3Cell = new Phrase("交易內容", font2);
                        PdfPCell t3 = new PdfPCell(t3Cell);
                        PdfPCell t4 = new PdfPCell(new Phrase("產品類型", font2));
                        PdfPCell t5 = new PdfPCell(new Phrase("數量", font2));
                        PdfPCell t9 = new PdfPCell(new Phrase("款數", font2));
                        PdfPCell t6 = new PdfPCell(new Phrase("總金額", font2));

                        PdfPCell t7 = new PdfPCell(new Phrase("已付款", font2));
                        PdfPCell t8 = new PdfPCell(new Phrase("未付款", font2));

                        //設置每個單元格所佔列數
                        t1.Colspan = 1;
                        t2.Colspan = 2;
                        t3.Colspan = 2;
                        t4.Colspan = 1;
                        t5.Colspan = 1;
                        t6.Colspan = 1;

                        t7.Colspan = 1;
                        t8.Colspan = 1;
                        t9.Colspan = 1;

                        //設置字體居中
                        t1.HorizontalAlignment = aligncenter;
                        t2.HorizontalAlignment = aligncenter;
                        t3.HorizontalAlignment = aligncenter;
                        t4.HorizontalAlignment = aligncenter;
                        t5.HorizontalAlignment = aligncenter;
                        t6.HorizontalAlignment = aligncenter;

                        t7.HorizontalAlignment = aligncenter;
                        t8.HorizontalAlignment = aligncenter;
                        t9.HorizontalAlignment = aligncenter;

                        //添加單元格到表格中
                        table.AddCell(t1);
                        table.AddCell(t2);
                        table.AddCell(t3);
                        table.AddCell(t4);
                        table.AddCell(t5);
                        table.AddCell(t9);
                        table.AddCell(t6);

                        table.AddCell(t7);
                        table.AddCell(t8);
                    }
                    #endregion

                    #region 表中數據

                    List<V5print.Model.Shop.Order.OrderItems> modelListByCache = this.orderItemManage.GetModelListByCache(" OrderId=" + row2.Field<long>("OrderId"));
                    if ((modelListByCache != null) && (modelListByCache.Count > 0))
                    {
                        int rownum = 1;
                        foreach (V5print.Model.Shop.Order.OrderItems info in modelListByCache)
                        {
                            //次序
                            //PdfPCell c1 = new PdfPCell(new Phrase(rowCount.ToString() + "-" + rownum.ToString(), font2));
                            PdfPCell c1 = new PdfPCell(new Phrase(rowCount.ToString(), font2));
                            rowCount++;
                            //訂單號
                            PdfPCell c2 = new PdfPCell(new Phrase(row2.Field<string>("OrderCode"), font2));

                            Phrase c3Cell = new Phrase();
                            //交易內容
                            string AttributeNames = "";
                            if (info.TypeId == 14 || info.TypeId == 13)
                            {
                                AttributeNames = info.AttributeNames.Split(',')[0];
                            }
                            else if (info.TypeId == 12)
                            {
                                AttributeNames = "樣本";
                            }

                            c3Cell = new Phrase(AttributeNames, font2);


                            PdfPCell c3 = new PdfPCell(c3Cell);
                            //產品類型
                            PdfPCell c4 = new PdfPCell(new Phrase(new V5print.BLL.Shop.Products.ProductType().GetModelByCache(info.TypeId).TypeName, font2));
                            //數量
                            PdfPCell c5 = new PdfPCell(new Phrase(info.Quantity.ToString() + info.Unit, font2));
                            //款數
                            string ModelCount = "1";
                            if (row2.Field<Int32>("ModelCount") != null)
                            {
                                ModelCount = row2.Field<Int32>("ModelCount").ToString();
                            }
                            PdfPCell c9 = new PdfPCell(new Phrase(ModelCount + "款", font2));

                            //總金額
                            PdfPCell c6 = new PdfPCell(new Phrase(info.Amount.ToString("F"), font2));


                            AllPageAmount += info.Amount;

                            //已付款和欠款
                            PdfPCell c7 = new PdfPCell();
                            PdfPCell c8 = new PdfPCell();
                            int PaymentStatus = int.Parse(row2.Field<Int16>("PaymentStatus").ToString());
                            if (PaymentStatus == (int)V5print.Model.Shop.Order.EnumHelper.PaymentStatus.Paid)
                            {
                                //已支付
                                c7 = new PdfPCell(new Phrase(info.Amount.ToString("F"), font2));
                                c8 = new PdfPCell(new Phrase("0", font2));

                                AllPagePaid += info.Amount;
                            }
                            else
                            {
                                c7 = new PdfPCell(new Phrase("0", font2));
                                c8 = new PdfPCell(new Phrase(info.Amount.ToString("F"), font2));
                                AllPageDebt += info.Amount;
                            }





                            //設置每個單元格所佔列數
                            c1.Colspan = 1;
                            c2.Colspan = 2;
                            c3.Colspan = 2;
                            c4.Colspan = 1;
                            c5.Colspan = 1;
                            c6.Colspan = 1;

                            c7.Colspan = 1;
                            c8.Colspan = 1;
                            c9.Colspan = 1;

                            //居中
                            c1.HorizontalAlignment = aligncenter;
                            c2.HorizontalAlignment = aligncenter;
                            c3.HorizontalAlignment = aligncenter;
                            c4.HorizontalAlignment = aligncenter;
                            c5.HorizontalAlignment = aligncenter;
                            c6.HorizontalAlignment = aligncenter;

                            c7.HorizontalAlignment = aligncenter;
                            c8.HorizontalAlignment = aligncenter;
                            c9.HorizontalAlignment = aligncenter;

                            //添加單元格到表格中
                            table.AddCell(c1);
                            table.AddCell(c2);
                            table.AddCell(c3);
                            table.AddCell(c4);
                            table.AddCell(c5);
                            table.AddCell(c9);
                            table.AddCell(c6);

                            table.AddCell(c7);
                            table.AddCell(c8);

                            rownum++;
                        }
                    }

                    #endregion


                    #region 表頭文字
                    if (countflag == 1)
                    {
                        Paragraph Title1 = new Paragraph(this.WebSiteSet.Company_Name, font);
                        Paragraph Title2 = new Paragraph("_______________________________", font);
                        string tel = "";
                        if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_Tel400))
                        {
                            tel = this.WebSiteSet.Company_Tel400;
                        }
                        else if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_Telephone))
                        {
                            tel = this.WebSiteSet.Company_Telephone;
                        }
                        else if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_MobilePhone))
                        {
                            tel = this.WebSiteSet.Company_MobilePhone;
                        }

                        Paragraph Title3 = new Paragraph("全國免費服務電話:" + tel, font);
                        Paragraph Title4 = new Paragraph("時間" + startTime + " ~ " + endTime, font);

                        //設置居中  
                        Title1.Alignment = aligncenter;
                        Title2.Alignment = aligncenter;
                        Title3.Alignment = aligncenter;
                        Title4.Alignment = aligncenter;


                        //空一行  
                        Paragraph nullp = new Paragraph(" ", font);
                        nullp.Leading = 10;

                        //將標題段加入PDF文檔中 
                        document.Add(Title1);
                        document.Add(Title2);
                        document.Add(Title3);
                        document.Add(Title4);
                        document.Add(nullp);
                    }
                    #endregion

                    document.Add(table);//table end 

                    table = null;
                    countflag++;


                }

                #endregion


                //彙總數據
                iTextSharp.text.pdf.PdfPTable tableGather = new iTextSharp.text.pdf.PdfPTable(11);//11列
                tableGather.TotalWidth = _tablewidth;
                tableGather.LockedWidth = false;
                PdfPCell gather1 = new PdfPCell(new Phrase("總計", font));
                PdfPCell gather2 = new PdfPCell(new Phrase(AllPageAmount.ToString("0.00"), font));
                PdfPCell gather3 = new PdfPCell(new Phrase(AllPagePaid.ToString("0.00"), font));
                PdfPCell gather4 = new PdfPCell(new Phrase(AllPageDebt.ToString("0.00"), font));

                gather1.Colspan = 8;
                gather2.Colspan = 1;
                gather3.Colspan = 1;
                gather4.Colspan = 1;

                gather1.HorizontalAlignment = iTextSharp.text.Rectangle.ALIGN_RIGHT;
                gather2.HorizontalAlignment = aligncenter;
                gather3.HorizontalAlignment = aligncenter;
                gather4.HorizontalAlignment = aligncenter;

                tableGather.AddCell(gather1);
                tableGather.AddCell(gather2);
                tableGather.AddCell(gather3);
                tableGather.AddCell(gather4);
                document.Add(tableGather);

                #endregion
                document.NewPage();
            }

            if (hs.Count == 0)
            {
                MessageBox.ShowFailTip(this, "抱歉,暫無數據!");
                return;
            }
            else
            {
                document.Close();
            }


            PDFToLocal(PdfPath ,docName);
        }


        //客戶對賬單
        private void PrintPDFCustomerDetail() 
        {
            this.PDFCustomer(1);
        }

        //客戶欠款單
        private void PrintPDFCustomerDebt() 
        {
            this.PDFCustomer(2);
        }












        ///<summary>
        ///業務員單據模板2(不含訂單明細)
        ///PDFType 1:對賬單  2:欠款單
        ///</summary>
        private void PDFEmployee2_no(int PDFType)
        {
            #region PDF設置
            string PdfPath = Server.MapPath("/Upload/Temp/");
            string docType = "";
            if (PDFType == 1)
            {
                docType = "業務員對賬單二";
            }
            else if (PDFType == 2)
            {
                docType = "業務員欠款單";
            }
            string docName = string.Format("{0}{1:yyyyMMddHHmmssffff}", docType, DateTime.Now);
            //設置中文字體
            //BaseFont bfHei = BaseFont.CreateFont(@"C:\Windows\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font font = new iTextSharp.text.Font(bfHei, 11);
            iTextSharp.text.Font font2 = new iTextSharp.text.Font(bfHei, 10);
            //設置居中
            int aligncenter = iTextSharp.text.Rectangle.ALIGN_CENTER;

            //設置紙張尺寸
            int pagenum = int.Parse(this.ddlPaperSize.SelectedValue);
            iTextSharp.text.Rectangle pagesize = PageSize.A4;
            switch (pagenum)
            {
                case 0: pagesize = PageSize.A0; break;
                case 1: pagesize = PageSize.A1; break;
                case 2: pagesize = PageSize.A2; break;
                case 3: pagesize = PageSize.A3; break;
                case 4: pagesize = PageSize.A4; break;
                case 5: pagesize = PageSize.A5; break;
            }
            iTextSharp.text.Document document = new iTextSharp.text.Document(pagesize);


            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PdfPath + docName + ".pdf", FileMode.Create));
            document.Open();
            #endregion

            #region 數據來源
            //獲取數據
            StringBuilder builder = new StringBuilder();

            //付款狀態
            if (PDFType == 2)
            {
                if (builder.Length > 1)
                {
                    builder.Append(" and ");
                }
                builder.Append(" PaymentStatus = 0 ");
            }



            if (PageValidate.IsDateTime(this.txtCreatedDateEnd.Text.Trim()) && PageValidate.IsDateTime(this.txtCreatedDateStart.Text.Trim()))
            {
                if (builder.Length > 1)
                {
                    builder.Append(" and ");
                }
                builder.AppendFormat(" CreatedDate between  '{0}' and  '{1}' ", InjectionFilter.QuoteFilter(this.txtCreatedDateStart.Text.Trim()), InjectionFilter.QuoteFilter(this.txtCreatedDateEnd.Text.Trim()));
            }
            //值查看印刷訂單
            if (builder.Length > 1)
            {
                builder.Append(" and ");
            }
            builder.AppendFormat(" OrderType in ({0},{1}) ", (int)V5print.Model.Shop.Order.EnumHelper.OrderItemType.Print, (int)V5print.Model.Shop.Order.EnumHelper.OrderItemType.Template);
            //已發貨的訂單
            if (builder.Length > 1)
            {
                builder.Append(" and ");
            }
            builder.Append(" ShippingStatus>1 ");
            DataSet dataSetSource = this.orderBll.GetListWithEmployee(-1, builder.ToString(), "CreatedDate desc");

            DateTime orderStartTime = new DateTime();
            DateTime orderEndTime = new DateTime();
            int TimeCountFlag = 0;
            //BuyerID去重、EmployeeID去重
            HashSet<int> BuyerIDs = new HashSet<int>();
            HashSet<int> EmployeeIDs = new HashSet<int>();
            foreach (DataRow row0 in dataSetSource.Tables[0].Rows)
            {
                TimeCountFlag++;
                if (TimeCountFlag == 1)
                {
                    orderEndTime = row0.Field<DateTime>("CreatedDate");
                }
                if (TimeCountFlag == dataSetSource.Tables[0].Rows.Count)
                {
                    orderStartTime = row0.Field<DateTime>("CreatedDate");
                }
                BuyerIDs.Add(row0.Field<int>("BuyerID"));
                EmployeeIDs.Add(row0.Field<int>("UserID"));
            }

            string startTime = "";
            string endTime = "";
            if (PageValidate.IsDateTime(this.txtCreatedDateEnd.Text.Trim()) && PageValidate.IsDateTime(this.txtCreatedDateStart.Text.Trim()))
            {
                startTime = this.txtCreatedDateEnd.Text.Trim();
                endTime = this.txtCreatedDateStart.Text.Trim();
            }
            else
            {
                startTime = orderStartTime.ToString("yyyy-MM-dd hh:mm:ss");
                endTime = orderEndTime.ToString("yyyy-MM-dd hh:mm:ss");
            }
            #endregion


            HashSet<int> hs = new HashSet<int>();
            //按業務員分頁
            hs = EmployeeIDs;
            foreach (var hsNow in hs)
            {
                #region BuyerID
                int countflag = 1;
                StringBuilder sbBuyerID = new StringBuilder();

                //按業務員分頁
                sbBuyerID.AppendFormat(" and p3.UserID={0} ", hsNow.ToString());

                DataSet dataSetSource2 = this.orderBll.GetListWithEmployee(-1, builder.ToString() + sbBuyerID.ToString(), "CreatedDate desc");

                #region 打印數據
                int rowCount = 1;
                foreach (DataRow row2 in dataSetSource2.Tables[0].Rows)
                {

                    iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(11);//11列
                    table.TotalWidth = _tablewidth;
                    table.LockedWidth = true;
                    if (countflag == 1)
                    {
                        //按業務員分頁
                        PdfPCell header21 = new PdfPCell(new Phrase("業務員:" + row2.Field<String>("UserName"), font));
                        header21.Colspan = 11;
                        table.AddCell(header21);

                        PdfPCell header9 = new PdfPCell(new Phrase("訂單詳情", font2));
                        header9.Colspan = 11;
                        header9.HorizontalAlignment = aligncenter;
                        table.AddCell(header9);

                        PdfPCell t1 = new PdfPCell(new Phrase("序號", font2));
                        //PdfPCell t2 = new PdfPCell(new Phrase("訂單號", font2));

                        PdfPCell t3 = new PdfPCell(new Phrase("客戶名稱", font2));
                        PdfPCell t4 = new PdfPCell(new Phrase("交易總額", font2));
                        PdfPCell t5 = new PdfPCell(new Phrase("訂單總數", font2));
                        PdfPCell t6 = new PdfPCell(new Phrase("已收金額", font2));

                        PdfPCell t7 = new PdfPCell(new Phrase("欠款金額", font2));
                        PdfPCell t8 = new PdfPCell(new Phrase("預存餘額", font2));

                        //設置每個單元格所佔列數
                        t1.Colspan = 1;
                        //t2.Colspan = 2;
                        t3.Colspan = 5;
                        t4.Colspan = 1;
                        t5.Colspan = 1;
                        t6.Colspan = 1;

                        t7.Colspan = 1;
                        t8.Colspan = 1;

                        //設置字體居中
                        t1.HorizontalAlignment = aligncenter;
                        //t2.HorizontalAlignment = aligncenter;
                        t3.HorizontalAlignment = aligncenter;
                        t4.HorizontalAlignment = aligncenter;
                        t5.HorizontalAlignment = aligncenter;
                        t6.HorizontalAlignment = aligncenter;

                        t7.HorizontalAlignment = aligncenter;
                        t8.HorizontalAlignment = aligncenter;

                        //添加單元格到表格中
                        table.AddCell(t1);
                        //table.AddCell(t2);
                        table.AddCell(t3);
                        table.AddCell(t4);
                        table.AddCell(t5);
                        table.AddCell(t6);

                        table.AddCell(t7);
                        table.AddCell(t8);
                    }



                    List<V5print.Model.Shop.Order.OrderItems> modelListByCache = this.orderItemManage.GetModelListByCache(" OrderId=" + row2.Field<long>("OrderId"));
                    if ((modelListByCache != null) && (modelListByCache.Count > 0))
                    {
                        int rownum = 1;
                        foreach (V5print.Model.Shop.Order.OrderItems info in modelListByCache)
                        {
                            //次序
                            //PdfPCell c1 = new PdfPCell(new Phrase(rowCount.ToString() + "-" + rownum.ToString(), font2));
                            PdfPCell c1 = new PdfPCell(new Phrase(rowCount.ToString(), font2));
                            rowCount++;
                            //訂單號
                            //PdfPCell c2 = new PdfPCell(new Phrase(row2.Field<string>("OrderCode"), font2));

                            Phrase c3Cell = new Phrase();
                            //客戶名稱
                            V5print.Model.Members.Users c3user = userBll.GetModel(row2.Field<int>("BuyerId"));
                            string c3UserNickName = c3user.NickName;
                            c3Cell = new Phrase(c3UserNickName, font2);
                            PdfPCell c3 = new PdfPCell(c3Cell);


                            //string sql = string.Format(" BuyerID={0} and PaymentStatus=0 and PaymentGateway='credit'", c3user.UserID);
                            string sql = string.Format(" BuyerID={0} ", c3user.UserID);
                            List<V5print.Model.Shop.Order.OrderInfo> orderList = orderBll.GetModelList(sql);
                            //交易總額
                            decimal allAmount = 0;
                            //已收金額
                            decimal allPaid = 0;

                            if (orderList.Count > 0)
                            {
                                foreach (V5print.Model.Shop.Order.OrderInfo order in orderList)
                                {
                                    allAmount += order.Amount;
                                    //已收金額
                                    if (order.PaymentStatus == (int)V5print.Model.Shop.Order.EnumHelper.PaymentStatus.Paid)
                                    {
                                        allPaid += order.Amount;
                                    }

                                }
                            }
                            //欠款金額
                            decimal allDebt = allAmount - allPaid;
                            //預存餘額
                            decimal allBalance = 0;

                            V5print.Model.Members.UsersExpModel userexp = userexpBll.GetUsersExpModel(c3user.UserID);
                            if (userexp != null)
                            {
                                allBalance = Math.Round(decimal.Parse(userexp.Balance.ToString()), 2);
                            }

                            //交易總額
                            PdfPCell c4 = new PdfPCell(new Phrase(allAmount.ToString("0.00"), font2));
                            //訂單總數
                            PdfPCell c5 = new PdfPCell(new Phrase(orderList.Count.ToString(), font2));
                            //已收金額
                            PdfPCell c6 = new PdfPCell(new Phrase(allPaid.ToString("0.00"), font2));
                            //欠款金額
                            PdfPCell c7 = new PdfPCell(new Phrase(allDebt.ToString("0.00"), font2));
                            //預存餘額
                            PdfPCell c8 = new PdfPCell(new Phrase(allBalance.ToString("0.00"), font2));

                            //設置每個單元格所佔列數
                            c1.Colspan = 1;
                            //c2.Colspan = 2;
                            c3.Colspan = 5;
                            c4.Colspan = 1;
                            c5.Colspan = 1;
                            c6.Colspan = 1;

                            c7.Colspan = 1;
                            c8.Colspan = 1;

                            //居中
                            c1.HorizontalAlignment = aligncenter;
                            //c2.HorizontalAlignment = aligncenter;
                            c3.HorizontalAlignment = aligncenter;
                            c4.HorizontalAlignment = aligncenter;
                            c5.HorizontalAlignment = aligncenter;
                            c6.HorizontalAlignment = aligncenter;

                            c7.HorizontalAlignment = aligncenter;
                            c8.HorizontalAlignment = aligncenter;

                            //添加單元格到表格中
                            table.AddCell(c1);
                            //table.AddCell(c2);
                            table.AddCell(c3);
                            table.AddCell(c4);
                            table.AddCell(c5);
                            table.AddCell(c6);

                            table.AddCell(c7);
                            table.AddCell(c8);

                            rownum++;
                        }
                    }
                    if (countflag == 1)
                    {
                        //添加表頭文字this.WebSiteSet.Company_Name
                        //Paragraph Title1 = new Paragraph("廈門晨輝快印", font);
                        Paragraph Title1 = new Paragraph(this.WebSiteSet.Company_Name, font);
                        Paragraph Title2 = new Paragraph("_______________________________", font);
                        //this.WebSiteSet.Company_Tel400
                        //this.WebSiteSet.Company_Telephone
                        //this.WebSiteSet.Company_MobilePhone
                        string tel = "";
                        if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_Tel400))
                        {
                            tel = this.WebSiteSet.Company_Tel400;
                        }
                        else if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_Telephone))
                        {
                            tel = this.WebSiteSet.Company_Telephone;
                        }
                        else if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_MobilePhone))
                        {
                            tel = this.WebSiteSet.Company_MobilePhone;
                        }

                        Paragraph Title3 = new Paragraph("全國免費服務電話:" + tel, font);
                        Paragraph Title4 = new Paragraph("時間" + startTime + " ~ " + endTime, font);

                        //設置居中  
                        Title1.Alignment = aligncenter;
                        Title2.Alignment = aligncenter;
                        Title3.Alignment = aligncenter;
                        Title4.Alignment = aligncenter;

                        //空一行  
                        Paragraph nullp = new Paragraph(" ", font);
                        nullp.Leading = 10;

                        //將標題段加入PDF文檔中 
                        document.Add(Title1);
                        document.Add(Title2);
                        document.Add(Title3);
                        document.Add(Title4);
                        document.Add(nullp);
                    }

                    document.Add(table);//table end 

                    table = null;
                    countflag++;
                #endregion
                }
                #endregion
                document.NewPage();
            }
            document.Close();


            PDFToLocal(PdfPath, docName);
        }



        ///<summary>
        ///業務員單據模板2(不含訂單明細)
        ///PDFType 1:對賬單  2:欠款單
        ///</summary>
        private void PDFEmployee2(int PDFType)
        {
            #region PDF設置
            string PdfPath = Server.MapPath("/Upload/Temp/");
            string docType = "";
            if (PDFType == 1)
            {
                docType = "業務員對賬單二";
            }
            else if (PDFType == 2)
            {
                docType = "業務員欠款單";
            }
            string docName = string.Format("{0}{1:yyyyMMddHHmmssffff}", docType, DateTime.Now);
            //設置中文字體
            //BaseFont bfHei = BaseFont.CreateFont(@"C:\Windows\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font font = new iTextSharp.text.Font(bfHei, 11);
            iTextSharp.text.Font font2 = new iTextSharp.text.Font(bfHei, 10);
            //設置居中
            int aligncenter = iTextSharp.text.Rectangle.ALIGN_CENTER;

            //設置紙張尺寸
            int pagenum = int.Parse(this.ddlPaperSize.SelectedValue);
            iTextSharp.text.Rectangle pagesize = PageSize.A4;
            switch (pagenum)
            {
                case 0: pagesize = PageSize.A0; break;
                case 1: pagesize = PageSize.A1; break;
                case 2: pagesize = PageSize.A2; break;
                case 3: pagesize = PageSize.A3; break;
                case 4: pagesize = PageSize.A4; break;
                case 5: pagesize = PageSize.A5; break;
            }
            iTextSharp.text.Document document = new iTextSharp.text.Document(pagesize);


            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PdfPath + docName + ".pdf", FileMode.Create));

            #endregion

            #region 數據來源
            //獲取數據
            StringBuilder builder = new StringBuilder();

            //付款狀態
            if (PDFType == 2)
            {
                if (builder.Length > 1)
                {
                    builder.Append(" and ");
                }
                builder.Append(" PaymentStatus = 0 ");
            }
            if (PageValidate.IsDateTime(this.txtCreatedDateEnd.Text.Trim()) && PageValidate.IsDateTime(this.txtCreatedDateStart.Text.Trim()))
            {
                if (builder.Length > 1)
                {
                    builder.Append(" and ");
                }
                builder.AppendFormat(" CreatedDate between  '{0}' and  '{1}' ", InjectionFilter.QuoteFilter(this.txtCreatedDateStart.Text.Trim()), InjectionFilter.QuoteFilter(this.txtCreatedDateEnd.Text.Trim()));
            }
            //值查看印刷訂單
            if (builder.Length > 1)
            {
                builder.Append(" and ");
            }
            builder.AppendFormat(" OrderType in ({0},{1}) ", (int)V5print.Model.Shop.Order.EnumHelper.OrderItemType.Print, (int)V5print.Model.Shop.Order.EnumHelper.OrderItemType.Template);
            //已發貨的訂單
            if (builder.Length > 1)
            {
                builder.Append(" and ");
            }
            builder.Append(" ShippingStatus>1 ");
            DataSet dataSetSource = this.orderBll.GetListWithEmployee(-1, builder.ToString(), "CreatedDate desc");

            DateTime orderStartTime = new DateTime();
            DateTime orderEndTime = new DateTime();
            int TimeCountFlag = 0;
            //BuyerID去重、EmployeeID去重
            HashSet<int> BuyerIDs = new HashSet<int>();
            HashSet<int> EmployeeIDs = new HashSet<int>();
            foreach (DataRow row0 in dataSetSource.Tables[0].Rows)
            {
                TimeCountFlag++;
                if (TimeCountFlag == 1)
                {
                    orderEndTime = row0.Field<DateTime>("CreatedDate");
                }
                if (TimeCountFlag == dataSetSource.Tables[0].Rows.Count)
                {
                    orderStartTime = row0.Field<DateTime>("CreatedDate");
                }
                BuyerIDs.Add(row0.Field<int>("BuyerID"));
                EmployeeIDs.Add(row0.Field<int>("UserID"));
            }

            string startTime = "";
            string endTime = "";
            if (PageValidate.IsDateTime(this.txtCreatedDateEnd.Text.Trim()) && PageValidate.IsDateTime(this.txtCreatedDateStart.Text.Trim()))
            {
                startTime = this.txtCreatedDateEnd.Text.Trim();
                endTime = this.txtCreatedDateStart.Text.Trim();
            }
            else
            {
                startTime = orderStartTime.ToString("yyyy-MM-dd hh:mm:ss");
                endTime = orderEndTime.ToString("yyyy-MM-dd hh:mm:ss");
            }
            #endregion


            HashSet<int> hs = new HashSet<int>();
            //按業務員分頁
            hs = EmployeeIDs;

            if (hs.Count == 0)
            {
                MessageBox.ShowFailTip(this, "抱歉,暫無數據!");
                return;
            }
            else
            {
                document.Open();
            }

            foreach (var hsNow in hs)
            {
                #region EmployeeID
                int countflag = 1;
                StringBuilder sbBuyerID = new StringBuilder();

                //按業務員分頁
                sbBuyerID.AppendFormat(" and p3.UserID={0} ", hsNow.ToString());
                DataSet dataSetSource2 = this.orderBll.GetListWithEmployee(-1, builder.ToString() + sbBuyerID.ToString(), "CreatedDate desc");


                //彙總數據
                //交易總額
                decimal AllPageAmount = 0;
                //訂單總數
                int AllPageOrderCount = 0;
                //已收金額
                decimal AllPagePaid = 0;
                //欠款金額
                decimal AllPageDebt = 0;
                //預存餘額
                decimal AllPageBalance = 0;



                //該業務員的所有客戶(去重)
                HashSet<int> Buyergroup = new HashSet<int>();
                foreach (DataRow row3 in dataSetSource2.Tables[0].Rows)
                {
                    Buyergroup.Add(row3.Field<int>("BuyerID"));

                }


                #region 打印數據
                int rowCount = 1;

                iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(11);//11列
                table.TotalWidth = _tablewidth;
                table.LockedWidth = false;

                #region 表頭
                if (countflag == 1)
                {
                    //按業務員分頁
                    V5print.Model.Members.Users employNow = userBll.GetModel(hsNow);
                    string employeeName = "";
                    if (employNow != null) 
                    {
                        employeeName = employNow.UserName;
                    }

                    PdfPCell header21 = new PdfPCell(new Phrase("業務員:" + employeeName, font));
                    header21.Colspan = 11;
                    table.AddCell(header21);

                    PdfPCell header9 = new PdfPCell(new Phrase("訂單詳情", font2));
                    header9.Colspan = 11;
                    header9.HorizontalAlignment = aligncenter;
                    table.AddCell(header9);

                    PdfPCell t1 = new PdfPCell(new Phrase("序號", font2));
                    //PdfPCell t2 = new PdfPCell(new Phrase("訂單號", font2));
                    PdfPCell t3 = new PdfPCell(new Phrase("客戶名稱", font2));
                    PdfPCell t4 = new PdfPCell(new Phrase("交易總額", font2));
                    PdfPCell t5 = new PdfPCell(new Phrase("訂單總數", font2));
                    PdfPCell t6 = new PdfPCell(new Phrase("已收金額", font2));
                    PdfPCell t7 = new PdfPCell(new Phrase("欠款金額", font2));
                    PdfPCell t8 = new PdfPCell(new Phrase("預存餘額", font2));

                    //設置每個單元格所佔列數
                    t1.Colspan = 1;
                    //t2.Colspan = 2;
                    t3.Colspan = 5;
                    t4.Colspan = 1;
                    t5.Colspan = 1;
                    t6.Colspan = 1;
                    t7.Colspan = 1;
                    t8.Colspan = 1;

                    //設置字體居中
                    t1.HorizontalAlignment = aligncenter;
                    //t2.HorizontalAlignment = aligncenter;
                    t3.HorizontalAlignment = aligncenter;
                    t4.HorizontalAlignment = aligncenter;
                    t5.HorizontalAlignment = aligncenter;
                    t6.HorizontalAlignment = aligncenter;
                    t7.HorizontalAlignment = aligncenter;
                    t8.HorizontalAlignment = aligncenter;

                    //添加單元格到表格中
                    table.AddCell(t1);
                    //table.AddCell(t2);
                    table.AddCell(t3);
                    table.AddCell(t4);
                    table.AddCell(t5);
                    table.AddCell(t6);
                    table.AddCell(t7);
                    table.AddCell(t8);
                }
                #endregion


                foreach (var BuyerIdNow in Buyergroup)
                {

                    int rownum = 1;
                    PdfPCell c1 = new PdfPCell(new Phrase(rowCount.ToString(), font2));
                    rowCount++;
                    //訂單號
                    //PdfPCell c2 = new PdfPCell(new Phrase(row2.Field<string>("OrderCode"), font2));

                    Phrase c3Cell = new Phrase();
                    //客戶名稱
                    V5print.Model.Members.Users c3user = userBll.GetModel(BuyerIdNow);
                    string c3UserNickName = c3user.NickName;
                    c3Cell = new Phrase(c3UserNickName, font2);
                    PdfPCell c3 = new PdfPCell(c3Cell);


                    //string sql = string.Format(" BuyerID={0} and PaymentStatus=0 and PaymentGateway='credit'", c3user.UserID);
                    string sql = string.Format(" and BuyerID={0} ", c3user.UserID);
                    List<V5print.Model.Shop.Order.OrderInfo> orderList = orderBll.GetModelList(builder.ToString() + sql);
                    //交易總額
                    decimal allAmount = 0;
                    //已收金額
                    decimal allPaid = 0;

                    if (orderList.Count > 0)
                    {
                        foreach (V5print.Model.Shop.Order.OrderInfo order in orderList)
                        {
                            allAmount += order.Amount;
                            //已收金額
                            if (order.PaymentStatus == (int)V5print.Model.Shop.Order.EnumHelper.PaymentStatus.Paid)
                            {
                                allPaid += order.Amount;
                            }


                        }
                    }
                    //欠款金額
                    decimal allDebt = allAmount - allPaid;
                    //預存餘額
                    decimal allBalance = 0;

                    V5print.Model.Members.UsersExpModel userexp = userexpBll.GetUsersExpModel(c3user.UserID);
                    if (userexp != null)
                    {
                        allBalance = Math.Round(decimal.Parse(userexp.Balance.ToString()), 2);
                    }



                    //彙總數據
                    AllPageAmount += allAmount;
                    AllPageOrderCount += orderList.Count;
                    AllPagePaid += allPaid;
                    AllPageDebt += allDebt;
                    AllPageBalance += allBalance;




                    //交易總額
                    PdfPCell c4 = new PdfPCell(new Phrase(allAmount.ToString("0.00"), font2));
                    //訂單總數
                    PdfPCell c5 = new PdfPCell(new Phrase(orderList.Count.ToString(), font2));
                    //已收金額
                    PdfPCell c6 = new PdfPCell(new Phrase(allPaid.ToString("0.00"), font2));
                    //欠款金額
                    PdfPCell c7 = new PdfPCell(new Phrase(allDebt.ToString("0.00"), font2));
                    //預存餘額
                    PdfPCell c8 = new PdfPCell(new Phrase(allBalance.ToString("0.00"), font2));

                    //設置每個單元格所佔列數
                    c1.Colspan = 1;
                    //c2.Colspan = 2;
                    c3.Colspan = 5;
                    c4.Colspan = 1;
                    c5.Colspan = 1;
                    c6.Colspan = 1;

                    c7.Colspan = 1;
                    c8.Colspan = 1;

                    //居中
                    c1.HorizontalAlignment = aligncenter;
                    //c2.HorizontalAlignment = aligncenter;
                    c3.HorizontalAlignment = aligncenter;
                    c4.HorizontalAlignment = aligncenter;
                    c5.HorizontalAlignment = aligncenter;
                    c6.HorizontalAlignment = aligncenter;

                    c7.HorizontalAlignment = aligncenter;
                    c8.HorizontalAlignment = aligncenter;

                    //添加單元格到表格中
                    table.AddCell(c1);
                    //table.AddCell(c2);
                    table.AddCell(c3);
                    table.AddCell(c4);
                    table.AddCell(c5);
                    table.AddCell(c6);

                    table.AddCell(c7);
                    table.AddCell(c8);

                    rownum++;

                }

                #region 表頭文字
                if (countflag == 1)
                {
                    Paragraph Title1 = new Paragraph(this.WebSiteSet.Company_Name, font);
                    Paragraph Title2 = new Paragraph("_______________________________", font);
                    string tel = "";
                    if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_Tel400))
                    {
                        tel = this.WebSiteSet.Company_Tel400;
                    }
                    else if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_Telephone))
                    {
                        tel = this.WebSiteSet.Company_Telephone;
                    }
                    else if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_MobilePhone))
                    {
                        tel = this.WebSiteSet.Company_MobilePhone;
                    }

                    Paragraph Title3 = new Paragraph("全國免費服務電話:" + tel, font);
                    Paragraph Title4 = new Paragraph("時間" + startTime + " ~ " + endTime, font);

                    //設置居中  
                    Title1.Alignment = aligncenter;
                    Title2.Alignment = aligncenter;
                    Title3.Alignment = aligncenter;
                    Title4.Alignment = aligncenter;

                    //空一行  
                    Paragraph nullp = new Paragraph(" ", font);
                    nullp.Leading = 10;

                    //將標題段加入PDF文檔中 
                    document.Add(Title1);
                    document.Add(Title2);
                    document.Add(Title3);
                    document.Add(Title4);
                    document.Add(nullp);
                }
                #endregion
                document.Add(table);//table end 

                table = null;
                countflag++;

                #endregion

                #endregion



                //彙總數據
                iTextSharp.text.pdf.PdfPTable tableGather = new iTextSharp.text.pdf.PdfPTable(11);//11列
                tableGather.TotalWidth = _tablewidth;
                tableGather.LockedWidth = false;
                PdfPCell gather1 = new PdfPCell(new Phrase("總計", font));
                PdfPCell gather2 = new PdfPCell(new Phrase(AllPageAmount.ToString("0.00"), font));
                PdfPCell gather3 = new PdfPCell(new Phrase(AllPageOrderCount.ToString("0"), font));
                PdfPCell gather4 = new PdfPCell(new Phrase(AllPagePaid.ToString("0.00"), font));
                PdfPCell gather5 = new PdfPCell(new Phrase(AllPageDebt.ToString("0.00"), font));
                PdfPCell gather6 = new PdfPCell(new Phrase(AllPageBalance.ToString("0.00"), font));

                gather1.Colspan = 6;
                gather2.Colspan = 1;
                gather3.Colspan = 1;
                gather4.Colspan = 1;
                gather5.Colspan = 1;
                gather6.Colspan = 1;

                gather1.HorizontalAlignment = iTextSharp.text.Rectangle.ALIGN_RIGHT;
                gather2.HorizontalAlignment = aligncenter;
                gather3.HorizontalAlignment = aligncenter;
                gather4.HorizontalAlignment = aligncenter;
                gather5.HorizontalAlignment = aligncenter;
                gather6.HorizontalAlignment = aligncenter;

                tableGather.AddCell(gather1);
                tableGather.AddCell(gather2);
                tableGather.AddCell(gather3);
                tableGather.AddCell(gather4);
                tableGather.AddCell(gather5);
                tableGather.AddCell(gather6);

                document.Add(tableGather);




                document.NewPage();
            }
            if (hs.Count == 0)
            {
                MessageBox.ShowFailTip(this, "抱歉,暫無數據!");
                return;
            }
            else
            {
                document.Close();
            }

            PDFToLocal(PdfPath, docName);
        }



        //業務員對賬單二(不含訂單明細)
        private void PrintPDFEmployeeDetail2()
        {
            this.PDFEmployee2(1);
        }

        //業務員欠款單(不含訂單明細)
        //不含訂單明細,含未付款,按業務員分頁
        private void PrintPDFEmployeeDebt()
        {
            this.PDFEmployee2(2);
        }









        //業務員對賬單一
        //含訂單明細,含已付款、未付款、客戶,按業務員分頁
        private void PrintPDFEmployeeDetail1()
        {
            #region pdf設置
            string PdfPath = Server.MapPath("/Upload/Temp/");
            string docName = string.Format("業務員對賬單一{0:yyyyMMddHHmmssffff}", DateTime.Now);
            //設置中文字體
            //BaseFont bfHei = BaseFont.CreateFont(@"C:\Windows\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font font = new iTextSharp.text.Font(bfHei, 11);
            iTextSharp.text.Font font2 = new iTextSharp.text.Font(bfHei, 10);
            //設置居中
            int aligncenter = iTextSharp.text.Rectangle.ALIGN_CENTER;

            //設置紙張尺寸
            int pagenum = int.Parse(this.ddlPaperSize.SelectedValue);
            iTextSharp.text.Rectangle pagesize = PageSize.A4;
            switch (pagenum)
            {
                case 0: pagesize = PageSize.A0; break;
                case 1: pagesize = PageSize.A1; break;
                case 2: pagesize = PageSize.A2; break;
                case 3: pagesize = PageSize.A3; break;
                case 4: pagesize = PageSize.A4; break;
                case 5: pagesize = PageSize.A5; break;
            }
            iTextSharp.text.Document document = new iTextSharp.text.Document(pagesize);
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PdfPath + docName + ".pdf", FileMode.Create));

            #endregion

            #region 數據來源
            //獲取數據
            StringBuilder builder = new StringBuilder();
            if (PageValidate.IsDateTime(this.txtCreatedDateEnd.Text.Trim()) && PageValidate.IsDateTime(this.txtCreatedDateStart.Text.Trim()))
            {
                if (builder.Length > 1)
                {
                    builder.Append(" and ");
                }
                builder.AppendFormat(" CreatedDate between  '{0}' and  '{1}' ", InjectionFilter.QuoteFilter(this.txtCreatedDateStart.Text.Trim()), InjectionFilter.QuoteFilter(this.txtCreatedDateEnd.Text.Trim()));
            }
            //值查看印刷訂單
            if (builder.Length > 1)
            {
                builder.Append(" and ");
            }
            builder.AppendFormat(" OrderType in ({0},{1}) ", (int)V5print.Model.Shop.Order.EnumHelper.OrderItemType.Print, (int)V5print.Model.Shop.Order.EnumHelper.OrderItemType.Template);
            //已發貨的訂單
            if (builder.Length > 1)
            {
                builder.Append(" and ");
            }
            builder.Append(" ShippingStatus>1 ");
            DataSet dataSetSource = this.orderBll.GetListWithEmployee(-1, builder.ToString(), "CreatedDate desc");

            DateTime orderStartTime = new DateTime();
            DateTime orderEndTime = new DateTime();
            int TimeCountFlag = 0;
            //BuyerID去重、EmployeeID去重
            HashSet<int> BuyerIDs = new HashSet<int>();
            HashSet<int> EmployeeIDs = new HashSet<int>();
            foreach (DataRow row0 in dataSetSource.Tables[0].Rows)
            {
                TimeCountFlag++;
                if (TimeCountFlag == 1)
                {
                    orderEndTime = row0.Field<DateTime>("CreatedDate");
                }
                if (TimeCountFlag == dataSetSource.Tables[0].Rows.Count)
                {
                    orderStartTime = row0.Field<DateTime>("CreatedDate");
                }
                BuyerIDs.Add(row0.Field<int>("BuyerID"));
                EmployeeIDs.Add(row0.Field<int>("UserID"));
            }

            string startTime = "";
            string endTime = "";
            if (PageValidate.IsDateTime(this.txtCreatedDateEnd.Text.Trim()) && PageValidate.IsDateTime(this.txtCreatedDateStart.Text.Trim()))
            {
                startTime = this.txtCreatedDateEnd.Text.Trim();
                endTime = this.txtCreatedDateStart.Text.Trim();
            }
            else
            {
                startTime = orderStartTime.ToString("yyyy-MM-dd hh:mm:ss");
                endTime = orderEndTime.ToString("yyyy-MM-dd hh:mm:ss");
            }
            #endregion

            //this.BindData();
            //DataSet dataSetSource = this.gridView.DataSetSource;
            HashSet<int> hs = new HashSet<int>();
            //按業務員分頁
            hs = EmployeeIDs;
            if (hs.Count == 0)
            {
                MessageBox.ShowFailTip(this, "抱歉,暫無數據!");
                return;
            }
            else
            {
                document.Open();
            }
            foreach (var hsNow in hs)
            {
                #region EmployeeID
                int countflag = 1;
                StringBuilder sbEmployeeID = new StringBuilder();
                //按業務員分頁
                sbEmployeeID.AppendFormat(" and p3.UserID={0} ", hsNow.ToString());
                DataSet dataSetSource2 = this.orderBll.GetListWithEmployee(-1, builder.ToString() + sbEmployeeID.ToString(), "CreatedDate desc");


                //彙總數據
                //總金額
                decimal AllPageAmount = 0;
                //總已付款
                decimal AllPagePaid = 0;
                //總欠款
                decimal AllPageDebt = 0;



                HashSet<int> BuyerGroup = new HashSet<int>();
                foreach (DataRow row1 in dataSetSource2.Tables[0].Rows)
                {
                    BuyerGroup.Add(row1.Field<int>("BuyerID"));
                }
                foreach (var Buyer in BuyerGroup)
                {
                    #region Buyer
                    StringBuilder sbBuyer = new StringBuilder();
                    sbBuyer.AppendFormat(" and BuyerID={0} ", Buyer.ToString());
                    DataSet dataSetSource3 = this.orderBll.GetListWithEmployee(-1, builder.ToString() + sbEmployeeID.ToString() + sbBuyer.ToString(), "CreatedDate desc");

                    #region 打印數據
                    int rowCount = 1;

                    iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(11);//11列
                    table.TotalWidth = _tablewidth;
                    table.LockedWidth = false;

                    foreach (DataRow row2 in dataSetSource3.Tables[0].Rows)
                    {
                        #region 表頭
                        if (countflag == 1)
                        {
                            PdfPCell header21 = new PdfPCell(new Phrase("業務員:" + row2.Field<String>("UserName"), font));
                            header21.Colspan = 11;
                            table.AddCell(header21);

                            PdfPCell header9 = new PdfPCell(new Phrase("訂單詳情", font2));
                            header9.Colspan = 11;
                            header9.HorizontalAlignment = aligncenter;
                            table.AddCell(header9);

                            PdfPCell t1 = new PdfPCell(new Phrase("客戶", font2));
                            PdfPCell t2 = new PdfPCell(new Phrase("訂單號", font2));
                            PdfPCell t3 = new PdfPCell(new Phrase("產品名稱", font2));
                            PdfPCell t4 = new PdfPCell(new Phrase("產品類型", font2));
                            PdfPCell t5 = new PdfPCell(new Phrase("數量", font2));
                            PdfPCell t9 = new PdfPCell(new Phrase("款數", font2));
                            PdfPCell t6 = new PdfPCell(new Phrase("總金額", font2));
                            PdfPCell t7 = new PdfPCell(new Phrase("已付款", font2));
                            PdfPCell t8 = new PdfPCell(new Phrase("未付款", font2));

                            //設置每個單元格所佔列數
                            t1.Colspan = 1;
                            t2.Colspan = 2;
                            t3.Colspan = 2;
                            t4.Colspan = 1;
                            t5.Colspan = 1;
                            t6.Colspan = 1;
                            t7.Colspan = 1;
                            t8.Colspan = 1;
                            t9.Colspan = 1;

                            //設置字體居中
                            t1.HorizontalAlignment = aligncenter;
                            t2.HorizontalAlignment = aligncenter;
                            t3.HorizontalAlignment = aligncenter;
                            t4.HorizontalAlignment = aligncenter;
                            t5.HorizontalAlignment = aligncenter;
                            t6.HorizontalAlignment = aligncenter;
                            t7.HorizontalAlignment = aligncenter;
                            t8.HorizontalAlignment = aligncenter;
                            t9.HorizontalAlignment = aligncenter;

                            //添加單元格到表格中
                            table.AddCell(t1);
                            table.AddCell(t2);
                            table.AddCell(t3);
                            table.AddCell(t4);
                            table.AddCell(t5);
                            table.AddCell(t9);
                            table.AddCell(t6);
                            table.AddCell(t7);
                            table.AddCell(t8);
                        }
                        #endregion


                        #region 表體數據
                        List<V5print.Model.Shop.Order.OrderItems> modelListByCache = this.orderItemManage.GetModelListByCache(" OrderId=" + row2.Field<long>("OrderId"));
                        if ((modelListByCache != null) && (modelListByCache.Count > 0))
                        {
                            int rownum = 1;
                            foreach (V5print.Model.Shop.Order.OrderItems info in modelListByCache)
                            {
                                if (rowCount == 1) 
                                {
                                    //客戶名稱
                                    V5print.Model.Members.Users user = userBll.GetModel(row2.Field<int>("BuyerId"));
                                    string UserNickName = user.NickName;
                                    PdfPCell c1 = new PdfPCell(new Phrase(UserNickName, font2));
                                    c1.Rowspan = dataSetSource3.Tables[0].Rows.Count;
                                    c1.Colspan = 1;
                                    c1.HorizontalAlignment = aligncenter;
                                    c1.VerticalAlignment = aligncenter;
                                    table.AddCell(c1);

                                    rowCount++;
                                }

                                //訂單號
                                PdfPCell c2 = new PdfPCell(new Phrase(row2.Field<string>("OrderCode"), font2));

                                Phrase c3Cell = new Phrase();
                                //交易內容
                                string AttributeNames = "";
                                if (info.TypeId == 14 || info.TypeId == 13)
                                {
                                    AttributeNames = info.AttributeNames.Split(',')[0];
                                }
                                else if (info.TypeId == 12)
                                {
                                    AttributeNames = "樣本";
                                }

                                c3Cell = new Phrase(AttributeNames, font2);


                                PdfPCell c3 = new PdfPCell(c3Cell);
                                //產品類型
                                PdfPCell c4 = new PdfPCell(new Phrase(new V5print.BLL.Shop.Products.ProductType().GetModelByCache(info.TypeId).TypeName, font2));
                                //數量
                                PdfPCell c5 = new PdfPCell(new Phrase(info.Quantity.ToString() + info.Unit, font2));
                                //款數
                                string ModelCount = "1";
                                if (row2.Field<Int32>("ModelCount") != null)
                                {
                                    ModelCount = row2.Field<Int32>("ModelCount").ToString();
                                }
                                PdfPCell c9 = new PdfPCell(new Phrase(ModelCount + "款", font2));
                                //總金額
                                PdfPCell c6 = new PdfPCell(new Phrase(info.Amount.ToString("F"), font2));

                                //已付款和欠款
                                PdfPCell c7 = new PdfPCell();
                                PdfPCell c8 = new PdfPCell();
                                int PaymentStatus = int.Parse(row2.Field<Int16>("PaymentStatus").ToString());
                                if (PaymentStatus == (int)V5print.Model.Shop.Order.EnumHelper.PaymentStatus.Paid)
                                {
                                    //已支付
                                    c7 = new PdfPCell(new Phrase(info.Amount.ToString("F"), font2));
                                    c8 = new PdfPCell(new Phrase("0", font2));
                                    AllPagePaid += info.Amount;
                                }
                                else
                                {
                                    c7 = new PdfPCell(new Phrase("0", font2));
                                    c8 = new PdfPCell(new Phrase(info.Amount.ToString("F"), font2));
                                    AllPageDebt += info.Amount;
                                }

                                AllPageAmount += info.Amount;


                                //設置每個單元格所佔列數

                                c2.Colspan = 2;
                                c3.Colspan = 2;
                                c4.Colspan = 1;
                                c5.Colspan = 1;
                                c6.Colspan = 1;
                                c7.Colspan = 1;
                                c8.Colspan = 1;
                                c9.Colspan = 1;

                                //居中

                                c2.HorizontalAlignment = aligncenter;
                                c3.HorizontalAlignment = aligncenter;
                                c4.HorizontalAlignment = aligncenter;
                                c5.HorizontalAlignment = aligncenter;
                                c6.HorizontalAlignment = aligncenter;
                                c7.HorizontalAlignment = aligncenter;
                                c8.HorizontalAlignment = aligncenter;
                                c9.HorizontalAlignment = aligncenter;

                                //添加單元格到表格中

                                table.AddCell(c2);
                                table.AddCell(c3);
                                table.AddCell(c4);
                                table.AddCell(c5);
                                table.AddCell(c9);
                                table.AddCell(c6);
                                table.AddCell(c7);
                                table.AddCell(c8);


                                rownum++;
                            }
                        }

                        #endregion


                        #region 表頭文字
                        if (countflag == 1)
                        {
                            Paragraph Title1 = new Paragraph(this.WebSiteSet.Company_Name, font);
                            Paragraph Title2 = new Paragraph("_______________________________", font);
                            string tel = "";
                            if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_Tel400))
                            {
                                tel = this.WebSiteSet.Company_Tel400;
                            }
                            else if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_Telephone))
                            {
                                tel = this.WebSiteSet.Company_Telephone;
                            }
                            else if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_MobilePhone))
                            {
                                tel = this.WebSiteSet.Company_MobilePhone;
                            }

                            Paragraph Title3 = new Paragraph("全國免費服務電話:" + tel, font);
                            Paragraph Title4 = new Paragraph("時間" + startTime + " ~ " + endTime, font);

                            //設置居中  
                            Title1.Alignment = aligncenter;
                            Title2.Alignment = aligncenter;
                            Title3.Alignment = aligncenter;
                            Title4.Alignment = aligncenter;


                            //空一行  
                            Paragraph nullp = new Paragraph(" ", font);
                            nullp.Leading = 10;

                            //將標題段加入PDF文檔中 
                            document.Add(Title1);
                            document.Add(Title2);
                            document.Add(Title3);
                            document.Add(Title4);
                            document.Add(nullp);
                        }
                        #endregion

                        countflag++;
                    }

                    #endregion
                    #endregion

                    document.Add(table);//table end 
                    table = null;

                #region 打印數據廢棄
                //int rowCount = 1;
                //foreach (DataRow row2 in dataSetSource2.Tables[0].Rows)
                //{

                //    iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(11);//11列
                //    table.TotalWidth = 400f;
                //    table.LockedWidth = true;

                //    if (countflag == 1)
                //    {
                //        PdfPCell header21 = new PdfPCell(new Phrase("業務員:" + row2.Field<String>("UserName"), font));
                //        header21.Colspan = 11;
                //        table.AddCell(header21);

                //        PdfPCell header9 = new PdfPCell(new Phrase("訂單詳情", font2));
                //        header9.Colspan = 11;
                //        header9.HorizontalAlignment = aligncenter;
                //        table.AddCell(header9);



                //        PdfPCell t1 = new PdfPCell(new Phrase("客戶", font2));
                //        PdfPCell t2 = new PdfPCell(new Phrase("訂單號", font2));
                //        PdfPCell t3 = new PdfPCell(new Phrase("產品名稱", font2));
                //        PdfPCell t4 = new PdfPCell(new Phrase("產品類型", font2));
                //        PdfPCell t5 = new PdfPCell(new Phrase("數量", font2));
                //        PdfPCell t6 = new PdfPCell(new Phrase("總金額", font2));
                //        PdfPCell t7 = new PdfPCell(new Phrase("已付款", font2));
                //        PdfPCell t8 = new PdfPCell(new Phrase("未付款", font2));

                //        //設置每個單元格所佔列數
                //        t1.Colspan = 1;
                //        t2.Colspan = 2;
                //        t3.Colspan = 3;
                //        t4.Colspan = 1;
                //        t5.Colspan = 1;
                //        t6.Colspan = 1;
                //        t7.Colspan = 1;
                //        t8.Colspan = 1;

                //        //設置字體居中
                //        t1.HorizontalAlignment = aligncenter;
                //        t2.HorizontalAlignment = aligncenter;
                //        t3.HorizontalAlignment = aligncenter;
                //        t4.HorizontalAlignment = aligncenter;
                //        t5.HorizontalAlignment = aligncenter;
                //        t6.HorizontalAlignment = aligncenter;
                //        t7.HorizontalAlignment = aligncenter;
                //        t8.HorizontalAlignment = aligncenter;

                //        //添加單元格到表格中
                //        table.AddCell(t1);
                //        table.AddCell(t2);
                //        table.AddCell(t3);
                //        table.AddCell(t4);
                //        table.AddCell(t5);
                //        table.AddCell(t6);
                //        table.AddCell(t7);
                //        table.AddCell(t8);
                //    }



                //    List<V5print.Model.Shop.Order.OrderItems> modelListByCache = this.orderItemManage.GetModelListByCache(" OrderId=" + row2.Field<long>("OrderId"));
                //    if ((modelListByCache != null) && (modelListByCache.Count > 0))
                //    {
                //        int rownum = 1;
                //        foreach (V5print.Model.Shop.Order.OrderItems info in modelListByCache)
                //        {
                //            //客戶名稱
                //            V5print.Model.Members.Users user = userBll.GetModel(row2.Field<int>("BuyerId"));
                //            string UserNickName = user.NickName;
                //            PdfPCell c1 = new PdfPCell(new Phrase(UserNickName, font2));

                //            //List<V5print.Model.Shop.Order.OrderItems> CurUserOrders = this.orderItemManage.GetModelListByCache(" BuyerId=" + row2.Field<int>("BuyerId"));
                //            //c1.Rowspan = 3;

                //            //次序
                //            //PdfPCell c1 = new PdfPCell(new Phrase(rowCount.ToString() + "-" + rownum.ToString(), font2));
                //            //PdfPCell c1 = new PdfPCell(new Phrase(rowCount.ToString(), font2));
                //            rowCount++;
                //            //訂單號
                //            PdfPCell c2 = new PdfPCell(new Phrase(row2.Field<string>("OrderCode"), font2));

                //            Phrase c3Cell = new Phrase();
                //            //交易內容
                //            string AttributeNames = "";
                //            if (info.TypeId == 14 || info.TypeId == 13)
                //            {
                //                AttributeNames = info.AttributeNames.Split(',')[0];
                //            }
                //            else if (info.TypeId == 12)
                //            {
                //                AttributeNames = "樣本";
                //            }

                //            c3Cell = new Phrase(AttributeNames, font2);


                //            PdfPCell c3 = new PdfPCell(c3Cell);
                //            //產品類型
                //            PdfPCell c4 = new PdfPCell(new Phrase(new V5print.BLL.Shop.Products.ProductType().GetModelByCache(info.TypeId).TypeName, font2));
                //            //數量
                //            PdfPCell c5 = new PdfPCell(new Phrase(info.Quantity.ToString(), font2));
                //            //總金額
                //            PdfPCell c6 = new PdfPCell(new Phrase(info.Amount.ToString("F"), font2));

                //            //已付款和欠款
                //            PdfPCell c7 = new PdfPCell();
                //            PdfPCell c8 = new PdfPCell();
                //            int PaymentStatus = int.Parse(row2.Field<Int16>("PaymentStatus").ToString());
                //            if (PaymentStatus == (int)V5print.Model.Shop.Order.EnumHelper.PaymentStatus.Paid)
                //            {
                //                //已支付
                //                c7 = new PdfPCell(new Phrase(info.Amount.ToString("F"), font2));
                //                c8 = new PdfPCell(new Phrase("0", font2));
                //            }
                //            else
                //            {
                //                c7 = new PdfPCell(new Phrase("0", font2));
                //                c8 = new PdfPCell(new Phrase(info.Amount.ToString("F"), font2));
                //            }


                //            //設置每個單元格所佔列數
                //            c1.Colspan = 1;
                //            c2.Colspan = 2;
                //            c3.Colspan = 3;
                //            c4.Colspan = 1;
                //            c5.Colspan = 1;
                //            c6.Colspan = 1;
                //            c7.Colspan = 1;
                //            c8.Colspan = 1;

                //            //居中
                //            c1.HorizontalAlignment = aligncenter;
                //            c2.HorizontalAlignment = aligncenter;
                //            c3.HorizontalAlignment = aligncenter;
                //            c4.HorizontalAlignment = aligncenter;
                //            c5.HorizontalAlignment = aligncenter;
                //            c6.HorizontalAlignment = aligncenter;
                //            c7.HorizontalAlignment = aligncenter;
                //            c8.HorizontalAlignment = aligncenter;

                //            //添加單元格到表格中
                //            table.AddCell(c1);
                //            table.AddCell(c2);
                //            table.AddCell(c3);
                //            table.AddCell(c4);
                //            table.AddCell(c5);
                //            table.AddCell(c6);
                //            table.AddCell(c7);
                //            table.AddCell(c8);

                //            rownum++;
                //        }
                //    }
                //    if (countflag == 1)
                //    {
                //        //添加表頭文字
                //        Paragraph Title1 = new Paragraph("廈門晨輝快印", font);
                //        Paragraph Title2 = new Paragraph("_______________________________", font);
                //        Paragraph Title3 = new Paragraph("全國免費服務電話:40081234567", font);
                //        Paragraph Title4 = new Paragraph("時間2015-2016", font);

                //        //設置居中  
                //        Title1.Alignment = aligncenter;
                //        Title2.Alignment = aligncenter;
                //        Title3.Alignment = aligncenter;
                //        Title4.Alignment = aligncenter;


                //        //空一行  
                //        Paragraph nullp = new Paragraph(" ", font);
                //        nullp.Leading = 10;

                //        //將標題段加入PDF文檔中 
                //        document.Add(Title1);
                //        document.Add(Title2);
                //        document.Add(Title3);
                //        document.Add(Title4);
                //        document.Add(nullp);
                //    }

                //    document.Add(table);//table end 

                //    table = null;
                //    countflag++;

                #endregion

                }
                #endregion


                //彙總數據
                iTextSharp.text.pdf.PdfPTable tableGather = new iTextSharp.text.pdf.PdfPTable(11);//11列
                tableGather.TotalWidth = _tablewidth;
                tableGather.LockedWidth = false;
                PdfPCell gather1 = new PdfPCell(new Phrase("總計", font));
                PdfPCell gather2 = new PdfPCell(new Phrase(AllPageAmount.ToString("0.00"), font));
                PdfPCell gather3 = new PdfPCell(new Phrase(AllPagePaid.ToString("0.00"), font));
                PdfPCell gather4 = new PdfPCell(new Phrase(AllPageDebt.ToString("0.00"), font));

                gather1.Colspan = 8;
                gather2.Colspan = 1;
                gather3.Colspan = 1;
                gather4.Colspan = 1;

                gather1.HorizontalAlignment = iTextSharp.text.Rectangle.ALIGN_RIGHT;
                gather2.HorizontalAlignment = aligncenter;
                gather3.HorizontalAlignment = aligncenter;
                gather4.HorizontalAlignment = aligncenter;

                tableGather.AddCell(gather1);
                tableGather.AddCell(gather2);
                tableGather.AddCell(gather3);
                tableGather.AddCell(gather4);
                document.Add(tableGather);



                document.NewPage();
            }
            if (hs.Count == 0)
            {
                MessageBox.ShowFailTip(this, "抱歉,暫無數據!");
                return;
            }
            else
            {
                document.Close();
            }


            PDFToLocal(PdfPath, docName);
        }










        ///<summary>
        ///老闆單據模板一(含客戶)
        ///PDFType 1:對賬單  2:欠款單
        ///</summary>
        private void PDFBoss1(int PDFType)
        {
            #region PDF設置
            string PdfPath = Server.MapPath("/Upload/Temp/");
            string docType = "";
            if (PDFType == 1)
            {
                docType = "老闆對賬單一";
            }
            else if (PDFType == 2)
            {
                docType = "老闆欠款單一";
            }
            string docName = string.Format("{0}{1:yyyyMMddHHmmssffff}", docType, DateTime.Now);
            //設置中文字體
            //BaseFont bfHei = BaseFont.CreateFont(@"C:\Windows\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font font = new iTextSharp.text.Font(bfHei, 11);
            iTextSharp.text.Font font2 = new iTextSharp.text.Font(bfHei, 10);
            //設置居中
            int aligncenter = iTextSharp.text.Rectangle.ALIGN_CENTER;
            //設置紙張尺寸
            int pagenum = int.Parse(this.ddlPaperSize.SelectedValue);
            iTextSharp.text.Rectangle pagesize = PageSize.A4;
            switch (pagenum)
            {
                case 0: pagesize = PageSize.A0; break;
                case 1: pagesize = PageSize.A1; break;
                case 2: pagesize = PageSize.A2; break;
                case 3: pagesize = PageSize.A3; break;
                case 4: pagesize = PageSize.A4; break;
                case 5: pagesize = PageSize.A5; break;
            }
            iTextSharp.text.Document document = new iTextSharp.text.Document(pagesize);
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PdfPath + docName + ".pdf", FileMode.Create));
            document.Open();
            #endregion

            #region 數據來源
            //獲取數據
            StringBuilder builder = new StringBuilder();

            //付款狀態
            if (PDFType == 2)
            {
                if (builder.Length > 1)
                {
                    builder.Append(" and ");
                }
                builder.Append(" PaymentStatus = 0 ");
            }
            if (PageValidate.IsDateTime(this.txtCreatedDateEnd.Text.Trim()) && PageValidate.IsDateTime(this.txtCreatedDateStart.Text.Trim()))
            {
                if (builder.Length > 1)
                {
                    builder.Append(" and ");
                }
                builder.AppendFormat(" CreatedDate between  '{0}' and  '{1}' ", InjectionFilter.QuoteFilter(this.txtCreatedDateStart.Text.Trim()), InjectionFilter.QuoteFilter(this.txtCreatedDateEnd.Text.Trim()));
            }
            //值查看印刷訂單
            if (builder.Length > 1)
            {
                builder.Append(" and ");
            }
            builder.AppendFormat(" OrderType in ({0},{1}) ", (int)V5print.Model.Shop.Order.EnumHelper.OrderItemType.Print, (int)V5print.Model.Shop.Order.EnumHelper.OrderItemType.Template);
            //已發貨的訂單
            if (builder.Length > 1)
            {
                builder.Append(" and ");
            }
            builder.Append(" ShippingStatus>1 ");
            DataSet dataSetSource = this.orderBll.GetListWithEmployee(-1, builder.ToString(), "CreatedDate desc");


            DateTime orderStartTime = new DateTime();
            DateTime orderEndTime = new DateTime();
            int TimeCountFlag = 0;
            //BuyerID去重、EmployeeID去重
            HashSet<int> BuyerIDs = new HashSet<int>();
            HashSet<int> EmployeeIDs = new HashSet<int>();
            foreach (DataRow row0 in dataSetSource.Tables[0].Rows)
            {
                TimeCountFlag++;
                if (TimeCountFlag == 1)
                {
                    orderEndTime = row0.Field<DateTime>("CreatedDate");
                }
                if (TimeCountFlag == dataSetSource.Tables[0].Rows.Count)
                {
                    orderStartTime = row0.Field<DateTime>("CreatedDate");
                }
                BuyerIDs.Add(row0.Field<int>("BuyerID"));
                EmployeeIDs.Add(row0.Field<int>("UserID"));
            }

            string startTime = "";
            string endTime = "";
            if (PageValidate.IsDateTime(this.txtCreatedDateEnd.Text.Trim()) && PageValidate.IsDateTime(this.txtCreatedDateStart.Text.Trim()))
            {
                startTime = this.txtCreatedDateEnd.Text.Trim();
                endTime = this.txtCreatedDateStart.Text.Trim();
            }
            else
            {
                startTime = orderStartTime.ToString("yyyy-MM-dd hh:mm:ss");
                endTime = orderEndTime.ToString("yyyy-MM-dd hh:mm:ss");
            }
            #endregion

            //this.BindData();
            //DataSet dataSetSource = this.gridView.DataSetSource;
            HashSet<int> hs = new HashSet<int>();
            //按業務員不分頁
            hs = EmployeeIDs;
            int countflag = 1;
            #region 表頭文字
            if (countflag == 1)
            {
                Paragraph Title1 = new Paragraph(this.WebSiteSet.Company_Name, font);
                Paragraph Title2 = new Paragraph("_______________________________", font);
                string tel = "";
                if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_Tel400))
                {
                    tel = this.WebSiteSet.Company_Tel400;
                }
                else if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_Telephone))
                {
                    tel = this.WebSiteSet.Company_Telephone;
                }
                else if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_MobilePhone))
                {
                    tel = this.WebSiteSet.Company_MobilePhone;
                }

                Paragraph Title3 = new Paragraph("全國免費服務電話:" + tel, font);
                Paragraph Title4 = new Paragraph("時間" + startTime + " ~ " + endTime, font);

                //設置居中  
                Title1.Alignment = aligncenter;
                Title2.Alignment = aligncenter;
                Title3.Alignment = aligncenter;
                Title4.Alignment = aligncenter;


                //空一行  
                Paragraph nullp = new Paragraph(" ", font);
                nullp.Leading = 10;

                //將標題段加入PDF文檔中 
                document.Add(Title1);
                document.Add(Title2);
                document.Add(Title3);
                document.Add(Title4);
                document.Add(nullp);
            }
            #endregion



            //彙總數據
            //交易總額
            decimal AllPageAmount = 0;
            //訂單總數
            int AllPageOrderCount = 0;
            //已收金額
            decimal AllPagePaid = 0;
            //欠款金額
            decimal AllPageDebt = 0;
            //成本金額
            decimal AllPageCost = 0;
            //利潤金額
            decimal AllPageProfit = 0;



            foreach (var hsNow in hs)
            {
                #region EmployeeID

                StringBuilder sbEmployeeID = new StringBuilder();
                //按業務員分頁
                sbEmployeeID.AppendFormat(" and p3.UserID={0} ", hsNow.ToString());
                DataSet dataSetSource2 = this.orderBll.GetListWithEmployee(-1, builder.ToString() + sbEmployeeID.ToString(), "CreatedDate desc");

                //該業務員的所有客戶(去重)
                HashSet<int> Buyergroup = new HashSet<int>();
                foreach (DataRow row3 in dataSetSource2.Tables[0].Rows)
                {
                    Buyergroup.Add(row3.Field<int>("BuyerID"));
                }

                #region 打印數據

                iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(11);//11列
                table.TotalWidth = _tablewidth;
                table.LockedWidth = false;

                int EndFlag = 0;
                foreach (DataRow row2 in dataSetSource2.Tables[0].Rows)
                {

                    #region 表頭
                    if (countflag == 1)
                    {
                        PdfPCell t1 = new PdfPCell(new Phrase("序號", font2));
                        PdfPCell t2 = new PdfPCell(new Phrase("業務員", font2));
                        PdfPCell t3 = new PdfPCell(new Phrase("客戶名稱", font2));
                        PdfPCell t4 = new PdfPCell(new Phrase("交易總額", font2));
                        PdfPCell t5 = new PdfPCell(new Phrase("訂單總數", font2));
                        PdfPCell t6 = new PdfPCell(new Phrase("已收金額", font2));
                        PdfPCell t7 = new PdfPCell(new Phrase("欠款金額", font2));
                        PdfPCell t8 = new PdfPCell(new Phrase("成本金額", font2));
                        PdfPCell t9 = new PdfPCell(new Phrase("利潤金額", font2));


                        //設置每個單元格所佔列數
                        t1.Colspan = 1;
                        t2.Colspan = 2;
                        t3.Colspan = 2;
                        t4.Colspan = 1;
                        t5.Colspan = 1;
                        t6.Colspan = 1;
                        t7.Colspan = 1;
                        t8.Colspan = 1;
                        t9.Colspan = 1;

                        //設置字體居中
                        t1.HorizontalAlignment = aligncenter;
                        t2.HorizontalAlignment = aligncenter;
                        t3.HorizontalAlignment = aligncenter;
                        t4.HorizontalAlignment = aligncenter;
                        t5.HorizontalAlignment = aligncenter;
                        t6.HorizontalAlignment = aligncenter;
                        t7.HorizontalAlignment = aligncenter;
                        t8.HorizontalAlignment = aligncenter;
                        t9.HorizontalAlignment = aligncenter;

                        //添加單元格到表格中
                        table.AddCell(t1);
                        table.AddCell(t2);
                        table.AddCell(t3);
                        table.AddCell(t4);
                        table.AddCell(t5);
                        table.AddCell(t6);
                        table.AddCell(t7);
                        table.AddCell(t8);
                        table.AddCell(t9);
                    }
                    #endregion


                    #region 表體數據


                    #region 不用管
                    List<V5print.Model.Shop.Order.OrderItems> modelListByCache = this.orderItemManage.GetModelListByCache(" OrderId=" + row2.Field<long>("OrderId"));
                    if ((modelListByCache != null) && (modelListByCache.Count > 0))
                    {

                        foreach (V5print.Model.Shop.Order.OrderItems info in modelListByCache)
                        {
                    #endregion
                            int BuyerCount = 1;
                            foreach (var BuyerIdNow in Buyergroup)
                            {
                                EndFlag++;
                                //編號
                                PdfPCell c1 = new PdfPCell(new Phrase(countflag.ToString(), font2));
                                c1.Colspan = 1;
                                c1.HorizontalAlignment = aligncenter;
                                table.AddCell(c1);

                                if (BuyerCount == 1)
                                {
                                    //業務員
                                    PdfPCell c2 = new PdfPCell(new Phrase(row2.Field<String>("UserName"), font2));
                                    c2.Rowspan = Buyergroup.Count;
                                    c2.Colspan = 2;
                                    c2.HorizontalAlignment = aligncenter;
                                    c2.VerticalAlignment = aligncenter;
                                    table.AddCell(c2);
                                }
                                BuyerCount++;


                                V5print.Model.Members.Users user = userBll.GetModel(BuyerIdNow);
                                //客戶名稱
                                string UserNickName = user.NickName;
                                PdfPCell c3 = new PdfPCell(new Phrase(UserNickName, font2));


                                string sql = string.Format(" and BuyerID={0} ", BuyerIdNow);
                                List<V5print.Model.Shop.Order.OrderInfo> orderList = orderBll.GetModelList(builder.ToString() + sql);
                                //交易總額
                                decimal allAmount = 0;
                                //已收金額
                                decimal allPaid = 0;
                                //總成本
                                decimal allCost = 0;
                                //總利潤
                                decimal allProfit = 0;
                                if (orderList.Count > 0)
                                {
                                    foreach (V5print.Model.Shop.Order.OrderInfo order in orderList)
                                    {
                                        allAmount += order.Amount;
                                        //已收金額
                                        if (order.PaymentStatus == (int)V5print.Model.Shop.Order.EnumHelper.PaymentStatus.Paid)
                                        {
                                            allPaid += order.Amount;
                                        }
                                        allCost += (order.OrderCostPrice == null ? 0 : order.OrderCostPrice.Value);
                                    }
                                }

                                allProfit = allAmount - allCost;

                                //欠款金額
                                decimal allDebt = allAmount - allPaid;
                                //預存餘額
                                decimal allBalance = 0;

                                V5print.Model.Members.UsersExpModel userexp = userexpBll.GetUsersExpModel(BuyerIdNow);
                                if (userexp != null)
                                {
                                    allBalance = Math.Round(decimal.Parse(userexp.Balance.ToString()), 2);
                                }

                                //交易總額
                                PdfPCell c4 = new PdfPCell(new Phrase(allAmount.ToString("0.00"), font2));
                                //訂單總數
                                PdfPCell c5 = new PdfPCell(new Phrase(orderList.Count.ToString(), font2));
                                //已收金額
                                PdfPCell c6 = new PdfPCell(new Phrase(allPaid.ToString("0.00"), font2));
                                //欠款金額
                                PdfPCell c7 = new PdfPCell(new Phrase(allDebt.ToString("0.00"), font2));
                                //成本
                                PdfPCell c8 = new PdfPCell(new Phrase(allCost.ToString("0.00"), font2));
                                //利潤金額
                                PdfPCell c9 = new PdfPCell(new Phrase(allProfit.ToString("0.00"), font2));



                                //彙總數據
                                AllPageAmount += allAmount;
                                AllPageOrderCount += orderList.Count;
                                AllPagePaid += allPaid;
                                AllPageDebt += allDebt;
                                AllPageCost += allCost;
                                AllPageProfit += allProfit;




                                //設置每個單元格所佔列數
                                //c1.Colspan = 1;
                                //c2.Colspan = 2;
                                c3.Colspan = 2;
                                c4.Colspan = 1;
                                c5.Colspan = 1;
                                c6.Colspan = 1;
                                c7.Colspan = 1;
                                c8.Colspan = 1;
                                c9.Colspan = 1;

                                //居中
                                //c1.HorizontalAlignment = aligncenter;
                                //c2.HorizontalAlignment = aligncenter;
                                c3.HorizontalAlignment = aligncenter;
                                c4.HorizontalAlignment = aligncenter;
                                c5.HorizontalAlignment = aligncenter;
                                c6.HorizontalAlignment = aligncenter;
                                c7.HorizontalAlignment = aligncenter;
                                c8.HorizontalAlignment = aligncenter;
                                c9.HorizontalAlignment = aligncenter;

                                //添加單元格到表格中
                                //table.AddCell(c1);
                                //table.AddCell(c2);
                                table.AddCell(c3);
                                table.AddCell(c4);
                                table.AddCell(c5);
                                table.AddCell(c6);
                                table.AddCell(c7);
                                table.AddCell(c8);
                                table.AddCell(c9);

                                countflag++;

                            }


                            #region 還是不用管

                        }
                    }
                            #endregion

                    if (EndFlag == Buyergroup.Count)
                    {
                        break;
                    }

                    #endregion

                }
                #endregion

                document.Add(table);//table end 
                table = null;

                #endregion


                //document.NewPage();
            }


            //彙總數據
            iTextSharp.text.pdf.PdfPTable tableGather = new iTextSharp.text.pdf.PdfPTable(11);//11列
            tableGather.TotalWidth = _tablewidth;
            tableGather.LockedWidth = false;
            PdfPCell gather1 = new PdfPCell(new Phrase("總計", font));
            PdfPCell gather2 = new PdfPCell(new Phrase(AllPageAmount.ToString("0.00"), font));
            PdfPCell gather3 = new PdfPCell(new Phrase(AllPageOrderCount.ToString("0"), font));
            PdfPCell gather4 = new PdfPCell(new Phrase(AllPagePaid.ToString("0.00"), font));
            PdfPCell gather5 = new PdfPCell(new Phrase(AllPageDebt.ToString("0.00"), font));
            PdfPCell gather6 = new PdfPCell(new Phrase(AllPageCost.ToString("0.00"), font));
            PdfPCell gather7 = new PdfPCell(new Phrase(AllPageProfit.ToString("0.00"), font));

            gather1.Colspan = 5;
            gather2.Colspan = 1;
            gather3.Colspan = 1;
            gather4.Colspan = 1;
            gather5.Colspan = 1;
            gather6.Colspan = 1;
            gather7.Colspan = 1;

            gather1.HorizontalAlignment = iTextSharp.text.Rectangle.ALIGN_RIGHT;
            gather2.HorizontalAlignment = aligncenter;
            gather3.HorizontalAlignment = aligncenter;
            gather4.HorizontalAlignment = aligncenter;
            gather5.HorizontalAlignment = aligncenter;
            gather6.HorizontalAlignment = aligncenter;
            gather7.HorizontalAlignment = aligncenter;

            tableGather.AddCell(gather1);
            tableGather.AddCell(gather2);
            tableGather.AddCell(gather3);
            tableGather.AddCell(gather4);
            tableGather.AddCell(gather5);
            tableGather.AddCell(gather6);
            tableGather.AddCell(gather7);

            document.Add(tableGather);


            document.Close();


            PDFToLocal(PdfPath, docName);
        }


        //老闆對賬單一(含客戶)
        private void PrintPDFBossDetail1()
        {
            this.PDFBoss1(1);
        }

        //老闆欠款單一
        private void PrintPDFBossDebt1()
        {
            this.PDFBoss1(2);
        }










        ///<summary>
        ///老闆單據模板二(含客戶)
        ///PDFType 1:對賬單  2:欠款單
        ///</summary>
        private void PDFBoss2(int PDFType)
        {
            #region PDF設置
            string PdfPath = Server.MapPath("/Upload/Temp/");
            string docType = "";
            if (PDFType == 1)
            {
                docType = "老闆對賬單二";
            }
            else if (PDFType == 2)
            {
                docType = "老闆欠款單二";
            }
            string docName = string.Format("{0}{1:yyyyMMddHHmmssffff}", docType, DateTime.Now);
            //設置中文字體
            //BaseFont bfHei = BaseFont.CreateFont(@"C:\Windows\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font font = new iTextSharp.text.Font(bfHei, 11);
            iTextSharp.text.Font font2 = new iTextSharp.text.Font(bfHei, 10);
            //設置居中
            int aligncenter = iTextSharp.text.Rectangle.ALIGN_CENTER;
            //設置紙張尺寸
            int pagenum = int.Parse(this.ddlPaperSize.SelectedValue);
            iTextSharp.text.Rectangle pagesize = PageSize.A4;
            switch (pagenum)
            {
                case 0: pagesize = PageSize.A0; break;
                case 1: pagesize = PageSize.A1; break;
                case 2: pagesize = PageSize.A2; break;
                case 3: pagesize = PageSize.A3; break;
                case 4: pagesize = PageSize.A4; break;
                case 5: pagesize = PageSize.A5; break;
            }
            iTextSharp.text.Document document = new iTextSharp.text.Document(pagesize);
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PdfPath + docName + ".pdf", FileMode.Create));
            document.Open();
            #endregion


            #region 數據來源
            //獲取數據
            StringBuilder builder = new StringBuilder();

            //付款狀態
            if (PDFType == 2)
            {
                if (builder.Length > 1)
                {
                    builder.Append(" and ");
                }
                builder.Append(" PaymentStatus = 0 ");
            }

            if (PageValidate.IsDateTime(this.txtCreatedDateEnd.Text.Trim()) && PageValidate.IsDateTime(this.txtCreatedDateStart.Text.Trim()))
            {
                if (builder.Length > 1)
                {
                    builder.Append(" and ");
                }
                builder.AppendFormat(" CreatedDate between  '{0}' and  '{1}' ", InjectionFilter.QuoteFilter(this.txtCreatedDateStart.Text.Trim()), InjectionFilter.QuoteFilter(this.txtCreatedDateEnd.Text.Trim()));
            }
            //值查看印刷訂單
            if (builder.Length > 1)
            {
                builder.Append(" and ");
            }
            builder.AppendFormat(" OrderType in ({0},{1}) ", (int)V5print.Model.Shop.Order.EnumHelper.OrderItemType.Print, (int)V5print.Model.Shop.Order.EnumHelper.OrderItemType.Template);
            //已發貨的訂單
            if (builder.Length > 1)
            {
                builder.Append(" and ");
            }
            builder.Append(" ShippingStatus>1 ");
            DataSet dataSetSource = this.orderBll.GetListWithEmployee(-1, builder.ToString(), "CreatedDate desc");


            DateTime orderStartTime = new DateTime();
            DateTime orderEndTime = new DateTime();
            int TimeCountFlag = 0;
            //BuyerID去重、EmployeeID去重
            HashSet<int> BuyerIDs = new HashSet<int>();
            HashSet<int> EmployeeIDs = new HashSet<int>();
            foreach (DataRow row0 in dataSetSource.Tables[0].Rows)
            {
                TimeCountFlag++;
                if (TimeCountFlag == 1)
                {
                    orderEndTime = row0.Field<DateTime>("CreatedDate");
                }
                if (TimeCountFlag == dataSetSource.Tables[0].Rows.Count)
                {
                    orderStartTime = row0.Field<DateTime>("CreatedDate");
                }
                BuyerIDs.Add(row0.Field<int>("BuyerID"));
                EmployeeIDs.Add(row0.Field<int>("UserID"));
            }

            string startTime = "";
            string endTime = "";
            if (PageValidate.IsDateTime(this.txtCreatedDateEnd.Text.Trim()) && PageValidate.IsDateTime(this.txtCreatedDateStart.Text.Trim()))
            {
                startTime = this.txtCreatedDateEnd.Text.Trim();
                endTime = this.txtCreatedDateStart.Text.Trim();
            }
            else
            {
                startTime = orderStartTime.ToString("yyyy-MM-dd hh:mm:ss");
                endTime = orderEndTime.ToString("yyyy-MM-dd hh:mm:ss");
            }
            #endregion


            //this.BindData();
            //DataSet dataSetSource = this.gridView.DataSetSource;
            HashSet<int> hs = new HashSet<int>();
            //按業務員不分頁
            hs = EmployeeIDs;
            int countflag = 1;
            #region 表頭文字
            if (countflag == 1)
            {
                Paragraph Title1 = new Paragraph(this.WebSiteSet.Company_Name, font);
                Paragraph Title2 = new Paragraph("_______________________________", font);
                string tel = "";
                if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_Tel400))
                {
                    tel = this.WebSiteSet.Company_Tel400;
                }
                else if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_Telephone))
                {
                    tel = this.WebSiteSet.Company_Telephone;
                }
                else if (!string.IsNullOrWhiteSpace(this.WebSiteSet.Company_MobilePhone))
                {
                    tel = this.WebSiteSet.Company_MobilePhone;
                }

                Paragraph Title3 = new Paragraph("全國免費服務電話:" + tel, font);
                Paragraph Title4 = new Paragraph("時間" + startTime + " ~ " + endTime, font);

                //設置居中  
                Title1.Alignment = aligncenter;
                Title2.Alignment = aligncenter;
                Title3.Alignment = aligncenter;
                Title4.Alignment = aligncenter;


                //空一行  
                Paragraph nullp = new Paragraph(" ", font);
                nullp.Leading = 10;

                //將標題段加入PDF文檔中 
                document.Add(Title1);
                document.Add(Title2);
                document.Add(Title3);
                document.Add(Title4);
                document.Add(nullp);
            }
            #endregion


            //彙總數據
            //客戶數
            int AllPageBuyerCount = 0;
            //交易總額
            decimal AllPageAmount = 0;
            //訂單總數
            int AllPageOrderCount = 0;
            //已收金額
            decimal AllPagePaid = 0;
            //欠款金額
            decimal AllPageDebt = 0;
            //成本金額
            decimal AllPageCost = 0;
            //利潤金額
            decimal AllPageProfit = 0;


            foreach (var hsNow in hs)
            {
                #region EmployeeID

                StringBuilder sbEmployeeID = new StringBuilder();
                //按業務員分頁
                sbEmployeeID.AppendFormat(" and p3.UserID={0} ", hsNow.ToString());
                DataSet dataSetSource2 = this.orderBll.GetListWithEmployee(-1, builder.ToString() + sbEmployeeID.ToString(), "CreatedDate desc");

                //該業務員的所有客戶(去重)
                HashSet<int> Buyergroup = new HashSet<int>();
                foreach (DataRow row3 in dataSetSource2.Tables[0].Rows)
                {
                    Buyergroup.Add(row3.Field<int>("BuyerID"));
                }

                #region 打印數據

                iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(11);//11列
                table.TotalWidth = _tablewidth;
                table.LockedWidth = false;

                int EndFlag = 0;
                foreach (DataRow row2 in dataSetSource2.Tables[0].Rows)
                {

                    #region 表頭
                    if (countflag == 1)
                    {
                        PdfPCell t1 = new PdfPCell(new Phrase("序號", font2));
                        PdfPCell t2 = new PdfPCell(new Phrase("業務員", font2));
                        PdfPCell t3 = new PdfPCell(new Phrase("客戶數", font2));
                        PdfPCell t4 = new PdfPCell(new Phrase("交易總額", font2));
                        PdfPCell t5 = new PdfPCell(new Phrase("訂單總數", font2));
                        PdfPCell t6 = new PdfPCell(new Phrase("已收金額", font2));
                        PdfPCell t7 = new PdfPCell(new Phrase("欠款金額", font2));
                        PdfPCell t8 = new PdfPCell(new Phrase("成本金額", font2));
                        PdfPCell t9 = new PdfPCell(new Phrase("利潤金額", font2));


                        //設置每個單元格所佔列數
                        t1.Colspan = 1;
                        t2.Colspan = 2;
                        t3.Colspan = 2;
                        t4.Colspan = 1;
                        t5.Colspan = 1;
                        t6.Colspan = 1;
                        t7.Colspan = 1;
                        t8.Colspan = 1;
                        t9.Colspan = 1;

                        //設置字體居中
                        t1.HorizontalAlignment = aligncenter;
                        t2.HorizontalAlignment = aligncenter;
                        t3.HorizontalAlignment = aligncenter;
                        t4.HorizontalAlignment = aligncenter;
                        t5.HorizontalAlignment = aligncenter;
                        t6.HorizontalAlignment = aligncenter;
                        t7.HorizontalAlignment = aligncenter;
                        t8.HorizontalAlignment = aligncenter;
                        t9.HorizontalAlignment = aligncenter;

                        //添加單元格到表格中
                        table.AddCell(t1);
                        table.AddCell(t2);
                        table.AddCell(t3);
                        table.AddCell(t4);
                        table.AddCell(t5);
                        table.AddCell(t6);
                        table.AddCell(t7);
                        table.AddCell(t8);
                        table.AddCell(t9);
                    }
                    #endregion


                    #region 表體數據

                    //編號
                    PdfPCell c1 = new PdfPCell(new Phrase(countflag.ToString(), font2));
                    //業務員
                    PdfPCell c2 = new PdfPCell(new Phrase(row2.Field<String>("UserName"), font2));
                    //客戶數
                    PdfPCell c3 = new PdfPCell(new Phrase(Buyergroup.Count.ToString(), font2));

                    //交易總額
                    decimal EmployallAmount = 0;
                    //已收金額
                    decimal EmployallPaid = 0;
                    //總成本
                    decimal EmployallCost = 0;
                    //總利潤
                    decimal EmployallProfit = 0;

                    int EmployallorderCount = 0;

                    #region 不用管
                    List<V5print.Model.Shop.Order.OrderItems> modelListByCache = this.orderItemManage.GetModelListByCache(" OrderId=" + row2.Field<long>("OrderId"));
                    if ((modelListByCache != null) && (modelListByCache.Count > 0))
                    {

                        foreach (V5print.Model.Shop.Order.OrderItems info in modelListByCache)
                        {
                    #endregion
                            #region 統計每個業務員下的用戶總數的相關數據
                            int BuyerCount = 1;
                            foreach (var BuyerIdNow in Buyergroup)
                            {
                                EndFlag++;
                                BuyerCount++;

                                string sql = string.Format(" and BuyerID={0} ", BuyerIdNow);
                                List<V5print.Model.Shop.Order.OrderInfo> orderList = orderBll.GetModelList(builder.ToString() + sql);
                                //交易總額
                                decimal allAmount = 0;
                                //已收金額
                                decimal allPaid = 0;
                                //總成本
                                decimal allCost = 0;
                                //總利潤
                                decimal allProfit = 0;
                                if (orderList.Count > 0)
                                {
                                    foreach (V5print.Model.Shop.Order.OrderInfo order in orderList)
                                    {
                                        allAmount += order.Amount;
                                        //已收金額
                                        if (order.PaymentStatus == (int)V5print.Model.Shop.Order.EnumHelper.PaymentStatus.Paid)
                                        {
                                            allPaid += order.Amount;
                                        }
                                        allCost += (order.OrderCostPrice == null ? 0 : order.OrderCostPrice.Value);
                                    }
                                }

                                allProfit = allAmount - allCost;

                                //欠款金額
                                decimal allDebt = allAmount - allPaid;

                                //訂單總數
                                EmployallorderCount += orderList.Count;
                                //交易總額
                                EmployallAmount += allAmount;
                                //已收金額
                                EmployallPaid += allPaid;
                                //總成本
                                EmployallCost += allCost;
                                //總利潤
                                EmployallProfit += allProfit;


                                countflag++;

                            }
                            #endregion

                            #region 還是不用管

                        }
                    }
                            #endregion



                    //欠款金額
                    decimal EmployallDebt = EmployallAmount - EmployallPaid;

                    //交易總額
                    PdfPCell c4 = new PdfPCell(new Phrase(EmployallAmount.ToString("0.00"), font2));
                    //訂單總數
                    PdfPCell c5 = new PdfPCell(new Phrase(EmployallorderCount.ToString(), font2));
                    //已收金額
                    PdfPCell c6 = new PdfPCell(new Phrase(EmployallPaid.ToString("0.00"), font2));
                    //欠款金額
                    PdfPCell c7 = new PdfPCell(new Phrase(EmployallDebt.ToString("0.00"), font2));
                    //成本
                    PdfPCell c8 = new PdfPCell(new Phrase(EmployallCost.ToString("0.00"), font2));
                    //利潤金額
                    PdfPCell c9 = new PdfPCell(new Phrase(EmployallProfit.ToString("0.00"), font2));


                    //彙總數據
                    AllPageBuyerCount += Buyergroup.Count;
                    AllPageAmount += EmployallAmount;
                    AllPageOrderCount += EmployallorderCount;
                    AllPagePaid += EmployallPaid;
                    AllPageDebt += EmployallDebt;
                    AllPageCost += EmployallCost;
                    AllPageProfit += EmployallProfit;



                    //設置每個單元格所佔列數
                    c1.Colspan = 1;
                    c2.Colspan = 2;
                    c3.Colspan = 2;
                    c4.Colspan = 1;
                    c5.Colspan = 1;
                    c6.Colspan = 1;
                    c7.Colspan = 1;
                    c8.Colspan = 1;
                    c9.Colspan = 1;

                    //居中
                    c1.HorizontalAlignment = aligncenter;
                    c2.HorizontalAlignment = aligncenter;
                    c3.HorizontalAlignment = aligncenter;
                    c4.HorizontalAlignment = aligncenter;
                    c5.HorizontalAlignment = aligncenter;
                    c6.HorizontalAlignment = aligncenter;
                    c7.HorizontalAlignment = aligncenter;
                    c8.HorizontalAlignment = aligncenter;
                    c9.HorizontalAlignment = aligncenter;

                    //添加單元格到表格中
                    table.AddCell(c1);
                    table.AddCell(c2);
                    table.AddCell(c3);
                    table.AddCell(c4);
                    table.AddCell(c5);
                    table.AddCell(c6);
                    table.AddCell(c7);
                    table.AddCell(c8);
                    table.AddCell(c9);



                    if (EndFlag == Buyergroup.Count)
                    {
                        break;
                    }

                    #endregion

                }
                #endregion

                document.Add(table);//table end 
                table = null;

                #endregion


                //document.NewPage();
            }

            //彙總數據
            iTextSharp.text.pdf.PdfPTable tableGather = new iTextSharp.text.pdf.PdfPTable(11);//11列
            tableGather.TotalWidth = _tablewidth;
            tableGather.LockedWidth = false;

            PdfPCell gather1 = new PdfPCell(new Phrase("總計", font));
            PdfPCell gather1_2 = new PdfPCell(new Phrase(AllPageBuyerCount.ToString("0"), font));
            PdfPCell gather2 = new PdfPCell(new Phrase(AllPageAmount.ToString("0.00"), font));
            PdfPCell gather3 = new PdfPCell(new Phrase(AllPageOrderCount.ToString("0"), font));
            PdfPCell gather4 = new PdfPCell(new Phrase(AllPagePaid.ToString("0.00"), font));
            PdfPCell gather5 = new PdfPCell(new Phrase(AllPageDebt.ToString("0.00"), font));
            PdfPCell gather6 = new PdfPCell(new Phrase(AllPageCost.ToString("0.00"), font));
            PdfPCell gather7 = new PdfPCell(new Phrase(AllPageProfit.ToString("0.00"), font));

            gather1.Colspan = 3;
            gather1_2.Colspan = 2;
            gather2.Colspan = 1;
            gather3.Colspan = 1;
            gather4.Colspan = 1;
            gather5.Colspan = 1;
            gather6.Colspan = 1;
            gather7.Colspan = 1;

            gather1.HorizontalAlignment = iTextSharp.text.Rectangle.ALIGN_RIGHT;
            gather1_2.HorizontalAlignment = aligncenter;
            gather2.HorizontalAlignment = aligncenter;
            gather3.HorizontalAlignment = aligncenter;
            gather4.HorizontalAlignment = aligncenter;
            gather5.HorizontalAlignment = aligncenter;
            gather6.HorizontalAlignment = aligncenter;
            gather7.HorizontalAlignment = aligncenter;

            tableGather.AddCell(gather1);
            tableGather.AddCell(gather1_2);
            tableGather.AddCell(gather2);
            tableGather.AddCell(gather3);
            tableGather.AddCell(gather4);
            tableGather.AddCell(gather5);
            tableGather.AddCell(gather6);
            tableGather.AddCell(gather7);

            document.Add(tableGather);



            document.Close();


            PDFToLocal(PdfPath, docName);
        }



        //老闆對賬單二(含客戶)
        private void PrintPDFBossDetail2()
        {
            this.PDFBoss2(1);
        }



        //老闆欠款單二(含客戶)
        private void PrintPDFBossDebt2()
        {
            this.PDFBoss2(2);
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章