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/17 20:53:35 UTC

POI XWPF: how to copy a picture from one XWPFDocument to another

Hello,

I want to append one XWPF document to another main document. I am going through all BodyElements in the document and copying every BodyELement to the main document. 

The problem with my current approach is that the getElementType() only returns "PARAGRAPH" or "TABLE".  If a pragraph contains a picture (PNG), it seems that the the picture cannot be copied over using setParagraph() method (please see my code block in below).

Would anyone advise how to copy a picture from one XWPFDocument to another? I am new to the XWPF API, or is there any better way to append one document to another?  Thanks in advance.

Best regards,
ShuiChen

// start of code block
XWPFDocument doc = new XWPFDocument(new FileInputStream(appFile));
XWPFDocument maindDoc = new XWPFDocument(new FileInputStream(mainFile));

List elementList = doc.getBodyElements();    
List paragraphList = doc.getParagraphs();
List tableList = doc.getTables();        
        
for (int i = 0; i < elementList.size(); i++){
  IBodyElement element = (IBodyElement) elementList.get(i);
  if (element.getElementType().toString().equals("PARAGRAPH")) {

    // copying the paragraph to the main document 
    paragraph = mainDoc.createParagraph();
    paragraph = (XWPFParagraph) paragraphList.get(doc.getParagraphPos(i));
    mainDoc.setParagraph(paragraph, doc.getParagraphPos(i));

  } else if (element.getElementType().toString().equals("TABLE")) {
    XWPFTable table = (XWPFTable) tableList.get(doc.getTablePos(i));
                
    // copying the table to the main document
    table = mainDoc.createTable(); 
    table = (XWPFTable) tableList.get(doc.getTablePos(i));
    mainDoc.setTable(doc.getTablePos(i), table);

  }
}
// end of code block




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


Re: POI XWPF: how to copy a picture from one XWPFDocument to another

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

Can anyone help? I am not sure if it's possible to append two Word documents using POI, I really appreciate if anyone could point me to the right direction?

Kind regards,
ShuiChen

--- On Tue, 1/17/12, Shuichen Zhang <zh...@yahoo.com> wrote:

> From: Shuichen Zhang <zh...@yahoo.com>
> Subject: POI XWPF: how to copy a picture from one XWPFDocument to another
> To: "POI Users List" <us...@poi.apache.org>
> Date: Tuesday, January 17, 2012, 1:53 PM
> Hello,
> 
> I want to append one XWPF document to another main document.
> I am going through all BodyElements in the document and
> copying every BodyELement to the main document. 
> 
> The problem with my current approach is that the
> getElementType() only returns "PARAGRAPH" or "TABLE".  If a
> pragraph contains a picture (PNG), it seems that the the
> picture cannot be copied over using setParagraph() method
> (please see my code block in below).
> 
> Would anyone advise how to copy a picture from one
> XWPFDocument to another? I am new to the XWPF API, or is
> there any better way to append one document to
> another?  Thanks in advance.
> 
> Best regards,
> ShuiChen
> 
> // start of code block
> XWPFDocument doc = new XWPFDocument(new
> FileInputStream(appFile));
> XWPFDocument maindDoc = new XWPFDocument(new
> FileInputStream(mainFile));
> 
> List elementList = doc.getBodyElements();    
> List paragraphList = doc.getParagraphs();
> List tableList = doc.getTables();        
>         
> for (int i = 0; i < elementList.size(); i++){
>   IBodyElement element = (IBodyElement)
> elementList.get(i);
>   if
> (element.getElementType().toString().equals("PARAGRAPH")) {
> 
>     // copying the paragraph to the main document 
>     paragraph = mainDoc.createParagraph();
>     paragraph = (XWPFParagraph)
> paragraphList.get(doc.getParagraphPos(i));
>     mainDoc.setParagraph(paragraph,
> doc.getParagraphPos(i));
> 
>   } else if
> (element.getElementType().toString().equals("TABLE")) {
>     XWPFTable table = (XWPFTable)
> tableList.get(doc.getTablePos(i));
>                 
>     // copying the table to the main document
>     table = mainDoc.createTable(); 
>     table = (XWPFTable)
> tableList.get(doc.getTablePos(i));
>     mainDoc.setTable(doc.getTablePos(i), table);
> 
>   }
> }
> // end of code block
> 
> 
> 
> 
> ---------------------------------------------------------------------
> 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