/// <summary> /// 根据格线构建出格子 /// </summary> private void BuildCells() { _rowCount = _tableHLines.Count - 1; _colCount = _tableVLines.Count - 1; _cells = new AcTableCell[_rowCount, _colCount]; for (int i = 0; i < _tableHLines.Count - 1; i++) { for (int j = 0; j < _tableVLines.Count - 1; j++) { AcTableCell tc = new AcTableCell(); tc.InnerCell.Row = i; tc.InnerCell.Col = j; tc.TopLeft = new Point2d(_tableVLines[j].XorY, _tableHLines[i].XorY); tc.BottomRight = new Point2d(_tableVLines[j + 1].XorY, _tableHLines[i + 1].XorY); tc.Center = new Point2d((tc.TopLeft.X + tc.BottomRight.X) / 2, (tc.TopLeft.Y + tc.BottomRight.Y) / 2); tc.LeftLine = _tableVLines[j]; tc.RightLine = _tableVLines[j + 1]; tc.TopLine = _tableHLines[i]; tc.BottomLine = _tableHLines[i + 1]; _cells[i, j] = tc; } } }
/// <summary> /// 计算单元格合并 /// </summary> private void CalcCellMerge() { for (int i = 0; i < _rowCount; i++) { for (int j = 0; j < _colCount; j++) { if (_cells[i, j].InnerCell.CellType == TableCellType.MergedSlave) { if (_cells[i, j].InnerCell.Row == _cells[i, j].InnerCell.MasterCell.Row && !_cells[i, j].RightLine.HasSegmentOn(_cells[i, j].Center.Y, _tolerance) && j < _colCount - 1 && _cells[i, j + 1].InnerCell.CellType == TableCellType.Normal) { _cells[i, j].InnerCell.MasterCell.ColSpan += 1; for (int row = i; row < i + _cells[i, j].InnerCell.MasterCell.RowSpan; row++) { _cells[row, j + 1].InnerCell.MasterCell = _cells[i, j].InnerCell.MasterCell; _cells[row, j + 1].InnerCell.CellType = TableCellType.MergedSlave; } } else if (_cells[i, j].InnerCell.Col == _cells[i, j].InnerCell.MasterCell.Col && !_cells[i, j].BottomLine.HasSegmentOn(_cells[i, j].Center.X, _tolerance) && i < _rowCount - 1 && _cells[i + 1, j].InnerCell.CellType == TableCellType.Normal) { _cells[i, j].InnerCell.MasterCell.RowSpan += 1; for (int col = j; col < j + _cells[i, j].InnerCell.MasterCell.ColSpan; col++) { _cells[i + 1, col].InnerCell.MasterCell = _cells[i, j].InnerCell.MasterCell; _cells[i + 1, col].InnerCell.CellType = TableCellType.MergedSlave; } } } else if (_cells[i, j].InnerCell.CellType == TableCellType.Normal) { if (!_cells[i, j].RightLine.HasSegmentOn(_cells[i, j].Center.Y, _tolerance) && j < _colCount - 1) { _cells[i, j].InnerCell.CellType = TableCellType.MergedMaster; _cells[i, j].InnerCell.ColSpan = 2; _cells[i, j + 1].InnerCell.MasterCell = _cells[i, j].InnerCell; _cells[i, j + 1].InnerCell.CellType = TableCellType.MergedSlave; } if (!_cells[i, j].BottomLine.HasSegmentOn(_cells[i, j].Center.X, _tolerance) && i < _rowCount - 1) { _cells[i, j].InnerCell.CellType = TableCellType.MergedMaster; _cells[i, j].InnerCell.RowSpan = 2; _cells[i + 1, j].InnerCell.MasterCell = _cells[i, j].InnerCell; _cells[i + 1, j].InnerCell.CellType = TableCellType.MergedSlave; if (_cells[i, j].InnerCell.ColSpan == 2 && j < _colCount - 1) { _cells[i + 1, j + 1].InnerCell.MasterCell = _cells[i, j].InnerCell; _cells[i + 1, j + 1].InnerCell.CellType = TableCellType.MergedSlave; } } } else { throw new AcTableParseException("表格解析错误"); } } } //处理合并后Text的归属 foreach (AcTableCell cell in _cells) { if (cell.InnerCell.CellType == TableCellType.MergedSlave && cell.Texts.Count > 0) { AcTableCell masterCell = _cells[cell.InnerCell.MasterCell.Row, cell.InnerCell.MasterCell.Col]; masterCell.Texts.AddRange(cell.Texts); cell.Texts.Clear(); } } }