From da34bfdead8900000b7e54e85bb11d1d73225a7f Mon Sep 17 00:00:00 2001 From: Mr-wang Date: Mon, 24 Jul 2023 19:18:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ppt=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zd/UpFileExcelAndPptController.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/com.awspaas.user.apps.coe.pal.output.zd/src/com/awspaas/apps/coe/pal/output/zd/UpFileExcelAndPptController.java b/com.awspaas.user.apps.coe.pal.output.zd/src/com/awspaas/apps/coe/pal/output/zd/UpFileExcelAndPptController.java index 3c5dca61..1b2ea946 100644 --- a/com.awspaas.user.apps.coe.pal.output.zd/src/com/awspaas/apps/coe/pal/output/zd/UpFileExcelAndPptController.java +++ b/com.awspaas.user.apps.coe.pal.output.zd/src/com/awspaas/apps/coe/pal/output/zd/UpFileExcelAndPptController.java @@ -35,10 +35,12 @@ import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.Cell; 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.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody; import org.springframework.util.StringUtils; import java.awt.*; @@ -401,6 +403,37 @@ public class UpFileExcelAndPptController { } catch ( Exception e) { 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(); @@ -408,6 +441,53 @@ public class UpFileExcelAndPptController { return result; } + + private static void copyConditionalFormatting(XSLFSlide sourceSlide, XSLFSlide targetSlide) { + List 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 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