0

Do anyone have any idea to delete pictures in .docx file? I'm using XWPFDocument and had tried like this. Please help.

List<XWPFPictureData> piclist = document.getAllPictures();              
for (int i = 0; i < piclist.size(); i++)     
{
    for (XWPFParagraph p : document.getParagraphs())         
    {
        List<XWPFRun> runs = p.getRuns();
        if (runs != null)
        {
            runs.remove(piclist);
        }
    }
}   

Exception:

ERROR [main] (DocxUtil.java:303) - 
Exception Caught: null java.lang.UnsupportedOperationException at
java.util.Collections$UnmodifiableCollection.remove(Collections.java:1078) at 
com.test.util.DocxUtil.processImage(DocxUtil.java:296) at 
com.test.util.DocxUtil.main(DocxUtil.java:37)   
JSON C11
  • 11,272
  • 7
  • 78
  • 65
Sherin
  • 349
  • 1
  • 15
  • You tell us what you've tried, but not what happens: Do some pictures remain? Do *any* pictures get deleted? Does it hang? – Wai Ha Lee Apr 07 '15 at 06:46
  • It threw out error ERROR [main] (DocxUtil.java:303) - Exception Caught: null java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.remove(Collections.java:1078) at com.test.util.DocxUtil.processImage(DocxUtil.java:296) at com.test.util.DocxUtil.main(DocxUtil.java:37) And also I'm not sure whether my approach is correct or not. Please advice me how to proceed further. – Sherin Apr 07 '15 at 07:10
  • I have tried this code and I am getting an `UnsupportedOperationException` exception – LittlePanda Apr 07 '15 at 07:18
  • I'm not sure whether my approach is correct or not. I tried like this. But didnt work. Thrown error. Please help me to take out this activity. – Sherin Apr 07 '15 at 07:21
  • Please provide some suggestions. – Sherin Apr 07 '15 at 08:06
  • I doubt this operation is supported in POI. I am still looking. Check if docx4j supports removing images. – LittlePanda Apr 07 '15 at 08:51
  • ok..will check with docx4j – Sherin Apr 07 '15 at 09:04
  • No one has any idea regarding this? Please help me to provide some suggestions at least – Sherin Apr 09 '15 at 09:29
  • possible duplicate of [Remove images in .docx file](http://stackoverflow.com/questions/29839147/remove-images-in-docx-file) – blagae Jun 30 '15 at 08:43

1 Answers1

0

In this line:

runs.remove(piclist);
  1. remove() is not supported on result of List<XWPFRun> getRuns(). Apache POI code doesn't support it. That's why you get UnsupportedOperationException. It is the expected behavior.
  2. You try to remove one List<XWPFPictureData> from a List of XWPFRun. It cannot work.

Here is code working well:

for (XWPFParagraph par : document.getParagraphs()) {
    int pos = 0;
    while (pos < par.getRuns().size()) {
        XWPFRun run = par.getRuns().get(pos);
        if (run.getEmbeddedPictures().size() > 0) {
            par.removeRun(pos);
        } else {
            pos++;
        }
    }
}

On more point: your code uses Apache POI. You should tag this question with "apache-poi".

Jean-Pierre Matsumoto
  • 1,917
  • 1
  • 18
  • 26