Ejemplo n.º 1
0
        /// <summary>
        /// 导出pdf
        /// </summary>
        /// <param name="IsHorizontal">是否为横向布局</param>
        /// <param name="TableTitle">查询列表,表头数据</param>
        /// <returns></returns>
        public byte[] GeneratePDF(bool IsHorizontal = false, PdfHelper.PdfTableTitle TableTitle = null)
        {
            //设置pdf宽度
            Rectangle pagesize = null;

            if (IsHorizontal)
            {
                pagesize = new Rectangle(PageSize.A4.Height, PageSize.A4.Width);
            }
            else
            {
                pagesize = new Rectangle(PageSize.A4.Width, PageSize.A4.Height);
            }
            Document     pdf    = new Document(pagesize);
            MemoryStream ms     = new MemoryStream();
            PdfWriter    writer = PdfWriter.GetInstance(pdf, ms);
            BaseFont     bfSun  = BaseFont.CreateFont(@"c:\windows\fonts\SIMSUN.TTC,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            iTextSharp.text.Font headerFont = new iTextSharp.text.Font(bfSun, 9);
            iTextSharp.text.Font normalFont = new iTextSharp.text.Font(bfSun, 8);
            pdf.Open();

            //如果查询字符串不为空,则加表头
            if (TableTitle != null)
            {
                //设置内容的左右上下margin
                pdf.SetMargins(10, 10, 105, 30);

                var pEvent = new PdfHelper.PDFEvent(bfSun, pagesize)
                {
                    TableTitle = TableTitle
                };
                writer.PageEvent = pEvent;
                //pEvent.CreateHeaderTemplate(write);
            }
            else
            {
                pdf.SetMargins(10, 10, 20, 20);
            }
            pdf.NewPage();
            #region 导出数据表格
            PdfPTable table = new PdfPTable(ListColumns.Sum(x => x.MaxChildrenCount));
            table.KeepTogether        = true;
            table.TotalWidth          = pagesize.Width;
            table.WidthPercentage     = 100;
            table.DefaultCell.Border  = 0;
            table.DefaultCell.Padding = 1;
            MakePdfHeader(ListColumns, table, headerFont);
            Dictionary <IGridColumn <TModel>, int> sameCount = new Dictionary <IGridColumn <TModel>, int>();
            foreach (var baseCol in ListColumns)
            {
                foreach (var col in baseCol.BottomChildren)
                {
                    sameCount.Add(col, 0);
                }
            }
            //int i = 0;
            //foreach (var row in EntityList)
            for (int i = 0; i < EntityList.Count; i++)
            {
                var row = EntityList[i];
                foreach (var baseCol in ListColumns)
                {
                    foreach (var col in baseCol.BottomChildren)
                    {
                        System.Drawing.Color backColor = col.GetBackGroundColor(row);
                        System.Drawing.Color foreColor = col.GetForeGroundColor(row);
                        if (foreColor != System.Drawing.Color.Empty)
                        {
                            normalFont.Color = new BaseColor(foreColor);
                        }
                        PdfPCell cell = new PdfPCell(new Phrase(col.GetText(row).ToHtmlString(), normalFont))
                        {
                            Border = 0, Padding = 1
                        };
                        if (backColor != System.Drawing.Color.Empty)
                        {
                            cell.BackgroundColor = new BaseColor(backColor);
                        }
                        if (col.NeedGroup == true && sameCount[col] == i)
                        {
                            sameCount[col] = sameCount[col] + 1;
                            string lastValue = col.GetText(row).ToHtmlString();
                            for (int j = i + 1; j < EntityList.Count; j++)
                            {
                                if (col.GetText(EntityList[j]).ToHtmlString() == lastValue)
                                {
                                    sameCount[col] = sameCount[col] + 1;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            cell.Rowspan = sameCount[col] - i;
                            table.AddCell(cell);
                        }
                        if (col.NeedGroup == false)
                        {
                            table.AddCell(cell);
                        }
                    }
                }
                //如果有表头,需要每一页又要有表头
                if (TableTitle != null && TableTitle.Count > 0 && (i + 1) % TableTitle.Count == 0)
                {
                    pdf.Add(table);
                    pdf.NewPage();
                    table.Rows.Clear();
                    MakePdfHeader(ListColumns, table, headerFont);
                }
                //i++;
            }
            if (table.Rows.Count > 0)
            {
                pdf.Add(table);
            }
            #endregion

            pdf.Close();
            byte[] rv = ms.ToArray();
            ms.Close();
            return(rv);
        }