Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除.docx文件中的图像_Java_Image_Xwpf - Fatal编程技术网

Java 删除.docx文件中的图像

Java 删除.docx文件中的图像,java,image,xwpf,Java,Image,Xwpf,我们是否可以使用xwpfdocument从java中的.docx文件中删除图片?请回复我,因为我在过去的一周里一直在努力。 已尝试的代码: public static void imageProcess(XWPFDocument document) throws IOException { List<XWPFPictureData> pic=document.getAllPictures(); Iterator<XWPFPictureDa

我们是否可以使用
xwpfdocument
从java中的
.docx
文件中删除图片?请回复我,因为我在过去的一周里一直在努力。 已尝试的代码:

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);
                }
            }
            }
        }  

如果您在试图删除项目0的通话中收到一个
IndexOutOfBoundsException
,那么您的列表显然是空的。因此,要么对
运行
对象中的所有图形执行空性检查,要么使用for循环-如果
列表
为空,则不会执行该循环

for (XWPFRun run : runs) {
    CTR ctr = run.getCTR();
    List<CTDrawing> lst = ctr.getDrawingList();
    for (int i = 0; i < lst.size(); i++) {
        ctr.removeDrawing(i);
    }
}
for(XWPFRun运行:运行){
CTR CTR=run.getCTR();
List lst=ctr.getDrawingList();
对于(inti=0;i
试试这个:

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

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

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

对于(int k=0;kIf如果您已经尝试了一周,请说明您已经尝试了什么。这样,我们就不会浪费时间建议您已经知道的事情不起作用。没有说明如何从docx文件中删除图像的文档。我怀疑POI是否支持删除图片。这是我从某个问题j中尝试的唯一片段ust不起作用。公共静态void imageProcess(XWPFDocument文档)引发IOException{List pic=document.getAllPictures();迭代器迭代器迭代器=pic.Iterator();if(pic.size()>0){for(int i=0;i它在线程“main”中引发了错误异常org.apache.xmlbeans.impl.store.Xobj.removeElement(Xobj.java:2200)的java.lang.IndexOutOfBoundsException位于org.apache.xmlbeans.impl.store.Xobj.remove_元素(Xobj.java:2230),org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTRImpl.removeDrawing(未知源代码)位于com.util.DocxUtil.imageProcess(DocxUtil.java:326)在com.util.DocxUtil.main(DocxUtil.java:60)上,我可以从我的系统中打开链接。如果您无法尝试在google上搜索“com.aspose.words.BookmarkStart.remove method”,请打开显示的第一个链接。。
        for (XWPFRun run : paragraph.getRuns())
                {
                     CTDrawing []  arr = run.getCTR().getDrawingArray();

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

                }