简体   繁体   中英

Apache POI - Error while merging pptx

I have a scenario where I need to copy few slides from a pptx (source.pptx) and download it as a separate pptx file (output.pptx) based on the presentation notes available in the slides. I am using apache poi to achieve it. This is my code.

String filename = filepath+"\\source.pptx";
    try {
        XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(filename));
        XMLSlideShow outputppt = new XMLSlideShow();
        XSLFSlide[] slides = ppt.getSlides();

        for (int i = 0; i < slides.length; i++) {
            try {
                XSLFNotes mynotes = slides[i].getNotes();
                for (XSLFShape shape : mynotes) {
                    if (shape instanceof XSLFTextShape) {
                        XSLFTextShape txShape = (XSLFTextShape) shape;
                        for (XSLFTextParagraph xslfParagraph : txShape.getTextParagraphs()) {
                            if (xslfParagraph.getText().equals("NOTES1") || xslfParagraph.getText().equals("NOTES2")) {
                                outputppt.createSlide().importContent(slides[i]);
                            }

                        }
                    }
                }
            } catch (Exception e) {

            }

        }
        FileOutputStream out = new FileOutputStream("output.pptx");
        outputppt.write(out);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

When I open the output.pptx which is created, I am getting the following error: "PowerPoint found a problem with the content in output.pptx PowerPoint can attempt to repair the presentation If you trust the source of this presentation, click Repair."

Upon clicking repair: "PowerPoint removed unreadable content in merged.pptx [Repaired]. You should review this presenation to determine whether any content was unexpectedly changed or removed" And I can see blank slides with "Click to add Title" and "Click to add Subtitle"

Any suggestions to solve this issue?

This code works for me to copy slide content, layout and notes. Just modify the code to your needs if you want to follow your original question. I assume you simple have to:

  1. not import the slide content from it's source slide
  2. copy the notes content to the slide instead

      // get the layout from the source slide XSLFSlideLayout layout = srcSlide.getSlideLayout(); XSLFSlide newslide = ppt .createSlide(defaultMaster.getLayout(layout.getType())) .importContent(srcSlide); XSLFNotes srcNotes = srcSlide.getNotes(); XSLFNotes newNotes = ppt.getNotesSlide(newslide); newNotes.importContent(srcNotes); 

I had the same error in a case where some text boxes were empty. Solved it by always setting an empty text in all placeholders when creating slides.

XSLFSlide slide = presentation.createSlide(slideMaster.getLayout(layout));
// remove any placeholder texts
for (XSLFTextShape ph : slide.getPlaceholders()) {
  ph.clearText();
  ph.setText("");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM