I am trying to create a powerpoint and set a different image as background for each slide of my powerpoint. Unfortunatly, even if I success to change the background of my powerpoint by an image, my background will always be the last image of the loop.

In the code every image is name 'image('i')' with i the number of the image and I want the first image to be the background of the first slide, the second image the background of the second slide and etc...

If someone can help me please. Thanks

//Find number of slides to create
int nbSlide = new File(directoryScreen).listFiles().length;
//creating a new empty slide show
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlideMaster slideMaster = ppt.getSlideMasters().get(0);
List<XSLFSlideLayout> slides = new ArrayList<XSLFSlideLayout>();
for(int i=1; i<=nbSlide; i++) 
   XSLFPictureData pd = ppt.addPicture(new 
   File(directoryScreen+"\\image"+i+".png"), PictureType.PNG);
   XSLFSlideLayout slidelayout = slideMaster.getLayout(SlideLayout.BLANK);
   XSLFPictureShape ps = slidelayout.createPicture(pd);
   ps.setAnchor(new Rectangle2D.Double(0, 0, 960, 540));
   slides.add(slidelayout);
}
for(int i=0; i<nbSlide; i++) {
   XSLFSlide sl = ppt.createSlide(slides.get(i));
}

1 Answers

0
Axel Richter On

What you are doing with your code is not setting background pictures but setting picture shapes on always the same slide layout named "Blank". A slide master provides only one slide layout named "Blank", so the slideMaster.getLayout(SlideLayout.BLANK) always gets this same one slide layout.

When the need is really setting background pictures to the slides then this must be done using the background properties of the slides.

Example:

import java.io.FileOutputStream;
import java.io.FileInputStream;

import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.*;

import org.openxmlformats.schemas.presentationml.x2006.main.*;
import org.openxmlformats.schemas.drawingml.x2006.main.*;

import java.awt.Dimension;

public class CreatePPTXSheetsDifferentBackgroundPictures {

 public static void main(String[] args) throws Exception {

  XMLSlideShow slideShow = new XMLSlideShow();
  XSLFPictureData[] pictureDatas = new XSLFPictureData[]{
   slideShow.addPicture(new FileInputStream("Hydrangeas.jpg"), PictureData.PictureType.JPEG),
   slideShow.addPicture(new FileInputStream("Desert.jpg"), PictureData.PictureType.JPEG),
   slideShow.addPicture(new FileInputStream("Chrysanthemum.jpg"), PictureData.PictureType.JPEG)
  };

  // s slides, each having one different background picture out of pictureDatas array
  for (int s = 0; s < pictureDatas.length; s++ ) {
   XSLFSlide slide = slideShow.createSlide();
   CTBackgroundProperties backgroundProperties = slide.getXmlObject().getCSld().addNewBg().addNewBgPr();
   CTBlipFillProperties blipFillProperties = backgroundProperties.addNewBlipFill();
   CTRelativeRect ctRelativeRect = blipFillProperties.addNewStretch().addNewFillRect();
   String idx = slide.addRelation(null, XSLFRelation.IMAGES, pictureDatas[s]).getRelationship().getId();
   CTBlip blib = blipFillProperties.addNewBlip();
   blib.setEmbed(idx);
  }

  FileOutputStream out = new FileOutputStream("CreatePPTXSheetsDifferentBackgroundPictures.pptx");
  slideShow.write(out);
  out.close();
 }
}

This is the same what PowerPoints GUI is doing when Format Background is used.

Each different slide layout also has background properties. So the background properties also could be set on different slide layouts. Different slide layouts can be one out of all possible SlideLayout per slide master. If done then that background automatically is used on all slides which uses that slide layout.