Search code examples
openxmlpresentationml

Openxml: Added ImagePart is not showing in Powerpoint / Missing RelationshipID


I'm trying to dynamically create a PowerPoint presentation. One slide has a bunch of placeholder images that need to be changed based on certain values. My approach is to create a new ImagePart and link it to the according Blip. The image is downloaded and stored to the presentation just fine. The problem is, that there is no relationship created in slide.xml.rels file for the image, which leads to an warning about missing images and empty boxes on the slide. Any ideas what I am doing wrong?

Thanks in advance for your help! Best wishes

SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite siteCollection = new SPSite(SPContext.Current.Site.RootWeb.Url))
                {
                    using (SPWeb oWeb = siteCollection.OpenWeb())
                    {
                        SPList pictureLibrary = oWeb.Lists[pictureLibraryName];                           
                        SPFile imgFile = pictureLibrary.RootFolder.Files[imgPath];                                                   
                        byte[] byteArray = imgFile.OpenBinary();
                        int pos = Convert.ToInt32(name.Replace("QQ", "").Replace("Image", ""));
                        foreach (DocumentFormat.OpenXml.Presentation.Picture pic in pictureList)
                        {
                            var oldimg = pic.BlipFill.Blip.Embed.ToString();                                                               ImagePart ip = (ImagePart)slidePart.AddImagePart(ImagePartType.Png, oldimg+pos);
                            using (var writer = new BinaryWriter(ip.GetStream()))
                            {
                                writer.Write(byteArray);
                            }
                            string newId = slidePart.GetIdOfPart(ip);
                            setDebugMessage("new img id: " + newId);
                            pic.BlipFill.Blip.Embed = newId;
                        }

                        slidePart.Slide.Save();
                    }
                }
            });

Solution

  • So, for everyone who's experiencing a similar problem, I finally found the solution. Quite a stupid mistake. Instad of
    PresentationDocument document = PresentationDocument.Open(mstream, true);
    you have to use

    using (PresentationDocument document = PresentationDocument.Open(mstream, true))
    {
        do your editing here
    }
    

    This answer brought me on the right way.