org.apache.poi.xslf.usermodel send image behind the text

Tassio Gustavo :

I am making an application that will create a power point slide show. I'm managing to put an image on every slide but it overlays the written text. How to put the text in front of the image?

This is my code.


public void createNewSlide() throws FileNotFoundException, IOException{
        XMLSlideShow ppt = new XMLSlideShow();

        XSLFSlideMaster master = ppt.getSlideMasters().get(0);
        XSLFSlideLayout layout = master.getLayout(SlideLayout.TITLE_ONLY);

        //propiedades do slide
        ppt.setPageSize(new Dimension(1280,720));
        XSLFSlide slide = ppt.createSlide(layout);

        //primeiro slide, começando com texto
        XSLFTextShape title = slide.getPlaceholder(0);
        title.clearText();
        XSLFTextParagraph paragraph = title.addNewTextParagraph();
        XSLFTextRun textRun = paragraph.addNewTextRun();
        textRun.setText("Simple Text");
        textRun.setFontColor(Color.decode("#00ff99"));
        textRun.setFontSize(60.);
        title.setAnchor(new Rectangle(100,100,500,500));

        //adicionando imagem ao ppt
        byte[] picData = IOUtils.toByteArray(new FileInputStream("espace.jpg"));
        XSLFPictureData pcData = ppt.addPicture(picData, PictureData.PictureType.JPEG);
        XSLFPictureShape pictureShape = slide.createPicture(pcData);
        pictureShape.setAnchor(new Rectangle(0,0,1280,720));



        FileOutputStream out = new FileOutputStream("AprentacaoTeste.pptx");
        ppt.write(out);
        out.close();
        ppt.close();
    }

these are my dependencies

<dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>
    </dependencies>

i'm using netbeans as IDE

Axel Richter :

Your inserted picture is the last shape in shape tree. So it is in front of all other shapes in shape tree. It would must be the first shape in shape tree, to be behind all other shapes. But it is very hard to change the shape tree' s order if shapes are already added. And the placeholders are already present in layout before other shapes were added.

But I think, what you trying to do is setting a background picture to the slide. This also is only possible using the underlying ooxml-schemas classes until now. But it is much more straight forward than changing the shape tree' s order.

Example:

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

import org.apache.poi.util.IOUtils;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xslf.usermodel.*;

import org.openxmlformats.schemas.presentationml.x2006.main.CTBackgroundProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRelativeRect;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Color;

public class CreatePPTXPictureBehindText {

 static void setBackgroundPicture(XSLFSlide slide, String picturePath, PictureData.PictureType pictureType) throws Exception {
  byte[] picData = IOUtils.toByteArray(new FileInputStream(picturePath));
  XSLFPictureData pcData = slide.getSlideShow().addPicture(picData, pictureType);
  CTBackgroundProperties backgroundProperties = slide.getXmlObject().getCSld().addNewBg().addNewBgPr();
  CTBlipFillProperties blipFillProperties = backgroundProperties.addNewBlipFill();
  CTRelativeRect ctRelativeRect = blipFillProperties.addNewStretch().addNewFillRect();
  String idx = slide.addRelation(null, XSLFRelation.IMAGES, pcData).getRelationship().getId();
  CTBlip blib = blipFillProperties.addNewBlip();
  blib.setEmbed(idx);
 }

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

  XMLSlideShow ppt = new XMLSlideShow();

  //set page size
  ppt.setPageSize(new Dimension(1280,720));

  //create first slide title layout
  XSLFSlideMaster master = ppt.getSlideMasters().get(0);
  XSLFSlideLayout layout = master.getLayout(SlideLayout.TITLE_ONLY);
  XSLFSlide slide = ppt.createSlide(layout);

  //set title placeholder's text and anchor
  XSLFTextShape title = slide.getPlaceholder(0);
  title.clearText();
  XSLFTextParagraph paragraph = title.addNewTextParagraph();
  XSLFTextRun textRun = paragraph.addNewTextRun();
  textRun.setText("Simple Text");
  textRun.setFontColor(Color.decode("#00ff99"));
  textRun.setFontSize(60.);
  title.setAnchor(new Rectangle(100,100,500,500));

  //set background picture for slide
  setBackgroundPicture(slide, "./espace.jpeg", PictureData.PictureType.JPEG);

  FileOutputStream out = new FileOutputStream("./AprentacaoTeste.pptx");
  ppt.write(out);
  out.close();
  ppt.close();
 }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=362616&siteId=1