我们是否可以选择.docx
使用 java 从文件中删除图片xwpfdocument
?请回复我,因为我过去一周都在尝试这样做。代码尝试:
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 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)