Ejemplo n.º 1
0
        private TableRenderer CreateTableRendererWithDiffColspan(int maxWidth)
        {
            Table         table         = CreateTableWithDiffColspan(maxWidth);
            TableRenderer tableRenderer = new TableRenderer(table);

            CellRenderer[] row1 = new CellRenderer[] { new CellRenderer(table.GetCell(0, 0)), null, null };
            CellRenderer[] row2 = new CellRenderer[] { null, new CellRenderer(table.GetCell(1, 1)), new CellRenderer(table
                                                                                                                     .GetCell(1, 2)) };
            CellRenderer[] row3 = new CellRenderer[] { new CellRenderer(table.GetCell(1, 0)), new CellRenderer(table.GetCell
                                                                                                                   (2, 1)), new CellRenderer(table.GetCell(2, 2)) };
            tableRenderer.rows[0]        = row1;
            tableRenderer.rows[1]        = row2;
            tableRenderer.rows[2]        = row3;
            tableRenderer.bordersHandler = new SeparatedTableBorders(tableRenderer.rows, table.GetNumberOfColumns(), tableRenderer
                                                                     .GetBorders());
            return(tableRenderer);
        }
Ejemplo n.º 2
0
        public virtual void TestProcessCellsWithPercentWidth02()
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new MemoryStream()));
            Document    doc    = new Document(pdfDoc);
            Table       table  = new Table(UnitValue.CreatePercentArray(new float[] { 2, 1, 1 }));

            table.SetWidth(UnitValue.CreatePercentValue(80));
            table.SetHorizontalAlignment(HorizontalAlignment.CENTER);
            Cell c1 = new Cell(1, 3);

            c1.SetProperty(Property.WIDTH, UnitValue.CreatePercentValue(90));
            c1.Add(new Paragraph("Cell with colspan 3"));
            table.AddCell(c1);
            Cell c2 = new Cell(2, 1);

            c2.Add(new Paragraph("Cell with rowspan 2"));
            c2.SetProperty(Property.WIDTH, UnitValue.CreatePercentValue(50));
            table.AddCell(c2);
            table.AddCell(new Cell().Add(new Paragraph("row 1; cell 1")).SetMinWidth(200));
            table.AddCell(new Cell().Add(new Paragraph("row 1; cell 2")).SetMaxWidth(50));
            table.AddCell("row 2; cell 1");
            table.AddCell("row 2; cell 2");
            TableRenderer tableRenderer = new TableRenderer(table);

            CellRenderer[] row1 = new CellRenderer[] { new CellRenderer(table.GetCell(0, 0)), null, null };
            CellRenderer[] row2 = new CellRenderer[] { null, new CellRenderer(table.GetCell(1, 1)), new CellRenderer(table
                                                                                                                     .GetCell(1, 2)) };
            CellRenderer[] row3 = new CellRenderer[] { new CellRenderer(table.GetCell(1, 0)), new CellRenderer(table.GetCell
                                                                                                                   (2, 1)), new CellRenderer(table.GetCell(2, 2)) };
            tableRenderer.rows[0] = row1;
            tableRenderer.rows[1] = row2;
            tableRenderer.rows[2] = row3;
            table.SetBorder(new SolidBorder(ColorConstants.GREEN, 5));
            tableRenderer.bordersHandler = new SeparatedTableBorders(tableRenderer.rows, table.GetNumberOfColumns(), tableRenderer
                                                                     .GetBorders());
            TableWidths tableWidths            = new TableWidths(tableRenderer, 150, true, 15, 15);
            IList <TableWidths.CellInfo> cells = tableWidths.AutoLayoutCustom();

            foreach (TableWidths.CellInfo cell in cells)
            {
                tableWidths.ProcessCell(cell);
            }
            tableWidths.Recalculate(200);
            doc.Add(table);
            doc.Close();
        }
Ejemplo n.º 3
0
        public virtual void TestProcessCellsWithPercentWidth01()
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new MemoryStream()));
            Document    doc    = new Document(pdfDoc);

            doc.Add(new Paragraph("With 2 columns:"));
            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();

            table.SetMarginTop(5);
            for (int i = 0; i < 4; i++)
            {
                Cell cell = new Cell().Add(new Paragraph("smth" + i));
                cell.SetProperty(Property.WIDTH, UnitValue.CreatePercentValue(50));
                table.AddCell(cell);
            }
            TableRenderer tableRenderer = new TableRenderer(table);

            CellRenderer[] row1 = new CellRenderer[] { new CellRenderer(table.GetCell(0, 0)), new CellRenderer(table.GetCell
                                                                                                                   (0, 1)) };
            CellRenderer[] row2 = new CellRenderer[] { new CellRenderer(table.GetCell(1, 0)), new CellRenderer(table.GetCell
                                                                                                                   (1, 1)) };
            tableRenderer.rows[0] = row1;
            tableRenderer.rows[1] = row2;
            table.SetBorder(new SolidBorder(ColorConstants.GREEN, 5));
            tableRenderer.bordersHandler = new SeparatedTableBorders(tableRenderer.rows, table.GetNumberOfColumns(), tableRenderer
                                                                     .GetBorders());
            TableWidths tableWidths            = new TableWidths(tableRenderer, 150, true, 15, 15);
            IList <TableWidths.CellInfo> cells = tableWidths.AutoLayoutCustom();

            foreach (TableWidths.CellInfo cell in cells)
            {
                tableWidths.ProcessCell(cell);
            }
            tableWidths.Recalculate(25);
            doc.Close();
        }
Ejemplo n.º 4
0
        //endregion
        protected internal override void BuildBordersArrays(CellRenderer cell, int row, int col, int[] rowspansToDeduct
                                                            )
        {
            // We should check if the row number is less than horizontal borders array size. It can happen if the cell with
            // big rowspan doesn't fit current area and is going to be placed partial.
            if (row > horizontalBorders.Count)
            {
                row--;
            }
            int currCellColspan = (int)cell.GetPropertyAsInteger(Property.COLSPAN);
            int nextCellRow;
            int j;

            // consider the cell on the left side of the current one
            if (col != 0 && null == rows[row][col - 1])
            {
                j = col;
                do
                {
                    j--;
                    nextCellRow = row;
                    while (rows.Count != nextCellRow && null == rows[nextCellRow][j])
                    {
                        nextCellRow++;
                    }
                }while (j > 0 && rows.Count != nextCellRow && (j + (int)rows[nextCellRow][j].GetPropertyAsInteger(Property.
                                                                                                                  COLSPAN) != col || (int)nextCellRow - rows[(int)nextCellRow][j].GetPropertyAsInteger(Property.ROWSPAN)
                                                               + 1 + rowspansToDeduct[j] != row));
                // process only valid cells which hasn't been processed yet
                if (j >= 0 && nextCellRow != rows.Count && nextCellRow > row)
                {
                    CellRenderer nextCell = rows[nextCellRow][j];
                    nextCell.SetProperty(Property.ROWSPAN, ((int)nextCell.GetPropertyAsInteger(Property.ROWSPAN)) - rowspansToDeduct
                                         [j]);
                    int nextCellColspan = (int)nextCell.GetPropertyAsInteger(Property.COLSPAN);
                    for (int i = j; i < j + nextCellColspan; i++)
                    {
                        rowspansToDeduct[i] = 0;
                    }
                    BuildBordersArrays(nextCell, nextCellRow, true);
                }
            }
            // consider cells under the current one
            j = 0;
            while (j < currCellColspan)
            {
                nextCellRow = row + 1;
                while (nextCellRow < rows.Count && null == rows[nextCellRow][col + j])
                {
                    nextCellRow++;
                }
                if (nextCellRow == rows.Count)
                {
                    break;
                }
                CellRenderer nextCell = rows[nextCellRow][col + j];
                // otherwise the border was considered previously
                if (row == nextCellRow - (int)nextCell.GetPropertyAsInteger(Property.ROWSPAN))
                {
                    BuildBordersArrays(nextCell, nextCellRow, true);
                }
                j += (int)nextCell.GetPropertyAsInteger(Property.COLSPAN);
            }
            // consider cells on the right side of the current one
            if (col + currCellColspan < rows[row].Length)
            {
                nextCellRow = row;
                while (nextCellRow < rows.Count && null == rows[nextCellRow][col + currCellColspan])
                {
                    nextCellRow++;
                }
                if (nextCellRow != rows.Count)
                {
                    CellRenderer nextCell = rows[nextCellRow][col + currCellColspan];
                    nextCell.SetProperty(Property.ROWSPAN, ((int)nextCell.GetPropertyAsInteger(Property.ROWSPAN)) - rowspansToDeduct
                                         [col + currCellColspan]);
                    int nextCellColspan = (int)nextCell.GetPropertyAsInteger(Property.COLSPAN);
                    for (int i = col + currCellColspan; i < col + currCellColspan + nextCellColspan; i++)
                    {
                        rowspansToDeduct[i] = 0;
                    }
                    BuildBordersArrays(nextCell, nextCellRow, true);
                }
            }
            // consider current cell
            BuildBordersArrays(cell, row, false);
        }
Ejemplo n.º 5
0
 public override IList <Border> GetHorizontalBorder(int index)
 {
     if (index == startRow)
     {
         IList <Border> firstBorderOnCurrentPage = TableBorderUtil.CreateAndFillBorderList(topBorderCollapseWith, tableBoundingBorders
                                                                                           [0], numberOfColumns);
         if (index == largeTableIndexOffset)
         {
             return(GetCollapsedList(horizontalBorders[index - largeTableIndexOffset], firstBorderOnCurrentPage));
         }
         if (0 != rows.Count)
         {
             int col = 0;
             int row = index;
             while (col < numberOfColumns)
             {
                 if (null != rows[row - largeTableIndexOffset][col] && row - index + 1 <= (int)((Cell)rows[row - largeTableIndexOffset
                                                                                                ][col].GetModelElement()).GetRowspan())
                 {
                     CellRenderer cell = rows[row - largeTableIndexOffset][col];
                     Border       cellModelTopBorder = TableBorderUtil.GetCellSideBorder(((Cell)cell.GetModelElement()), Property.BORDER_TOP
                                                                                         );
                     int colspan = (int)cell.GetPropertyAsInteger(Property.COLSPAN);
                     if (null == firstBorderOnCurrentPage[col] || (null != cellModelTopBorder && cellModelTopBorder.GetWidth()
                                                                   > firstBorderOnCurrentPage[col].GetWidth()))
                     {
                         for (int i = col; i < col + colspan; i++)
                         {
                             firstBorderOnCurrentPage[i] = cellModelTopBorder;
                         }
                     }
                     col += colspan;
                     row  = index;
                 }
                 else
                 {
                     row++;
                     if (row == rows.Count)
                     {
                         break;
                     }
                 }
             }
         }
         return(firstBorderOnCurrentPage);
     }
     else
     {
         if ((index == finishRow + 1))
         {
             IList <Border> lastBorderOnCurrentPage = TableBorderUtil.CreateAndFillBorderList(bottomBorderCollapseWith,
                                                                                              tableBoundingBorders[2], numberOfColumns);
             if (index - largeTableIndexOffset == horizontalBorders.Count - 1)
             {
                 return(GetCollapsedList(horizontalBorders[index - largeTableIndexOffset], lastBorderOnCurrentPage));
             }
             if (0 != rows.Count)
             {
                 int col = 0;
                 int row = index - 1;
                 while (col < numberOfColumns)
                 {
                     // TODO
                     if (null != rows[row - largeTableIndexOffset][col])
                     {
                         CellRenderer cell = rows[row - largeTableIndexOffset][col];
                         Border       cellModelBottomBorder = TableBorderUtil.GetCellSideBorder(((Cell)cell.GetModelElement()), Property.
                                                                                                BORDER_BOTTOM);
                         int colspan = (int)cell.GetPropertyAsInteger(Property.COLSPAN);
                         if (null == lastBorderOnCurrentPage[col] || (null != cellModelBottomBorder && cellModelBottomBorder.GetWidth
                                                                          () > lastBorderOnCurrentPage[col].GetWidth()))
                         {
                             for (int i = col; i < col + colspan; i++)
                             {
                                 lastBorderOnCurrentPage[i] = cellModelBottomBorder;
                             }
                         }
                         col += colspan;
                         row  = index - 1;
                     }
                     else
                     {
                         row++;
                         if (row == rows.Count)
                         {
                             break;
                         }
                     }
                 }
             }
             return(lastBorderOnCurrentPage);
         }
         else
         {
             return(horizontalBorders[index - largeTableIndexOffset]);
         }
     }
 }
Ejemplo n.º 6
0
 // endregion
 protected internal abstract void BuildBordersArrays(CellRenderer cell, int row, int col, int[] rowspansToDeduct
                                                     );