1

I'm using apache poi library to make a ppt file in my java program.

To insert elements, I added elements to a Document object.

the add order is below.

Picture1 -> rectangle1 -> Picture2 -> rectangle2

But, All Pictures is over all rectangles in output ppt file.

How to set z-order of elements such as the order of add?

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Hyunjin Lee
  • 113
  • 1
  • 17
  • when you say Document object, what class do you mean? Is it XMLSlideShow class for your presentation? If yes, then your slides are of XSLFSlide class, right? – DenisFLASH Jul 26 '14 at 09:06
  • 1
    it would be great if you added some code that you use into your question – DenisFLASH Jul 26 '14 at 09:08
  • Sorry, i've given you a code for creating **pptx**, but you asked about ppt. Do you need another example, this time using ppt, or pptx is okay? – DenisFLASH Jul 26 '14 at 13:21
  • if the answer helped you, please mark it as "accepted" with a green tick. Thank you. – DenisFLASH Jul 28 '14 at 08:24
  • @DenisFLASH I'm sorry to reply so late. I'm using 'org.apache.poi.hslf.usermodel.SlideShow' class for my presentation. – Hyunjin Lee Jul 31 '14 at 07:47
  • i have edited my answer, now it's using `..hslf.usermodel.SlideShow` , it works as well. Good luck! – DenisFLASH Jul 31 '14 at 14:28

1 Answers1

1

Well, here is what you can do. The order of adding is, as you see, Rectangle1, Picture, Rectangle2. And the z-order is respected (we can see all Rectangle2, but a Rectangle1 is partly hidden behind the image):

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hslf.model.PPGraphics2D;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.model.ShapeGroup;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;

public class PowerPointTest {

    public static void main( String[] args ) {

        SlideShow slideShow = new SlideShow();
        Slide slide = slideShow.createSlide();

        try {
            // Rectangle1 (partly hidden)
            fillRectangle( slide, Color.blue, 20, 20, 300, 300 );

            // Image
            int index = slideShow.addPicture(new File("IMG_8499.jpg"), Picture.JPEG);
            Picture picture = new Picture(index);
            picture.setAnchor(new Rectangle( 50, 50, 300, 200 ));
            slide.addShape(picture);

            // Rectangle2 (all visible)
            fillRectangle( slide, Color.yellow, 250, 150, 50, 10 );


            FileOutputStream out = new FileOutputStream( "z-order.ppt" );
            slideShow.write( out );
            out.close();
        } catch ( FileNotFoundException e ) {
            e.printStackTrace();
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }

private static void fillRectangle( Slide slide, Color color, int x, int y, int width, int height ) {
        // Objects are drawn into a shape group, so we need to create one
        ShapeGroup group = new ShapeGroup();
        // Define position of the drawing inside the slide
        Rectangle bounds = new Rectangle(x, y, width, height);

        group.setAnchor(bounds);
        slide.addShape(group);

        // Drawing a rectangle
        Graphics2D graphics = new PPGraphics2D(group);
        graphics.setColor(color);
        graphics.fillRect(x, y, width, height); 
    }
}

Take a look at this tutorial. If you need not just to a draw a rectangle using lines, but, for example, adding a table with text cells, see examples here. Hope it helps!

DenisFLASH
  • 734
  • 1
  • 9
  • 14