XWPFDocument.removeBodyElement(int pos) throws IndexOutOfBoundsException whatever the value of the element index. I think that this is caused by two XWPFDocument class code errors: 1 - In removeBodyElement(int pos), the statement: bodyElements.remove(pos); should be placed AFTER the statements: Integer tablePos = getTablePos(pos); and Integer paraPos = getParagraphPos(pos); instead of before, otherwise the element is not there in the bodyElements List to be used to locate it in the paragraphs or tables Lists. 2 - In getParagraphPos(int pos) and getTablePos(int pos), the for loop should be: for(int i = startPos; i >= 0; i--) { .... }
Good spot. I've cleaned up the code a bit (the table/paragraph code is now generic), and added a bunch of unit tests for it. Fixed in r1100027.