如何使用Apache POI使用power-point-template创建PPTX文件

时间:2016-02-25 13:31:24

标签: templates apache-poi powerpoint

您好我想使用power-point-template(可能已经存在或可能由poi生成)创建功率点演示,用于创建幻灯片中具有背景图像的功率点模板文件,我写了以下代码创建模板文件,该文件在开放式办公室打开,但在Microsoft-power-point打开时出错。

守则

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();
}

之后我尝试使用生成的POTX文件创建PPT文件但是 但是它给出了错误。我正在尝试以下代码。 代码是

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();
    }

即将到来的例外就像休耕一样:

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

请建议我该怎么做。

由于

0 个答案:

没有答案