Example #1
0
        // use the TableObject to represent the contents. It's easier to export it later.
        private TableObject GetTable(bool autoRows)
        {
            TableObject table = new TableObject();

            table.SetPrinting(IsPrinting);
            table.SetReport(Report);

            float cellWidth  = CellWidth;
            float cellHeight = CellHeight;

            // calculate cellWidth, cellHeight automatically
            if (cellWidth == 0 || cellHeight == 0)
            {
                float fontHeight = Font.GetHeight() * 96f / DrawUtils.ScreenDpi;
                cellWidth  = (int)Math.Round((fontHeight + 10) / Page.SnapSize.Width) * Page.SnapSize.Width;
                cellHeight = cellWidth;
            }

            int colCount = (int)((Width + HorzSpacing + 1) / (cellWidth + HorzSpacing));

            if (colCount == 0)
            {
                colCount = 1;
            }
            int rowCount = (int)((Height + VertSpacing + 1) / (cellHeight + VertSpacing));

            if (rowCount == 0 || autoRows)
            {
                rowCount = 1;
            }

            table.ColumnCount = colCount;
            table.RowCount    = rowCount;

            // process the text
            int    row       = 0;
            int    lineBegin = 0;
            int    lastSpace = 0;
            string text      = Text.Replace("\r\n", "\n");

            for (int i = 0; i < text.Length; i++)
            {
                bool isCRLF = text[i] == '\n';
                if (text[i] == ' ' || isCRLF)
                {
                    lastSpace = i;
                }

                if (i - lineBegin + 1 > colCount || isCRLF)
                {
                    if (WordWrap && lastSpace > lineBegin)
                    {
                        AddText(table, row, text.Substring(lineBegin, lastSpace - lineBegin));
                        lineBegin = lastSpace + 1;
                    }
                    else if (i - lineBegin > 0)
                    {
                        AddText(table, row, text.Substring(lineBegin, i - lineBegin));
                        lineBegin = i;
                    }
                    else
                    {
                        lineBegin = i + 1;
                    }

                    lastSpace = lineBegin;
                    row++;
                    if (autoRows && row >= rowCount)
                    {
                        rowCount++;
                        table.RowCount++;
                    }
                }
            }

            // finish the last line
            if (lineBegin < text.Length)
            {
                AddText(table, row, text.Substring(lineBegin, text.Length - lineBegin));
            }

            // set up cells appearance
            for (int i = 0; i < colCount; i++)
            {
                for (int j = 0; j < rowCount; j++)
                {
                    TableCell cell = table[i, j];
                    cell.Border    = Border.Clone();
                    cell.Fill      = Fill.Clone();
                    cell.Font      = Font;
                    cell.TextFill  = TextFill.Clone();
                    cell.HorzAlign = HorzAlign.Center;
                    cell.VertAlign = VertAlign.Center;
                }
            }

            // set cell's width and height
            for (int i = 0; i < colCount; i++)
            {
                table.Columns[i].Width = cellWidth;
            }

            for (int i = 0; i < rowCount; i++)
            {
                table.Rows[i].Height = cellHeight;
            }

            // insert spacing between cells
            if (HorzSpacing > 0)
            {
                for (int i = 0; i < colCount - 1; i++)
                {
                    TableColumn newColumn = new TableColumn();
                    newColumn.Width = HorzSpacing;
                    table.Columns.Insert(i * 2 + 1, newColumn);
                }
            }

            if (VertSpacing > 0)
            {
                for (int i = 0; i < rowCount - 1; i++)
                {
                    TableRow newRow = new TableRow();
                    newRow.Height = VertSpacing;
                    table.Rows.Insert(i * 2 + 1, newRow);
                }
            }

            table.Left   = AbsLeft;
            table.Top    = AbsTop;
            table.Width  = table.Columns[table.ColumnCount - 1].Right;
            table.Height = table.Rows[table.RowCount - 1].Bottom;
            return(table);
        }