none
Unable to add an Image to a powerpoint presentation. RRS feed

  • Question

  • Hi,

    I am using the following code to add a new slide to a ppt file and add an image. I am using Open XML 2.5 SDK.

    A new slide is getting added but not the image. Is there anything wrong in this code?

    int position = 1;
                using (PresentationDocument presentationDocument = PresentationDocument.Open("c.pptx", true))
                {

                    PresentationPart presentationPart = presentationDocument.PresentationPart;

                    Slide slide = new Slide(new CommonSlideData(new ShapeTree()));
       
                    NonVisualGroupShapeProperties nonVisualProperties = slide.CommonSlideData.ShapeTree.AppendChild(new NonVisualGroupShapeProperties());
                    nonVisualProperties.NonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = 1, Name = "" };
                    nonVisualProperties.NonVisualGroupShapeDrawingProperties = new NonVisualGroupShapeDrawingProperties();
                    nonVisualProperties.ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();

                    // Specify the group shape properties of the new slide.
                    slide.CommonSlideData.ShapeTree.AppendChild(new GroupShapeProperties());

                    // Create the slide part for the new slide.
                    SlidePart slidePart = presentationPart.AddNewPart<SlidePart>();


                    // Save the new slide part.
                    slide.Save(slidePart);
                    string imgId = "rId" + new Random().Next(2000).ToString();
                    ImagePart imagePart = slidePart.AddImagePart(ImagePartType.Png, imgId); 

                    using (FileStream stream = new FileStream("a.png", FileMode.Open))
                    {
                        stream.Position = 0;
                        imagePart.FeedData(stream);
                    }

                    

                    slide.Save(slidePart);
                    



                    

                    // Modify the slide ID list in the presentation part.
                    // The slide ID list should not be null.
                    SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;

                    // Find the highest slide ID in the current list.
                    uint maxSlideId = 1;
                    SlideId prevSlideId = null;

                    foreach (SlideId slideId in slideIdList.ChildElements)
                    {
                        if (slideId.Id > maxSlideId)
                        {
                            maxSlideId = slideId.Id;
                        }

                        position--;
                        if (position == 0)
                        {
                            prevSlideId = slideId;
                        }

                    }

                    maxSlideId++;

                    // Get the ID of the previous slide.
                    SlidePart lastSlidePart;

                    if (prevSlideId != null)
                    {
                        lastSlidePart = (SlidePart)presentationPart.GetPartById(prevSlideId.RelationshipId);
                    }
                    else
                    {
                        lastSlidePart = (SlidePart)presentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId);
                    }


                    // Use the same slide layout as that of the previous slide.
                    if (null != lastSlidePart.SlideLayoutPart)
                    {
                        slidePart.AddPart(lastSlidePart.SlideLayoutPart);
                    }

                    // Insert the new slide into the slide list after the previous slide.
                    SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId);
                    newSlideId.Id = maxSlideId;
                    newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart);

                    // Save the modified prsentation.
                    presentationPart.Presentation.Save();

    Thanks in advance.

    Thursday, March 6, 2014 9:31 AM

Answers

  • Hi,

    When you use the slidePart.AddImagePart(ImagePartType.Png, imgId) to add image into presentation, the imgId needs to be the relationshipId that corresponds to your image that you are adding to the presentation. You could also leave that parameter blank and a default relationship id will be created for you, and then you need to feed that image part the actual image.

    Please refer to following code to add image into presentation:

    using A = DocumentFormat.OpenXml.Drawing;
    using P = DocumentFormat.OpenXml.Presentation;
    public void InsertImageInSlide(Slide slide, string imagePath, string imageExt)
            {
                // Creates a Picture instance and adds its children.
                P.Picture picture = new P.Picture();
                string embedId = string.Empty;
                embedId = "rId" + (slide.Elements<P.Picture>().Count() + 915).ToString();
                P.NonVisualPictureProperties nonVisualPictureProperties = new P.NonVisualPictureProperties(
                    new P.NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Picture 5" },
                    new P.NonVisualPictureDrawingProperties(new A.PictureLocks() { NoChangeAspect = true }),
                    new ApplicationNonVisualDrawingProperties());
    
                P.BlipFill blipFill = new P.BlipFill();
                Blip blip = new Blip() { Embed = embedId };
    
                // Creates a BlipExtensionList instance and adds its children
                BlipExtensionList blipExtensionList = new BlipExtensionList();
                BlipExtension blipExtension = new BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" };
    
                UseLocalDpi useLocalDpi = new UseLocalDpi() { Val = false };
                useLocalDpi.AddNamespaceDeclaration("a14",
                    "http://schemas.microsoft.com/office/drawing/2010/main");
    
                blipExtension.Append(useLocalDpi);
                blipExtensionList.Append(blipExtension);
                blip.Append(blipExtensionList);
    
                Stretch stretch = new Stretch();
                FillRectangle fillRectangle = new FillRectangle();
                stretch.Append(fillRectangle);
    
                blipFill.Append(blip);
                blipFill.Append(stretch);
    
                // Creates a ShapeProperties instance and adds its children.
                P.ShapeProperties shapeProperties = new P.ShapeProperties();
    
                A.Transform2D transform2D = new A.Transform2D();
                A.Offset offset = new A.Offset() { X = 457200L, Y = 1524000L };
                A.Extents extents = new A.Extents() { Cx = 8229600L, Cy = 5029200L };
    
                transform2D.Append(offset);
                transform2D.Append(extents);
    
                A.PresetGeometry presetGeometry = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
                A.AdjustValueList adjustValueList = new A.AdjustValueList();
    
                presetGeometry.Append(adjustValueList);
    
                shapeProperties.Append(transform2D);
                shapeProperties.Append(presetGeometry);
    
                picture.Append(nonVisualPictureProperties);
                picture.Append(blipFill);
                picture.Append(shapeProperties);
    
                slide.CommonSlideData.ShapeTree.AppendChild(picture);
    
                // Generates content of imagePart.
                ImagePart imagePart = slide.SlidePart.AddNewPart<ImagePart>(imageExt, embedId);
                FileStream fileStream = new FileStream(imagePath, FileMode.Open);
                imagePart.FeedData(fileStream);
                fileStream.Close();
            }
    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, March 7, 2014 8:36 AM
    Moderator