poi 移除图片_用poi替换ppt中的文字和图片

这篇博客介绍了如何使用Apache POI库来读取PPTX文件,并替换其中包含特定标记的文字和图片。通过遍历每张幻灯片的形状,如果找到匹配的文本,就替换为新的内容;如果遇到图片标记,则替换为新的图片。
摘要由CSDN通过智能技术生成

try {

// 获取PPT文件

String pptModelPath =ConfigReadUtil.getInstance().getConfigItem("ppt_path");  //获取路径

FileInputStream is = new FileInputStream(pptModelPath+"2.pptx");  //读pptx存放的路径

XMLSlideShow ppt = new XMLSlideShow(is);

is.close();

for (XSLFSlide slide : ppt.getSlides()) {

// 获取每一张幻灯片中的shape

for (XSLFShape shape : slide.getShapes()) {

Rectangle2D anchor = shape.getAnchor();

if (shape instanceof XSLFTextShape) {

XSLFTextShape txShape = (XSLFTextShape) shape;

if (txShape.getText().contains("{time}")) {

// 替换文字内容

txShape.setText(txShape.getText().replace(

"{time}", time1));

}else if (txShape.getText().contains("{eight}")) {

// 替换文字内容

txShape.setText(txShape.getText().replace(

"{eight}", finalTime));

}else if (txShape.getText().contains("{pic}")) {  //替换图片

byte[] pictureData = IOUtils.toByteArray(new FileInputStream("C:\\Users\\gls\\Desktop\\1.jpg"));

int idx = ppt.addPicture(pictureData,XSLFPictureData.PICTURE_TYPE_PNG);

XSLFPictureShape pic = slide.createPicture(idx);

// 设置XSLFPictureShape的位置信息

pic.setAnchor(anchor);

// 移除XSLFTextShape

slide.removeShape(txShape);

}

else if (txShape.getText().contains("{today}")) {

// 替换文字内容

txShape.setText(txShape.getText().replace(

"{today}", firstTime));

} else if (txShape.getText().contains("{tomorrow}")) {

// 替换文字内容

txShape.setText(txShape.getText().replace(

"{tomorrow}", secondTime));

}else if (txShape.getText().contains("{the day after tomorrow}")) {

// 替换文字内容

txShape.setText(txShape.getText().replace(

"{the day after tomorrow}", thirdTime));

}

else if (txShape.getText().contains("{dwfirstRain}")) {

// 替换文字内容

txShape.setT

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值