Example #1
0
File: Range.cs Project: 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()));
        }