增加ppt代码

This commit is contained in:
Mr-wang 2023-07-24 19:18:35 +08:00
parent 819a35ac24
commit da34bfdead

View File

@ -35,10 +35,12 @@ import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Color; import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.awt.*; import java.awt.*;
@ -401,6 +403,37 @@ public class UpFileExcelAndPptController {
} catch ( Exception e) { } catch ( Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
}else {
UpfileModel model_old = new UpfileModel();
model_old.setUuid(UUIDGener.getUUID());
model_old.setPl_uuid("f9df56d7-a6b2-442c-9898-822619057493");
model_old.setShape_uuid("obj_7ebfdb24f36e469cb841988cbda035c8");
model_old.setDownload(1);
model_old.setCreateUser(UserContext.fromSessionId(sid).getUID());
model_old.setCreateTime(new Timestamp(System.currentTimeMillis()));
model_old.setType("s");
model_old.setFileName("ppt版本模板"+name.substring(name.lastIndexOf(".")));
DCContext dcContext_old = getDCContext(model_old,UserContext.fromSessionId(sid));
String[] fileNames ={dcContext.getFilePath(),dcContext_old.getFilePath()} ;
String mergedFileName = dcContext.getFilePath();
try (FileOutputStream outputStream = new FileOutputStream(mergedFileName)) {
XMLSlideShow mergedSlideShow = new XMLSlideShow();
for (String fileName : fileNames) {
XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream(fileName));
for (XSLFSlide slide : slideShow.getSlides()) {
XSLFSlide newSlide = mergedSlideShow.createSlide().importContent(slide);
copyConditionalFormatting(slide, newSlide);
copyImages(slide, newSlide);
}
}
mergedSlideShow.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
} }
} }
JSONObject result = new JSONObject(); JSONObject result = new JSONObject();
@ -408,6 +441,53 @@ public class UpFileExcelAndPptController {
return result; return result;
} }
private static void copyConditionalFormatting(XSLFSlide sourceSlide, XSLFSlide targetSlide) {
List<XSLFShape> shapes = sourceSlide.getShapes();
for (XSLFShape shape : shapes) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape textShape = (XSLFTextShape) shape;
XSLFTextShape newShape = getMatchingTextShape(targetSlide, textShape);
if (newShape != null && newShape.getXmlObject() != null && newShape.getXmlObject() != null) {
textShape.getXmlObject().set(newShape.getXmlObject());
}
}
}
}
private static XSLFTextShape getMatchingTextShape(XSLFSlide slide, XSLFTextShape sourceShape) {
List<XSLFShape> shapes = slide.getShapes();
for (XSLFShape shape : shapes) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape textShape = (XSLFTextShape) shape;
if (textShape.getTextParagraphs().size() == sourceShape.getTextParagraphs().size()) {
return textShape;
}
}
}
return null;
}
private static void copyImages(XSLFSlide sourceSlide, XSLFSlide targetSlide) {
for (XSLFShape shape : sourceSlide.getShapes()) {
if (shape instanceof XSLFPictureShape) {
XSLFPictureShape pictureShape = (XSLFPictureShape) shape;
XSLFPictureData pictureData = pictureShape.getPictureData();
try {
byte[] pictureBytes = pictureData.getData();
String pictureType = pictureData.getContentType();
XSLFPictureData pictureIndex = targetSlide.getSlideShow().addPicture(pictureBytes, pictureData.getType());
XSLFPictureShape newPictureShape = targetSlide.createPicture(pictureIndex);
newPictureShape.setAnchor(pictureShape.getAnchor());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/** /**
*获取修订记录 *获取修订记录
* @param repositoryModel * @param repositoryModel