How to create PPTX file using power-point-template

2019-09-17 17:04发布

问题:

Hi I want to create power-point presentation using power-point-template(which may be already exists or may be generated by poi.) for creating power point template file which have a background image in the slides, I write the following code which creates template file which opens in open-office but giving error to opening in Microsoft-power-point.

The Code is

private static void generatePOTX() throws IOException, FileNotFoundException {
     String imgPathStr = System.getProperty("user.dir") + "/src/resources/images/TestSameChnl_001_t.jpeg";
     File imgFile = new File(imgPathStr);
     File potxFile = new File(System.getProperty("user.dir") + "/src/resources/Examples/layout.potx");
     FileOutputStream out = new FileOutputStream(potxFile);
     HSLFSlideShow ppt = new HSLFSlideShow();
     HSLFSlide slide = ppt.createSlide();
     slide.setFollowMasterBackground(false);
     HSLFFill fill = slide.getBackground().getFill();
     HSLFPictureData pd = ppt.addPicture(imgFile,    PictureData.PictureType.JPEG);
     fill.setFillType(HSLFFill.FILL_PICTURE);
     fill.setPictureData(pd);  
     ppt.write(out);
     out.close();
}

After that I tried to create a PPT file using the generated POTX file but But it's giving error. I am trying bellow code for this. And the code is

private static void GeneratePPTXUsingPOTX() throws FileNotFoundException, IOException {
        File imgFile = new File(System.getProperty("user.dir")+"/src/resources/images/TestSameChnl_001_t.jpeg");
        File potx_File = new File(System.getProperty("user.dir") + "/src/resources/Examples/layout.potx" );
        File pptx_File = new File(System.getProperty("user.dir") + "/src/resources/Examples/PPTWithTemplate.pptx" );
        File movieFile = new File(System.getProperty("user.dir") + "/src/resources/movie/Dummy.mp4");
        FileInputStream ins = new FileInputStream(potx_File);
        FileOutputStream out = new FileOutputStream(pptx_File);

        HSLFSlideShow ppt = new HSLFSlideShow(ins);
        List<HSLFSlide> slideList = ppt.getSlides();
        int movieIdx = ppt.addMovie(movieFile.getAbsolutePath(), MovieShape.MOVIE_MPEG);
        HSLFPictureData pictureData = ppt.addPicture(imgFile, PictureData.PictureType.JPEG);
        MovieShape shape = new MovieShape(movieIdx, pictureData);
        shape.setAnchor(new java.awt.Rectangle(300,225,420,280));
        slideList.get(0).addShape(shape);
        shape.setAutoPlay(true);
        ppt.write(out);
        out.close();
    }

And the exception which is coming is as fallows:

java.lang.NullPointerException at org.apache.poi.hslf.usermodel.HSLFPictureShape.afterInsert(HSLFPictureShape.java:185) at org.apache.poi.hslf.usermodel.HSLFSheet.addShape(HSLFSheet.java:189)

Please suggest me what should I do.

Thanks