Beispiel #1
0
        /**
         *  Wraps around the text in all cells so it fits the column width.
         *  This method should be called after all calls to SetColumnWidth and AutoAdjustColumnWidths.
         *
         */
        public void WrapAroundCellText()
        {
            List <List <Cell> > tableData2 = new List <List <Cell> >();

            for (int i = 0; i < tableData.Count; i++)
            {
                List <Cell> row            = tableData[i];
                int         maxNumVerCells = 1;
                for (int j = 0; j < row.Count; j++)
                {
                    Cell cell    = row[j];
                    int  colspan = cell.GetColSpan();
                    for (int n = 1; n < colspan; n++)
                    {
                        Cell next = row[j + n];
                        cell.SetWidth(cell.GetWidth() + next.GetWidth());
                        next.SetWidth(0f);
                    }
                    int numVerCells = cell.GetNumVerCells();
                    if (numVerCells > maxNumVerCells)
                    {
                        maxNumVerCells = numVerCells;
                    }
                }

                for (int j = 0; j < maxNumVerCells; j++)
                {
                    List <Cell> row2 = new List <Cell>();
                    for (int k = 0; k < row.Count; k++)
                    {
                        Cell cell = row[k];

                        Cell cell2 = new Cell(cell.GetFont(), "");
                        cell2.SetFallbackFont(cell.GetFallbackFont());
                        cell2.SetPoint(cell.GetPoint());
                        cell2.SetWidth(cell.GetWidth());
                        if (j == 0)
                        {
                            cell2.SetTopPadding(cell.top_padding);
                        }
                        if (j == (maxNumVerCells - 1))
                        {
                            cell2.SetBottomPadding(cell.bottom_padding);
                        }
                        cell2.SetLeftPadding(cell.left_padding);
                        cell2.SetRightPadding(cell.right_padding);
                        cell2.SetLineWidth(cell.GetLineWidth());
                        cell2.SetBgColor(cell.GetBgColor());
                        cell2.SetPenColor(cell.GetPenColor());
                        cell2.SetBrushColor(cell.GetBrushColor());
                        cell2.SetProperties(cell.GetProperties());
                        cell2.SetVerTextAlignment(cell.GetVerTextAlignment());
                        cell2.SetIgnoreImageHeight(cell.GetIgnoreImageHeight());

                        if (j == 0)
                        {
                            cell2.SetImage(cell.GetImage());
                            if (cell.GetCompositeTextLine() != null)
                            {
                                cell2.SetCompositeTextLine(cell.GetCompositeTextLine());
                            }
                            else
                            {
                                cell2.SetText(cell.GetText());
                            }
                            if (maxNumVerCells > 1)
                            {
                                cell2.SetBorder(Border.BOTTOM, false);
                            }
                        }
                        else
                        {
                            cell2.SetBorder(Border.TOP, false);
                            if (j < (maxNumVerCells - 1))
                            {
                                cell2.SetBorder(Border.BOTTOM, false);
                            }
                        }
                        row2.Add(cell2);
                    }
                    tableData2.Add(row2);
                }
            }

            for (int i = 0; i < tableData2.Count; i++)
            {
                List <Cell> row = tableData2[i];
                for (int j = 0; j < row.Count; j++)
                {
                    Cell cell = row[j];
                    if (cell.text != null)
                    {
                        int      n         = 0;
                        String[] textLines = cell.GetText().Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                        foreach (String textLine in textLines)
                        {
                            StringBuilder sb     = new StringBuilder();
                            String[]      tokens = Regex.Split(textLine, @"\s+");
                            if (tokens.Length == 1)
                            {
                                sb.Append(tokens[0]);
                            }
                            else
                            {
                                for (int k = 0; k < tokens.Length; k++)
                                {
                                    String token = tokens[k];
                                    if (cell.font.StringWidth(cell.fallbackFont, sb.ToString() + " " + token) >
                                        (cell.GetWidth() - (cell.left_padding + cell.right_padding)))
                                    {
                                        tableData2[i + n][j].SetText(sb.ToString());
                                        sb = new StringBuilder(token);
                                        n++;
                                    }
                                    else
                                    {
                                        if (k > 0)
                                        {
                                            sb.Append(" ");
                                        }
                                        sb.Append(token);
                                    }
                                }
                            }
                            tableData2[i + n][j].SetText(sb.ToString());
                            n++;
                        }
                    }
                    else
                    {
                        tableData2[i][j].SetCompositeTextLine(cell.GetCompositeTextLine());
                    }
                }
            }

            tableData = tableData2;
        }