简体   繁体   English

Apache Poi替换标题上的现有图片

[英]Apache poi replace existing picture on header

Is there any way to replace an image on word(docx) file header by name of the image with apache poi? 有什么方法可以用apache poi替换word(docx)文件头上的图像吗? I'am thinking about that: 我正在考虑:

+--------------------------------+ + -------------------------------- +
+HEADER myimage.jpeg-+ + HEADER myimage.jpeg- +
+ -----------BODY------------+ + -----------身体------------ +
+--------------------------------+ + -------------------------------- +

replaceImage("myimage.jpeg", newPictureInputStream, "newPicture_name.jpeg"); replaceImage(“ myimage.jpeg”,newPictureInputStream,“ newPicture_name.jpeg”);

Here what I tried: 这是我尝试的:

    XWPFParagraph originalParagraph = null;
    originalParagraph = getPictureParagraphInHead(lookingPictureName);
    ListIterator<XWPFRun> it = originalParagraph.getRuns().listIterator();
    XWPFRun replacedRun = null;

    while (it.hasNext()) {
        XWPFRun run = it.next();
        int runIDX = it.nextIndex();
        if (run.getEmbeddedPictures().size() > 0) {
            XWPFRun newRun = null;
            newRun = new XWPFRun(run.getCTR(), (IRunBody) originalParagraph);
            originalParagraph.addRun(newRun);
            originalParagraph.removeRun(originalParagraph.getRuns().indexOf(run));
            break;
        }
    }

I'm not sure if you can get the "filename" of the image with POI. 我不确定是否可以通过POI获得图像的“文件名”。 It's probably in the XML so you might have to make your own method for finding the image. 它可能在XML中,因此您可能必须使用自己的方法来查找图像。

To get the Header you do: 要获取标题,请执行以下操作:

XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(doc); // XWPFDocument
XWPFHeader header = policy.getDefaultHeader();

And to delete the images, get the XWPFRun from your paragraph (cell/row/table..) 要删除图像,请从您的段落(单元格/行/表..)中获取XWPFRun。

CTR ctr = myRun.getCTR(); // 
List<CTDrawing> images = ctr.getDrawingList();
for (int i=0; i<images.size(); i++)
{
    ctr.removeDrawing(i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM