public override byte[] Render()
        {
            byte[] buffer = null;

            using (doc = new Document(PageSize.A4))
            using (MemoryStream stream = new MemoryStream())
            {
                writer = PdfWriter.GetInstance(doc, stream);
                writer.CloseStream = false;
                doc.Open();
                dc = writer.DirectContent;

                var r = Reports[0];
                currentReport = r;
                marginTop = r.Margins.Top;
                marginBottom = r.Margins.Bottom;
                marginLeft = r.Margins.Left;
                marginRight = r.Margins.Right;

                var tHeader = RenderHeader();
                var tFooter = RenderFooter();

                dc.AddTemplate(tHeader, 0, -1 * marginTop);
                dc.AddTemplate(tFooter, 0, marginBottom);

                var bodyHeight = PageHeight - (marginBottom + marginTop + r.Header.Height + r.Footer.Height);

                var t = GenerateTable(r.Body.Elements[0] as TableElement);
                t.CalculateHeights();

                var height = 0f;
                var index = 0;
                var page = 1;
                for (int i = 0; i < t.Rows.Count; i++)
                {
                    var rowHeight = t.GetRowHeight(i);
                    if (height + rowHeight > bodyHeight)
                    {
                        t.WriteSelectedRows(index, i, TranslateLeft(r.Body.Elements[0]), TranslateTop(r.Body.Elements[0]) - marginTop - (page > 1 ? t.GetRowHeight(0) : 0), dc);
                        index = i;
                        doc.NewPage();
                        dc.AddTemplate(tHeader, 0, -1 * marginTop);
                        dc.AddTemplate(tFooter, 0, marginBottom);
                        t.WriteSelectedRows(0, 1, TranslateLeft(r.Body.Elements[0]), TranslateTop(r.Body.Elements[0]) - marginTop, dc);
                        height = t.GetRowHeight(0);
                        page++;
                    }
                    else
                        height += rowHeight;
                }
                t.WriteSelectedRows(index, t.Rows.Count, TranslateLeft(r.Body.Elements[0]), TranslateTop(r.Body.Elements[0]) - marginTop - (page > 1 ? t.GetRowHeight(0) : 0), dc);

                doc.Close();
                buffer = stream.GetBuffer();
            }
            return buffer;
        }
 public ReportSection(Report report)
 {
     Report = report;
     Elements = new ElementCollection(this);
 }