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.CTBodyImpl.removeTbl(Unknown
 Source)
 at 
org.apache.poi.xwpf.usermodel.XWPFDocument.removeBodyElement(XWPFDocument.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.

Reply via email to