我想合并多个ppt。我使用POI实现了大部分功能,但仍然存在一些问题。有些元素没有生成。我测试了几组ppt。
案例1:如果PPT中只有一张幻灯片,结果是正确的。如果有多张幻灯片,就会抛出异常。下面是异常堆栈:不能在org.apache.poi.xslf.usermodel.XSLFSheet.importBlip(XSLFSheet.java:649) at org.apache.poi.xslf.usermodel.XSLFPictureShape.copy(XSLFPictureShape.java:378) at org.apache.poi.xslf.usermodel.XSLFSheet.wipeAndReinitialize(XSLFSheet.java:454) at org.apache.poi.xslf.usermodel.XSLFSheet.importContent(XSLFSheet.java:433)将java.lang.ClassCastException: org.apache.poi.ooxml.POIXMLDocumentPart转换为org.apache.poi.xslf.usermodel.XSLFPictureData在org.apache.poi.xslf.usermodel.XSLFSlide.importContent(XSLFSlide.java:294) at com.office.MergingMultiplePresentations.main(MergingMultiplePresentations.java:38)
案例2:我测试了另一个PPT,当我打开它时,它会提示“内容有问题,您可以尝试修复它”。当我单击“修复”时,PPT的一些幻灯片被删除。有什么东西没有被复制吗?
这是我的代码:
XMLSlideShow ppt = new XMLSlideShow();
//taking the two presentations that are to be merged
String path = "E:\\prj\\test\\";
String file1 = "1.pptx";
String file2 = "2.pptx";
String[] inputs = {file1,file2};
for(String arg : inputs){
FileInputStream inputstream = new FileInputStream(path+arg);
XMLSlideShow src = new XMLSlideShow(inputstream);
for(XSLFSlide srcSlide : src.getSlides()) {
try {
XSLFSlideLayout srcLayout = srcSlide.getSlideLayout();
XSLFSlideMaster srcMaster = srcSlide.getSlideMaster();
XSLFSlide slide = ppt.createSlide();
XSLFSlideLayout layout = slide.getSlideLayout();
XSLFSlideMaster master = slide.getSlideMaster();
layout.importContent(srcLayout);
master.importContent(srcMaster);
slide.importContent(srcSlide);
}
catch (Exception e){
e.printStackTrace();
}
}
}
String file3 = "3.pptx";
//creating the file object
FileOutputStream out = new FileOutputStream(path+file3);
// saving the changes to a file
ppt.write(out);
out.close();发布于 2022-04-08 13:52:15
使用POI合并演示文稿的操作看起来有点麻烦,因为您必须自己处理布局和主程序。在这方面使用Aspose.Slides for Java更容易。下面的代码示例演示如何使用该库合并演示文稿。幻灯片布局和幻灯片母版将自动合并。
String file1 = "1.pptx";
String file2 = "2.pptx";
String[] inputs = {file1, file2};
// Prepare a new empty presentation.
Presentation ppt = new Presentation();
ppt.getSlides().removeAt(0); // removes the first empty slide
ppt.getSlideSize().setSize(SlideSizeType.Widescreen, SlideSizeScaleType.Maximize);
// Merge the input presentations.
for (String file : inputs) {
Presentation source = new Presentation(file);
for (ISlide slide : source.getSlides()) {
ppt.getSlides().addClone(slide);
}
source.dispose();
}
ppt.save("3.pptx", SaveFormat.Pptx);
ppt.dispose();这是一款付费产品,但您可以获得试用的临时许可证。
或者,您可以使用Aspose.Slides Cloud SDK for Java。该产品提供了一个基于REST的API,允许您每月为API学习和演示处理进行150个免费API调用。下面的代码示例向您展示了如何使用Aspose.Slides云进行相同操作:
SlidesApi slidesApi = new SlidesApi("my_client_id", "my_client_secret");
String file1 = "1.pptx";
String file2 = "2.pptx";
String outFile = "3.pptx";
// Prepare a new empty presentation.
slidesApi.createPresentation(outFile, null, null, null, null, null);
slidesApi.deleteSlide(outFile, 1, null, null, null); // removes the first empty slide
SlideProperties slideProperties = new SlideProperties();
slideProperties.setSizeType(SlideProperties.SizeTypeEnum.WIDESCREEN);
slideProperties.setScaleType(SlideProperties.ScaleTypeEnum.MAXIMIZE);
slidesApi.setSlideProperties(outFile, slideProperties, null, null, null);
// Merge the input presentations.
PresentationsMergeRequest mergeRequest = new PresentationsMergeRequest();
mergeRequest.setPresentationPaths(Arrays.asList(file1, file2));
slidesApi.merge(outFile, mergeRequest, null, null, null);有时,需要在没有任何代码的情况下合并演示文稿。对于这种情况,您可以使用免费的Aspose在线合并。
我是Aspose的一个支持开发人员。
https://stackoverflow.com/questions/71723007
复制相似问题