Bug 59273

Summary: Unable to create pptx file by potx file using Apache POI
Product: POI Reporter: Rahul <rahul.kr.sharma29>
Component: XSLFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 3.13-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Bug Depends on:    
Bug Blocks: 59061    
Attachments: Potx file by which I want to create pptx.

Description Rahul 2016-04-05 11:27:18 UTC
Created attachment 33728 [details]
Potx file by which I want to create pptx.

Hi I am trying to create power-point presentation(pptx) file with potx power point presentation template using Apache POI. The file was generated but giving error "The File format or File extension is not valid Verify that the file has not been corrupted and that the file extension matches the format of the file." when trying to open it up . I am using below code Kindly see what I am doing wrong.


public static void createPPTXWithPOTX() throws Exception
 {

        File potx_File = new File(System.getProperty("user.dir") + "/src/resources/Examples/SelfMadePOTX.potx");

        File pptx_File = new File(System.getProperty("user.dir") + "/src/resources/Examples/PPTWithTemplate12.pptx");

        FileInputStream ins = new FileInputStream(potx_File);
        FileOutputStream out = new FileOutputStream(pptx_File);
        XMLSlideShow ppt = new XMLSlideShow(ins);
        // first see what slide layouts are available :
        System.out.println("Available slide layouts:");
        for (XSLFSlideMaster master : ppt.getSlideMasters()) {
            for (XSLFSlideLayout layout : master.getSlideLayouts()) {
                System.out.println(layout.getType());
            }
        }

        // blank slide
        XSLFSlide blankSlide = ppt.createSlide();
    // there can be multiple masters each referencing a number of layouts
        // for demonstration purposes we use the first (default) slide master
        XSLFSlideMaster defaultMaster = ppt.getSlideMasters()[0];
        // title slide
        XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE);
        // fill the placeholders
        XSLFSlide slide1 = ppt.createSlide(titleLayout);
        XSLFTextShape title1 = slide1.getPlaceholder(0);
        title1.setText("First Title");

        // title and content
        XSLFSlideLayout titleBodyLayout = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
        XSLFSlide slide2 = ppt.createSlide(titleBodyLayout);

        XSLFTextShape title2 = slide2.getPlaceholder(0);
        title2.setText("Second Title");

        XSLFTextShape body2 = slide2.getPlaceholder(1);
        body2.clearText(); // unset any existing text
        body2.addNewTextParagraph().addNewTextRun().setText("First paragraph");
        body2.addNewTextParagraph().addNewTextRun().setText("Second paragraph");
        body2.addNewTextParagraph().addNewTextRun().setText("Third paragraph");

        ppt.write(out);
    }
Comment 1 Andreas Beeker 2016-11-10 09:41:15 UTC
The trick is to change the content-type of the presentation:

ppt.getPackage().replaceContentType(
XSLFRelation.PRESENTATIONML_TEMPLATE, XSLFRelation.MAIN
);

... but the new content type is not saved.

I'm on it ...
Comment 2 Andreas Beeker 2016-11-10 22:57:00 UTC
The content-type replacement was fixed via r1769220
Comment 3 Andreas Beeker 2018-05-18 17:08:12 UTC
*** Bug 59061 has been marked as a duplicate of this bug. ***