有没有办法用apache poi用图片的名称替换word(docx)文件头上的图像?我在想这个:
+ -------------------------------- +
+ HEADER myimage.jpeg- +
+ ----------- BODY ------------ +
+ -------------------------------- +replaceImage(“myimage.jpeg”,newPictureInputStream, “newPicture_name.jpeg”);
这是我试过的:
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;
}
}
答案 0 :(得分:0)
我不确定您是否可以使用POI获取图像的“文件名”。它可能在XML中,因此您可能必须使用自己的方法来查找图像。
要获得标题:
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(doc); // XWPFDocument
XWPFHeader header = policy.getDefaultHeader();
要删除图像,请从段落中获取XWPFRun(单元格/行/表..)
CTR ctr = myRun.getCTR(); //
List<CTDrawing> images = ctr.getDrawingList();
for (int i=0; i<images.size(); i++)
{
ctr.removeDrawing(i);
}