You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Shuichen Zhang <zh...@yahoo.com> on 2012/01/12 23:43:35 UTC

POI XWPF: is it possible to get contents from the BodyElement

Hello,

I need to remove some of the paragraphs from a Word document (.docx). I tried several ways using POI XWPF. The simplest way seems to use removeBodyElement(int pos) method in the XWPFDoument class. 

XWPFDocument doc = new XWPFDocument(new FileInputStream("test.docx"));
List bodyElementList = doc.getBodyElements();
doc.removeBodyElement(pos);  // pos is the position of BodyELement to be removed

The problem of this approach is that the "pos" has to be determined in the run time, i.e. I have to check the contents of the BodyElement before I remove it. It seems that there is no such method for user to get text from a BodyElement.

Could anyone advise how to get text from an BodyElement? Thanks in advance!

Best regards,
ShuiChen







---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: POI XWPF: is it possible to get contents from the BodyElement

Posted by Shuichen Zhang <zh...@yahoo.com>.
Hi Nick,

Your suggestion worked very nicely.  Thank you so much for your advice!

Best regards,
ShuiChen

--- On Fri, 1/13/12, Nick Burch <ni...@alfresco.com> wrote:

> From: Nick Burch <ni...@alfresco.com>
> Subject: Re: POI XWPF: is it possible to get contents from the BodyElement
> To: "POI Users List" <us...@poi.apache.org>
> Date: Friday, January 13, 2012, 6:17 AM
> On Thu, 12 Jan 2012, Shuichen Zhang
> wrote:
> > Could anyone advise how to get text from an
> BodyElement? Thanks in advance!
> 
> First you need to check what type it is:
> http://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/IBodyElement.html#getElementType()
> 
> From that, you can cast it to a XWPFParagraph or XWPFTable,
> both of which provide ways to get their text
> 
> Nick
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: POI XWPF: is it possible to get contents from the BodyElement

Posted by Nick Burch <ni...@alfresco.com>.
On Thu, 12 Jan 2012, Shuichen Zhang wrote:
> Could anyone advise how to get text from an BodyElement? Thanks in 
> advance!

First you need to check what type it is:
http://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/IBodyElement.html#getElementType()

>From that, you can cast it to a XWPFParagraph or XWPFTable, both of which 
provide ways to get their text

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org