My application needs to split presentations into individual slides. When doing this operation sequentially in a loop, it works fine with the following test case:
@Test
public void testSequentialSave() throws IOException {
Presentation presentation = new Presentation("C:\\Users\\z.a\\Desktop\\Test5.pptx");
presentation.getSlides().forEach(slide -> {
System.out.println(slide.getSlideId());
Presentation singleSlide = new Presentation();
singleSlide.getSlides().addClone(slide);
singleSlide.getSlides().removeAt(0);
ByteArrayOutputStream singleSlideOutputStream = new ByteArrayOutputStream();
singleSlide.save(singleSlideOutputStream, SaveFormat.Pptx);
try (OutputStream outputStream = new FileOutputStream(
"C:\\Users\\z.a\\Desktop\\Test1Result " + slide.getSlideId() + ".pptx")) {
singleSlideOutputStream.writeTo(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
});
}
However, this operation is slow. It takes on average 3 seconds to get one slide out. I trying to use parallel stream to speed up the process in the following way:
@Test
public void testParallelSave() throws IOException {
Presentation presentation = new Presentation("C:\\Users\\z.a\\Desktop\\Test1.pptx");
ISlide[] slidesArray = presentation.getSlides().toArray();
Arrays.asList(slidesArray).stream().parallel().forEach(slide -> {
System.out.println(
"Thread : " + Thread.currentThread().getName() + ", value: " + ((ISlide) slide).getSlideId());
Presentation singleSlide = new Presentation();
singleSlide.getSlides().addClone((ISlide) slide);
ByteArrayOutputStream singleSlideOutputStream = new ByteArrayOutputStream();
singleSlide.save(singleSlideOutputStream, SaveFormat.Pptx);
try (OutputStream outputStream = new FileOutputStream(
"C:\\Users\\z.a\\Desktop\\Test1Result " + slide.getSlideId() + ".pptx")) {
singleSlideOutputStream.writeTo(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
});
}
However this results in a error. Stack trace:
Caused by: java.lang.NullPointerException
at com.aspose.slides.ThreeDFormatPPTXSerialization.do(Unknown Source)
at com.aspose.slides.rq.do(Unknown Source)
at com.aspose.slides.bg.do(Unknown Source)
at com.aspose.slides.vs.do(Unknown Source)
at com.aspose.slides.ald.do(Unknown Source)
at com.aspose.slides.agp.do(Unknown Source)
at com.aspose.slides.agp.do(Unknown Source)
at com.aspose.slides.age.do(Unknown Source)
at com.aspose.slides.MasterSlideCollection.do(Unknown Source)
at com.aspose.slides.MasterSlideCollection.do(Unknown Source)
at com.aspose.slides.SlideCollection.addClone(Unknown Source)
at AsposeTests.lambda$1(AsposeTests.java:60)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.ForEachOps$ForEachTask.compute(ForEachOps.java:290)
at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:746)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Why is it not possible to perform this operation in parallel? Are there any other means to speed up the splitting of powerpoint file into individual slides?