Esempio n. 1
0
        internal override void Render(IRenderData renderData, int page)
        {
            if (IsNotVisible(renderData))
            {
                return;
            }

            var pushTextForwardOnPages = 0;

            if (Visibility == PageVisibility.LastPage && renderData.PageNumberInfo.TotalPages != page)
            {
                pushTextForwardOnPages = renderData.PageNumberInfo.TotalPages.Value - 1;
            }

            if (_pageRowSet == null)
            {
                throw new InvalidOperationException("PreRendering has not yet been performed.");
            }

            renderData.ElementBounds = GetBounds(renderData.ParentBounds);

            if (!renderData.IncludeBackground && IsBackground)
            {
                return;
            }

            var headerFont  = new XFont(_headerFont.GetName(renderData.Section), _headerFont.GetSize(renderData.Section), _headerFont.GetStyle(renderData.Section));
            var headerBrush = new XSolidBrush(XColor.FromArgb(_headerFont.GetColor(renderData.Section)));
            var lineFont    = new XFont(_contentFont.GetName(renderData.Section), _contentFont.GetSize(renderData.Section), _contentFont.GetStyle(renderData.Section));
            var lineBrush   = new XSolidBrush(XColor.FromArgb(_contentFont.GetColor(renderData.Section)));
            var groupFont   = new XFont(_groupFont.GetName(renderData.Section), _groupFont.GetSize(renderData.Section), _groupFont.GetStyle(renderData.Section));

            var firstColumn = _columns.FirstOrDefault();

            if (firstColumn.Value == null)
            {
                return;
            }
            var headerSize  = renderData.Graphics.MeasureString(firstColumn.Value.Title, headerFont, XStringFormats.TopLeft);
            var stdLineSize = renderData.Graphics.MeasureString(firstColumn.Value.Title, lineFont, XStringFormats.TopLeft);

            if (renderData.DebugData != null)
            {
                renderData.Graphics.DrawString(string.Format("Table: {0}", Name), renderData.DebugData.Font, renderData.DebugData.Brush, renderData.ElementBounds.Center);
            }

            if (renderData.DocumentData != null)
            {
                var dataTable = renderData.DocumentData.GetDataTable(Name);
                if (dataTable != null)
                {
                    var columnPadding = ColumnPadding.ToXUnit(renderData.ElementBounds.Width);

                    foreach (var column in _columns.Where(x => x.Value.WidthMode == WidthMode.Auto).ToList())
                    {
                        //Get the size of the columnt title text
                        var stringSize = renderData.Graphics.MeasureString(column.Value.Title, headerFont, XStringFormats.TopLeft);
                        var wd         = UnitValue.Parse((stringSize.Width + columnPadding).ToString(CultureInfo.InvariantCulture) + "px");

                        //If there is a fixed width value, start with that.
                        if (column.Value.Width == null)
                        {
                            column.Value.Width = wd;
                        }

                        //If the column header title is greater than
                        if (column.Value.Width.Value < wd)
                        {
                            column.Value.Width = wd;
                        }

                        foreach (var row in dataTable.Rows)
                        {
                            if (row is DocumentDataTableData)
                            {
                                var rowData = row as DocumentDataTableData;

                                var cellData = GetValue(column.Key, rowData.Columns);
                                stringSize = renderData.Graphics.MeasureString(cellData, lineFont, XStringFormats.TopLeft);
                                wd         = UnitValue.Parse((stringSize.Width + columnPadding).ToString(CultureInfo.InvariantCulture) + "px");
                                if (column.Value.Width < wd)
                                {
                                    column.Value.Width = wd;
                                }
                            }
                        }
                    }

                    foreach (var column in _columns.ToList())
                    {
                        if (column.Value.HideValue != null)
                        {
                            column.Value.Hide = true;
                        }

                        foreach (var row in dataTable.Rows)
                        {
                            if (row is DocumentDataTableData)
                            {
                                var rowData         = row as DocumentDataTableData;
                                var cellData        = GetValue(column.Key, rowData.Columns);
                                var parsedHideValue = GetValue(column.Value.HideValue, rowData.Columns);
                                if (parsedHideValue != cellData)
                                {
                                    column.Value.Hide = false;
                                }
                            }
                        }

                        if (column.Value.Hide)
                        {
                            column.Value.Width = new UnitValue();
                            if (HideTableWhenColumnIsHidden == column.Key)
                            {
                                //Hide the entire table
                                return;
                            }
                        }
                    }

                    RenderBorder(renderData.ElementBounds, renderData.Graphics, headerSize);

                    var totalWidth     = renderData.ElementBounds.Width;
                    var nonSpringWidth = _columns.Where(x => x.Value.WidthMode != WidthMode.Spring).Sum(x => x.Value.Width != null ? x.Value.Width.Value.ToXUnit(totalWidth) : 0);

                    var springCount = _columns.Count(x => x.Value.WidthMode == WidthMode.Spring && !x.Value.Hide);
                    if (springCount > 0)
                    {
                        foreach (var column in _columns.Where(x => x.Value.WidthMode == WidthMode.Spring && !x.Value.Hide).ToList())
                        {
                            if (column.Value.Width == null)
                            {
                                column.Value.Width = "0";
                            }
                            var calculatedWidth = new UnitValue((renderData.ElementBounds.Width - nonSpringWidth) / springCount, UnitValue.EUnit.Point);
                            if (calculatedWidth > column.Value.Width)
                            {
                                column.Value.Width = calculatedWidth;
                            }
                        }
                    }

                    AssureTotalColumnWidth(renderData.ElementBounds.Width);

                    //Create header
                    double left         = 0;
                    var    tableColumns = _columns.Values.Where(x => !x.Hide).ToList();
                    foreach (var column in tableColumns)
                    {
                        var alignmentJusttification = 0D;
                        if (column.Align == Alignment.Right)
                        {
                            var stringSize = renderData.Graphics.MeasureString(column.Title, headerFont, XStringFormats.TopLeft);
                            alignmentJusttification = column.Width.Value.ToXUnit(renderData.ElementBounds.Width) - stringSize.Width - (columnPadding / 2);
                        }
                        else
                        {
                            alignmentJusttification += columnPadding / 2;
                        }

                        renderData.Graphics.DrawString(column.Title, headerFont, headerBrush, new XPoint(renderData.ElementBounds.Left + left + alignmentJusttification, renderData.ElementBounds.Top), XStringFormats.TopLeft);
                        left += column.Width.Value.ToXUnit(renderData.ElementBounds.Width);

                        if (renderData.DebugData != null)
                        {
                            renderData.Graphics.DrawLine(renderData.DebugData.Pen, renderData.ElementBounds.Left + left, renderData.ElementBounds.Top, renderData.ElementBounds.Left + left, renderData.ElementBounds.Bottom);
                        }
                    }

                    var top       = headerSize.Height + RowPadding.ToXUnit(renderData.ElementBounds.Height);
                    var pageIndex = 1;

                    var defaultRowset = true;
                    var pageRowSet    = new PageRowSet {
                        FromRow = 1
                    };
                    var index = page - renderData.Section.GetPageOffset() - pushTextForwardOnPages;
                    if (_pageRowSet.Count > index)
                    {
                        try
                        {
                            defaultRowset = false;
                            pageRowSet    = _pageRowSet[index];
                        }
                        catch (Exception exception)
                        {
                            throw new InvalidOperationException(string.Format("_pageRowSet.Count={0}, index={1}", _pageRowSet.Count, index), exception);
                        }
                    }

                    //Draw column separator lines
                    if (ColumnBorderColor != null)
                    {
                        left = 0;
                        var borderPen = new XPen(ColumnBorderColor.Value, 0.1); //TODO: Set the thickness of the boarder
                        foreach (var column in _columns.Where(x => !x.Value.Hide).TakeAllButLast().ToList())
                        {
                            left += column.Value.Width.Value.ToXUnit(renderData.ElementBounds.Width);
                            renderData.Graphics.DrawLine(borderPen, renderData.ElementBounds.Left + left, renderData.ElementBounds.Top, renderData.ElementBounds.Left + left, renderData.ElementBounds.Bottom);
                        }
                    }

                    for (var i = pageRowSet.FromRow; i < pageRowSet.ToRow + 1; i++)
                    {
                        DocumentDataTableLine row;
                        try
                        {
                            row = dataTable.Rows[i];
                        }
                        catch (Exception exception)
                        {
                            var msg = string.Format("Looping from {0} to {1}. Currently at {2}, collection has {3} lines.", pageRowSet.FromRow, pageRowSet.ToRow + 1, i, dataTable.Rows.Count);
                            throw new InvalidOperationException(msg + string.Format("dataTable.Rows.Count={0}, i={1}, pageIndex={2}, page={3}, GetPageOffset={4}, index={5}, FromRow={6}, ToRow={7}, _pageRowSet.Count={8}, defaultRowset={9}", dataTable.Rows.Count, i, pageIndex, page, renderData.Section.GetPageOffset(), index, pageRowSet.FromRow, pageRowSet.ToRow, _pageRowSet.Count, defaultRowset), exception);
                        }

                        left = 0;
                        var lineSize = stdLineSize;
                        if (row is DocumentDataTableData)
                        {
                            var rowData = row as DocumentDataTableData;

                            foreach (var column in _columns.Where(x => !x.Value.Hide).ToList())
                            {
                                var cellData = GetValue(column.Key, rowData.Columns);

                                var alignmentJusttification = 0D;
                                if (column.Value.Align == Alignment.Right)
                                {
                                    var stringSize = renderData.Graphics.MeasureString(cellData, lineFont, XStringFormats.TopLeft);
                                    alignmentJusttification = column.Value.Width.Value.ToXUnit(renderData.ElementBounds.Width) - stringSize.Width - (columnPadding / 2);
                                }
                                else
                                {
                                    alignmentJusttification += columnPadding / 2;
                                }

                                var parsedHideValue = GetValue(column.Value.HideValue, rowData.Columns);
                                if (parsedHideValue == cellData)
                                {
                                    cellData = string.Empty;
                                }

                                //TODO: If the string is too long. Cut the string down.
                                var calculatedCellData = AssureThatTextFitsInColumn(renderData, cellData, column, columnPadding, lineFont);

                                renderData.Graphics.DrawString(calculatedCellData, lineFont, lineBrush, new XPoint(renderData.ElementBounds.Left + left + alignmentJusttification, renderData.ElementBounds.Top + top), XStringFormats.TopLeft);
                                left += column.Value.Width.Value.ToXUnit(renderData.ElementBounds.Width);
                            }

                            if (_skipLine != null && pageIndex % SkipLine.Interval == 0)
                            {
                                var skipLineHeight = SkipLine.Height.ToXUnit(renderData.ElementBounds.Height);

                                if (SkipLine.BorderColor != null)
                                {
                                    renderData.Graphics.DrawLine(new XPen(XColor.FromArgb((Color)SkipLine.BorderColor), 0.1), renderData.ElementBounds.Left, renderData.ElementBounds.Top + top + lineSize.Height + skipLineHeight / 2, renderData.ElementBounds.Right, renderData.ElementBounds.Top + top + lineSize.Height + skipLineHeight / 2);
                                }

                                top += skipLineHeight;
                            }
                        }
                        else if (row is DocumentDataTableGroup)
                        {
                            var group = row as DocumentDataTableGroup;

                            if (pageIndex != 1)
                            {
                                top += GroupSpacing.ToXUnit(renderData.ElementBounds.Height);
                            }

                            var groupData  = group.Content;
                            var stringSize = renderData.Graphics.MeasureString(groupData, groupFont, XStringFormats.TopLeft);
                            lineSize = stringSize;
                            var topLeftBox  = new XPoint(renderData.ElementBounds.Left + left, renderData.ElementBounds.Top + top);
                            var topLeftText = new XPoint(renderData.ElementBounds.Left + left + (columnPadding / 2), renderData.ElementBounds.Top + top);

                            if (GroupBackgroundColor != null)
                            {
                                var brush = new XSolidBrush(XColor.FromArgb(GroupBackgroundColor.Value));
                                var rect  = new XRect(topLeftBox, new XSize(renderData.ElementBounds.Width, stringSize.Height));
                                renderData.Graphics.DrawRectangle(new XPen(XColor.FromArgb(GroupBorderColor ?? GroupBackgroundColor.Value), 0.1), brush, rect);
                            }
                            else if (GroupBorderColor != null)
                            {
                                var rect = new XRect(topLeftBox, new XSize(renderData.ElementBounds.Width, stringSize.Height));
                                renderData.Graphics.DrawRectangle(new XPen(XColor.FromArgb(GroupBorderColor.Value), 0.1), rect);
                            }

                            renderData.Graphics.DrawString(groupData, groupFont, lineBrush, topLeftText, XStringFormats.TopLeft);
                            pageIndex = 0;
                        }

                        top += lineSize.Height;
                        top += RowPadding.ToXUnit(renderData.ElementBounds.Height);

                        pageIndex++;
                    }

                    ////Draw column separator lines
                    //if (ColumnBorderColor != null)
                    //{
                    //    left = 0;
                    //    var borderPen = new XPen(ColumnBorderColor.Value, 0.1); //TODO: Set the thickness of the boarder
                    //    foreach (var column in _columns.Where(x => !x.Value.Hide).TakeAllButLast().ToList())
                    //    {
                    //        left += column.Value.Width.Value.GetXUnitValue(renderData.ElementBounds.Width);
                    //        renderData.Graphics.DrawLine(borderPen, renderData.ElementBounds.Left + left, renderData.ElementBounds.Top, renderData.ElementBounds.Left + left, renderData.ElementBounds.Bottom);
                    //    }
                    //}
                }
            }

            if (renderData.DebugData != null)
            {
                renderData.Graphics.DrawRectangle(renderData.DebugData.Pen, renderData.ElementBounds);
            }
        }
Esempio n. 2
0
        internal override int PreRender(IRenderData renderData)
        {
            _pageRowSet = new List <PageRowSet>();

            renderData.ElementBounds = GetBounds(renderData.ParentBounds);

            if (IsBackground && !renderData.IncludeBackground)
            {
                return(0);
            }

            var headerFont = new XFont(_headerFont.GetName(renderData.Section), _headerFont.GetSize(renderData.Section), _headerFont.GetStyle(renderData.Section));
            var lineFont   = new XFont(_contentFont.GetName(renderData.Section), _contentFont.GetSize(renderData.Section), _contentFont.GetStyle(renderData.Section));
            var groupFont  = new XFont(_groupFont.GetName(renderData.Section), _groupFont.GetSize(renderData.Section), _groupFont.GetStyle(renderData.Section));

            var firstColumn = _columns.FirstOrDefault();

            if (firstColumn.Value == null)
            {
                return(0);
            }

            var headerSize  = renderData.Graphics.MeasureString(firstColumn.Value.Title, headerFont, XStringFormats.TopLeft);
            var stdLineSize = renderData.Graphics.MeasureString(firstColumn.Value.Title, lineFont, XStringFormats.TopLeft);

            if (renderData.DocumentData != null)
            {
                var dataTable = renderData.DocumentData.GetDataTable(Name);
                if (dataTable != null)
                {
                    var top             = headerSize.Height + RowPadding.ToXUnit(renderData.ElementBounds.Height);
                    var pageIndex       = 1;
                    var firstLineOnPage = 0;
                    for (var i = 0; i < dataTable.Rows.Count; i++)
                    {
                        var lineSize = stdLineSize;
                        if (dataTable.Rows[i] is DocumentDataTableData)
                        {
                            if (_skipLine != null && pageIndex % SkipLine.Interval == 0)
                            {
                                top += SkipLine.Height.ToXUnit(renderData.ElementBounds.Height);
                            }
                        }
                        else if (dataTable.Rows[i] is DocumentDataTableGroup)
                        {
                            top      += GroupSpacing.ToXUnit(renderData.ElementBounds.Height);
                            lineSize  = renderData.Graphics.MeasureString("X", groupFont, XStringFormats.TopLeft);
                            pageIndex = 0;
                        }

                        top += lineSize.Height;
                        top += RowPadding.ToXUnit(renderData.ElementBounds.Height);

                        pageIndex++;

                        if (top > renderData.ElementBounds.Height - lineSize.Height)
                        {
                            _pageRowSet.Add(new PageRowSet {
                                FromRow = firstLineOnPage, ToRow = i
                            });
                            firstLineOnPage = i + 1;
                            top             = headerSize.Height + RowPadding.ToXUnit(renderData.ElementBounds.Height);
                        }
                    }

                    if (firstLineOnPage != dataTable.Rows.Count)
                    {
                        _pageRowSet.Add(new PageRowSet {
                            FromRow = firstLineOnPage, ToRow = dataTable.Rows.Count - 1
                        });
                    }
                }
            }

            return(_pageRowSet.Count);
        }