Exemplo n.º 1
0
Arquivo: Range.cs Projeto: zzy092/npoi
        /**
         * Gets the table that starts with paragraph. In a Word file, a table
         * consists of a group of paragraphs with certain flags Set.
         *
         * @param paragraph
         *            The paragraph that is the first paragraph in the table.
         * @return The table that starts with paragraph
         */
        public Table GetTable(Paragraph paragraph)
        {
            if (!paragraph.IsInTable())
            {
                throw new ArgumentException("This paragraph doesn't belong to a table");
            }

            Range r = paragraph;

            if (r._parent != this)
            {
                throw new ArgumentException("This paragraph is not a child of this range");
            }

            r.InitAll();
            int tableLevel        = paragraph.GetTableLevel();
            int tableEndInclusive = r._parStart;

            if (r._parStart != 0)
            {
                Paragraph previous = new Paragraph(
                    _paragraphs[r._parStart - 1], this);
                if (previous.IsInTable() &&
                    previous.GetTableLevel() == tableLevel &&
                    previous._sectionEnd >= r._sectionStart)
                {
                    throw new ArgumentException("This paragraph is not the first one in the table");
                }
            }

            Range overallRange = _doc.GetOverallRange();
            int   limit        = _paragraphs.Count;

            for (; tableEndInclusive < limit; tableEndInclusive++)
            {
                Paragraph next = new Paragraph(
                    _paragraphs[tableEndInclusive + 1], overallRange);
                if (!next.IsInTable() || next.GetTableLevel() < tableLevel)
                {
                    break;
                }
            }

            InitAll();
            if (tableEndInclusive > _parEnd)
            {
                throw new IndexOutOfRangeException(
                          "The table's bounds fall outside of this Range");
            }
            if (tableEndInclusive < 0)
            {
                throw new IndexOutOfRangeException(
                          "The table's end is negative, which isn't allowed!");
            }
            int endOffsetExclusive = _paragraphs[tableEndInclusive].End;

            return(new Table(paragraph.StartOffset, endOffsetExclusive, this, paragraph.GetTableLevel()));
        }
Exemplo n.º 2
0
        private void InitRows()
        {
            if (_rowsFound)
            {
                return;
            }

            _rows = new List <TableRow>();
            int rowStart = 0;
            int rowEnd   = 0;

            int numParagraphs = NumParagraphs;

            while (rowEnd < numParagraphs)
            {
                Paragraph startRowP = GetParagraph(rowStart);
                Paragraph endRowP   = GetParagraph(rowEnd);
                rowEnd++;
                if (endRowP.IsTableRowEnd() &&
                    endRowP.GetTableLevel() == _tableLevel)
                {
                    _rows.Add(new TableRow(startRowP.StartOffset, endRowP
                                           .EndOffset, this, _tableLevel));
                    rowStart = rowEnd;
                }
            }
            _rowsFound = true;
        }
Exemplo n.º 3
0
        private void initCells()
        {
            if (_cellsFound)
            {
                return;
            }

            short expectedCellsCount = _tprops.GetItcMac();

            int lastCellStart      = 0;
            List <TableCell> cells = new List <TableCell>(
                expectedCellsCount + 1);

            for (int p = 0; p < NumParagraphs; p++)
            {
                Paragraph paragraph = GetParagraph(p);
                String    s         = paragraph.Text;

                if (((s.Length > 0 && s[s.Length - 1] == TABLE_CELL_MARK) || paragraph
                     .IsEmbeddedCellMark()) &&
                    paragraph.GetTableLevel() == _levelNum)
                {
                    TableCellDescriptor tableCellDescriptor = _tprops.GetRgtc() != null &&
                                                              _tprops.GetRgtc().Length > cells.Count ? _tprops
                                                              .GetRgtc()[cells.Count] : new TableCellDescriptor();
                    short leftEdge = (_tprops.GetRgdxaCenter() != null &&
                                      _tprops.GetRgdxaCenter().Length > cells.Count) ? (short)_tprops
                                     .GetRgdxaCenter()[cells.Count] : (short)0;
                    short rightEdge = (_tprops.GetRgdxaCenter() != null &&
                                       _tprops.GetRgdxaCenter().Length > cells.Count + 1) ? (short)_tprops
                                      .GetRgdxaCenter()[cells.Count + 1] : (short)0;

                    TableCell tableCell = new TableCell(GetParagraph(
                                                            lastCellStart).StartOffset, GetParagraph(p)
                                                        .EndOffset, this, _levelNum, tableCellDescriptor,
                                                        leftEdge, rightEdge - leftEdge);
                    cells.Add(tableCell);
                    lastCellStart = p + 1;
                }
            }

            if (lastCellStart < (NumParagraphs - 1))
            {
                TableCellDescriptor tableCellDescriptor = _tprops.GetRgtc() != null &&
                                                          _tprops.GetRgtc().Length > cells.Count ? _tprops
                                                          .GetRgtc()[cells.Count] : new TableCellDescriptor();
                short leftEdge = _tprops.GetRgdxaCenter() != null &&
                                 _tprops.GetRgdxaCenter().Length > cells.Count ? (short)_tprops
                                 .GetRgdxaCenter()[cells.Count] : (short)0;
                short rightEdge = _tprops.GetRgdxaCenter() != null &&
                                  _tprops.GetRgdxaCenter().Length > cells.Count + 1 ? (short)_tprops
                                  .GetRgdxaCenter()[cells.Count + 1] : (short)0;

                TableCell tableCell = new TableCell(lastCellStart,
                                                    (NumParagraphs - 1), this, _levelNum,
                                                    tableCellDescriptor, leftEdge, rightEdge - leftEdge);
                cells.Add(tableCell);
            }

            if (cells.Count > 0)
            {
                TableCell lastCell = cells[cells.Count - 1];
                if (lastCell.NumParagraphs == 1 &&
                    (lastCell.GetParagraph(0).IsTableRowEnd()))
                {
                    // remove "fake" cell
                    cells.RemoveAt(cells.Count - 1);
                }
            }

            if (cells.Count != expectedCellsCount)
            {
                _tprops.SetItcMac((short)cells.Count);
            }

            _cells      = cells.ToArray();
            _cellsFound = true;
        }