4

Do we have the option to remove pictures from .docx file in java using xwpfdocument? Please reply me since I'm trying to do it for past one week. Code tried:

public static void imageProcess(XWPFDocument document) throws IOException
    {
        List<XWPFPictureData> pic=document.getAllPictures();
        Iterator<XWPFPictureData> iterator=pic.iterator();      
        if (pic.size()>0)
        {   
            for (XWPFParagraph para : document.getParagraphs())
            { 
                List<XWPFRun> runs = para.getRuns();
                for( XWPFRun run : runs ){
                    run.getCTR().removeDrawing(0);
                }
            }
            }
        }  

Exception:

 Exception in thread "main" java.lang.IndexOutOfBoundsException
    at org.apache.xmlbeans.impl.store.Xobj.removeElement(Xobj.java:2200)
    at org.apache.xmlbeans.impl.store.Xobj.remove_element(Xobj.java:2230)
    at org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTRImpl.removeDrawing(Unknown Source)
    at com.util.DocxUtil.imageProcess(DocxUtil.java:326)
    at com.util.DocxUtil.main(DocxUtil.java:60)   
NorthCat
  • 9,643
  • 16
  • 47
  • 50
Sherin
  • 349
  • 1
  • 15
  • 7
    If you've been trying to do it for a week, please **state** what you have tried. That way we won't waste our time suggesting things you already know don't work. – Wai Ha Lee Apr 24 '15 at 05:10
  • There is no documentation that states on how to remove images from a docx file. I doubt if POI even supports removing pictures. And the only snippet I had tried from some question just doesnt work. – LittlePanda Apr 24 '15 at 05:18
  • public static void imageProcess(XWPFDocument document) throws IOException { List pic=document.getAllPictures(); Iterator iterator=pic.iterator(); if (pic.size()>0) { for (int i=0;i runs = para.getRuns(); for( XWPFRun run : runs ){ run.getCTR().removeDrawing(0); } } } } } – Sherin Apr 24 '15 at 05:53
  • but it threw error Exception in thread "main" java.lang.IndexOutOfBoundsException at org.apache.xmlbeans.impl.store.Xobj.removeElement(Xobj.java:2200) at org.apache.xmlbeans.impl.store.Xobj.remove_element(Xobj.java:2230) at org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTRImpl.removeDrawing(Unknown Source) at com.util.DocxUtil.imageProcess(DocxUtil.java:326) at com.util.DocxUtil.main(DocxUtil.java:60) – Sherin Apr 24 '15 at 05:53
  • @Sherin: Please edit your question and add your code and exception to the **question**. – LittlePanda Apr 24 '15 at 05:55
  • Why no one is responding? is remove picture not possible with xwpfdocument? – Sherin Apr 24 '15 at 06:44
  • Because the question seemed interesting , i did a little recon for you and like you mentioned there is almost nill info on internet about removing an image from docx using xwpfdocument . All the questions regarding this are unanswered. Check this http://stackoverflow.com/questions/23789739/apache-poi-remove-picture-in-xwpfdocument . If i was in such a situation , i would keep a copy of the project in the present state to try if anyone answers but would move on with the project and try other api's . – Raj Apr 24 '15 at 07:03
  • Also instead of trying to remove images i would try to see if i can create a new doc and copy only text to that. Maybe convert the doc to another format like simple text,html etc first which would allow me to remove images and then reconvert to word doc. – Raj Apr 24 '15 at 07:05
  • If we reconvert to word doc can we preserve formats? and also regarding other api, Do we have other api which perform image remove than xwpfdocument? Preserving format is mandatory in all the cases@Raj – Sherin Apr 24 '15 at 07:48
  • The link below gives a list of paid and free api used for doc convertion. https://angelozerr.wordpress.com/2012/12/06/how-to-convert-docxodt-to-pdfhtml-with-java/ This link below has some code for " how to delete all images from a document" using apose. http://docs.aspose.com:8082/docs/display/wordsjava/com.aspose.words.BookmarkStart.remove+method – Raj Apr 24 '15 at 09:37
  • docs.aspose.com:8082/docs/display/wordsjava/ shows connection refused error. @Raj – Sherin Apr 24 '15 at 09:49
  • 1
    I can open the link fine from my system . If you cant try searching for "com.aspose.words.BookmarkStart.remove method" on google. Open the first link that shows up.. – Raj Apr 24 '15 at 10:18

2 Answers2

2

If you get an IndexOutOfBoundsException on a call where you try to remove item #0, then your list is obviously empty. So either do an emptiness check on all the drawings in your Run object, or use a for loop - which won't execute if your List<CTDrawing> is empty.

for (XWPFRun run : runs) {
    CTR ctr = run.getCTR();
    List<CTDrawing> lst = ctr.getDrawingList();
    for (int i = 0; i < lst.size(); i++) {
        ctr.removeDrawing(i);
    }
}
blagae
  • 2,342
  • 1
  • 27
  • 48
0

Try this :

        for (XWPFRun run : paragraph.getRuns())
                {
                     CTDrawing []  arr = run.getCTR().getDrawingArray();

                     for(int k=0; k<arr.length; k++)
                     {
                         run.getCTR().removeDrawing(k);
                     }

                }
Brice
  • 1
  • 1