Bug 59061

Summary: How to create PPTX using power-point-template file
Product: POI Reporter: Rahul <rahul.kr.sharma29>
Component: XSLFAssignee: POI Developers List <dev>
Status: RESOLVED DUPLICATE    
Severity: normal    
Priority: P2    
Version: 3.14-FINAL   
Target Milestone: ---   
Hardware: All   
OS: All   
Bug Depends on: 59273    
Bug Blocks:    

Description Rahul 2016-02-24 10:25:32 UTC
Hi I am trying to create a template file(with .potx file name extension) and using the template as base wants to create pptx file.
I am trying to create template file using a image file as background of slide but the generated potx file doesn't have any background image, file has been created with blank slide. I used the bellow Code:

public class CreateTemplate {
    public static void main(String[] args) throws Exception {
        String imgPathStr = System.getProperty("user.dir") + "/src/resources/images/TestSameChnl_001_t.jpg";
        File imgFile = new File(imgPathStr);
        File potxFile = new File(System.getProperty("user.dir") + "/src/resources/Examples/layout_v0.potx");
        FileOutputStream out = new FileOutputStream(potxFile);

        XMLSlideShow ppt = new XMLSlideShow();
        //creating a slide in it 
        XSLFSlide slide = ppt.createSlide();

        byte[] rawData = getRawBytesFromFile(imgPathStr); // some code to read raw bytes from image file
        ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(rawData));
        BufferedImage img = ImageIO.read(iis);

        Dimension dimension = ppt.getPageSize();
//        XSLFShape[] shapes = slide.getShapes();
//        BufferedImage img = new BufferedImage(dimension.width, dimension.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = img.createGraphics();
//        graphics.setPaint(f);
        graphics.fill(new Rectangle2D.Float(0, 0, dimension.width, dimension.height));
        slide.draw(graphics);//getBackground().setLineColor(Color.GREEN);//tFillColor(Color.GREEN);//draw(graphics);
        ppt.write(out);
        System.out.println("image added successfully");
        out.close();        
    }    
    private static byte[] getRawBytesFromFile(String path) throws FileNotFoundException, IOException {
        byte[] image;
        File file = new File(path);
        image = new byte[(int)file.length()];

        FileInputStream fileInputStream = new FileInputStream(file);
        fileInputStream.read(image);

        return image;
    }
} 

After creating potx file I am trying to create pptx file using potx file and used the bellow Java code:



import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFSlide;

/**
 *
 * @author rahul.sharma
 */
public class PPTWithPotx_Template_Example {

    static DecimalFormat df_time = new DecimalFormat("0.####");
    
    public static void main(String[] args) throws Exception{
        File imgFile = new File(System.getProperty("user.dir")+"/src/resources/images/TestSameChnl_001_t.jpg");
        File potx_File = new File(System.getProperty("user.dir") + "/src/resources/Examples/layout_v0.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);
        XMLSlideShow ppt = new XMLSlideShow(ins);
        XSLFSlide slide = ppt.createSlide();
        byte[] rawData = getRawBytesFromFile(imgFile.getAbsolutePath()); // some code to read raw bytes from image file
        ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(rawData));
        BufferedImage img = ImageIO.read(iis);
        XSLFPictureData pictureData = ppt.addPicture(rawData, PictureData.PictureType.JPEG);
        ppt.write(out);
        out.close();
    }
    
    private static byte[] getRawBytesFromFile(String path) throws FileNotFoundException, IOException {

        byte[] image;
        File file = new File(path);
        image = new byte[(int)file.length()];

        FileInputStream fileInputStream = new FileInputStream(file);
        fileInputStream.read(image);

        return image;
    }
    
}

Please help me to generate pots file and then from potx to ppt.
Thanks in advance.

Thanks & Regards
Rahul Kumar Sharma.
Comment 1 Rahul 2016-02-26 08:15:01 UTC
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)



I used this code on poi-3.13-20150929.jar as well as poi-3.14-beta1-20151223.jar the latest one and both have the same result.

Please suggest me what should I do.


Thanks
Comment 2 Andreas Beeker 2018-05-18 17:08:12 UTC

*** This bug has been marked as a duplicate of bug 59273 ***