Hello, For the record, I found that this is caused by a couple of code errors in the XWPFDocument class.
As far as I've been able to see, the errors are not corrected in the https://svn.apache.org/repos/asf/poi/trunk/ repository, so I opened Bug 51148 in Bugzilla with a proposed solution. Cheers, Pere > -----Mensaje original----- > De: Pere Torrodellas [mailto:[email protected]] > Enviado el: jueves, 28 de abril de 2011 17:26 > Para: [email protected] > Asunto: XWPF removeBodyElement() > > Hello List, > > I am experimenting with XWPF (POI 3.7) to evaluate its > possibilities for a new project that needs to manipulate > existing Word files, for example to remove some parts. > > I supposed that this would be possible using the > removeBodyElemet(int pos) method of the XWPFDocument class, > but it didn't work. > > I created a simple docx document with 2 paragraphs, 1 table > and another paragraph. The following code: > > XWPFDocument doc = > new XWPFDocument(new FileInputStream("mydoc.docx")); > java.util.List<IBodyElement> elements = doc.getBodyElements(); > for(int i = 0; i < elements.size(); i++) { > IBodyElement be = elements.get(i); > System.out.println("Element " + String.valueOf(i) + ": " > + be.getClass().getName()); > } > > Correctly prints: > > Element 0: org.apache.poi.xwpf.usermodel.XWPFParagraph > Element 1: org.apache.poi.xwpf.usermodel.XWPFParagraph > Element 2: org.apache.poi.xwpf.usermodel.XWPFTable > Element 3: org.apache.poi.xwpf.usermodel.XWPFParagraph > > but then executing: > > doc.removeBodyElement(i); > > allways fires an exception, whatever the value of i: > > Exception in thread "main" java.lang.IndexOutOfBoundsException > at org.apache.xmlbeans.impl.store.Xobj.remove_element(Xobj.java:2207) > at > org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CT > BodyImpl.removeTbl(Unknown Source) > at > org.apache.poi.xwpf.usermodel.XWPFDocument.removeBodyElement(X > WPFDocument.java:670) > at > org.apache.poi.xwpf.usermodel.ManipDocument.main(ManipDocument > .java:29) > > Where is my mistake? Is this not the way to remove a document element? > > Thanks for any info that can help. > > Pere > > > ________________________________ > AVÍS DE CONFIDENCIALITAT: Aquest missatge i els seus > documents adjunts, poden contenir informació privilegiada i/o > confidencial que està dirigida exclusivament al seu > destinatari. Si vostè rep aquest missatge i no és el > destinatari indicat, si us plau notifiqui-ho immediatament i > remeti el missatge original a l'adreça electrònica indicada. > Qualsevol còpia, ús o distribució no autoritzats d'aquesta > comunicació queda estrictament prohibida. > > AVISO DE CONFIDENCIALIDAD: Este mensaje y sus documentos > adjuntos, pueden contener información privilegiada y/o > confidencial que está dirigida exclusivamente a su > destinatario. Si usted recibe este mensaje y no es el > destinatario indicado, por favor notifíquelo inmediatamente y > remita el mensaje original a la dirección electrónica > indicada. Cualquier copia, uso o distribución no autorizados > de esta comunicación queda estrictamente prohibida. > AVÍS DE CONFIDENCIALITAT: Aquest missatge i els seus documents adjunts, poden contenir informació privilegiada i/o confidencial que està dirigida exclusivament al seu destinatari. Si vostè rep aquest missatge i no és el destinatari indicat, si us plau notifiqui-ho immediatament i remeti el missatge original a l'adreça electrònica indicada. Qualsevol còpia, ús o distribució no autoritzats d'aquesta comunicació queda estrictament prohibida. AVISO DE CONFIDENCIALIDAD: Este mensaje y sus documentos adjuntos, pueden contener información privilegiada y/o confidencial que está dirigida exclusivamente a su destinatario. Si usted recibe este mensaje y no es el destinatario indicado, por favor notifíquelo inmediatamente y remita el mensaje original a la dirección electrónica indicada. Cualquier copia, uso o distribución no autorizados de esta comunicación queda estrictamente prohibida. --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
