Search code examples
c#.netinteroppowerpointpowerpoint-interop

PowerPoint Interop order of add picture matters?


I have to automate the creation of a powerpoint presentation.

The master slide which is causing trouble has 2 pictureboxes and some textfields.

I'm adding pictures by getting the shape (the prepared picturebox) with an id and adding the picture at position shape.Left and shape.Right.

Now it gets wierd... When I do it like this, the pictures are positioned correctly.

var shape = slide.Shapes[ContentFields.Print.WithImage.Bild];
slide.Shapes.AddPicture(artikel.BildPath, MsoTriState.msoFalse, MsoTriState.msoCTrue, shape.Left, shape.Top, shape.Width, shape.Height);

shape = slide.Shapes[ContentFields.Print.WithImage.Kanal];
slide.Shapes.AddPicture(artikel.KanalIconPath, MsoTriState.msoFalse, MsoTriState.msoCTrue, shape.Left, shape.Top, shape.Width, shape.Height);

But when I add the Kanal first, the pictures are mixed up (Kanal is at the position of Bild and Bild at the position of Kanal).

var shape = slide.Shapes[ContentFields.Print.WithImage.Kanal];
slide.Shapes.AddPicture(artikel.KanalIconPath, MsoTriState.msoFalse, MsoTriState.msoCTrue, shape.Left, shape.Top, shape.Width, shape.Height);

shape = slide.Shapes[ContentFields.Print.WithImage.Bild];
slide.Shapes.AddPicture(artikel.BildPath, MsoTriState.msoFalse, MsoTriState.msoCTrue, shape.Left, shape.Top, shape.Width, shape.Height);

Why is this?
I'm getting the correct shape with the Id, how come they get mixed up? :-S

Thanks in advance


Solution

  • In at least some versions of PPT, if you add a picture to a slide that contains an empty Picture or Content placeholder, the picture is added TO the placeholder rather than being placed as a new shape.

    With two placeholders on the slide, the first added picture will land in the first placeholder, the second in the second placeholder, so the order in which you add pictures will matter.

    Try the same code on a slide with no placeholders; substitute rectangles instead if you need the shapes to determine picture placement.

    Or if you need to preserve the placeholders, temporarily add "dummy" text to any empty content placeholders before adding the pictures, then remove the dummy text afterwards.