Reputation:
I am using Apaches POI library version 5.0.0 to create documentation directly into PowerPoint slideshows (.pptx) from within a Java 17 desktop application.
I use a prepared slideshow in .pptx which include a title page, some slides with to-be-filled tables (as that POI version apparently has trouble creating those from source code) and a contact slide. This template (XMLSlideShow presentation
) is dynamically filled with
XSLFSlide slide = presentation.createSlide(...);
based on user input parameters, including generous usage of
presentation.setSlideOrder(slide, slidenumber);
to adjust their positioning according to my needs, mainly before or after said table-slides. Generally slidenumber
is basically just the index of the last added slide plus 1, but for ordering before or after table-slides I put them between already existing slides. According to https://poi.apache.org/components/slideshow/xslf-cookbook.html (sub heading "Re-order slides"), this is - to my understanding - how it should be used (..?). To my understanding it shifts the other slides, similar to an ArrayList
for example.
When I open said slideshow with PowerPoint 2019 it gives me the "file is corrupted"-message and prompting me to allow it to repair it. This works and the slideshow apparently remains unchanged. Various free/open source slideshow viewers/editors open the file without problem.
When inspecting the file closer (rename to zip, open to check on the directories and files), it became apparent that the actual content was exactly the same (to the byte) but in the slides directory the slide-xml-files were renamed. The visual order of the slides before/after repair in the file are the same, but the internal ordering apparently changed.
To check whether I messed something up in manually setting the slide order I simply tried checking
for (XSLFSlide slide : presentation.getSlides()) {
System.out.println(slide.getSlideNumber());
}
at the end just before writing but there (probably as expected) the slide numbers are in ascending order.
I also tried using POI 5.2.3 as it is almost 2.5 years newer and I thought it might be a bug, but with that version I couldn't even open my template-pptx.
I am also using POI 5.0.0 to again create documention in a pptx with a template file, but in that workflow there's no manual setting of the slide order. This results in non-corrupted slideshow-files, which is why I am putting my focus currently on the slide ordering/numbering.
Did someone have a similar problem and/or is there a way to validate the XMLSlideShow before writing?
Upvotes: 0
Views: 248
Reputation:
The problem is not related to reordering slides at all but to the content of the XMLSlideShow
. A small scale test revealed that adding a XSLFTextRun
into the document without setting the content in the newly returned instance reproduced the error message reliably.
A random discovery of https://stackoverflow.com/a/41859313/22470287 led to the solution of the problem, which is not only restricted to XSLFTable
:
XMLSlideShow presentation = new XMLSlideShow(stream);
XSLFTextBox textbox = presentation.getSlides().get(0).createTextBox();
XSLFTextParagraph paragraph = textbox.addNewTextParagraph();
XSLFTextRun textrun = paragraph.addNewTextRun();
textrun.setText("foo");
works perfectly, whereas
XMLSlideShow presentation = new XMLSlideShow(stream);
XSLFTextBox textbox = presentation.getSlides().get(0).createTextBox();
XSLFTextParagraph paragraph = textbox.addNewTextParagraph();
XSLFTextRun textrun = paragraph.addNewTextRun();
//textrun.setText("foo");
does produce the error at opening of the file.
Upvotes: 0