none
Add image to PowerPoint SlideMaster using OpenXML RRS feed

  • Question

  • I am trying to add image to default Master slide with below code.Code is working fine without error but no image has been added to slide master Please help

    PresentationPart presentationPart = presentationDoc.PresentationPart; SlideMasterPart sectionSlidePart = (SlideMasterPart)presentationPart.GetPartById("rId1"); SlideMasterPart slieMaster = presentationPart.SlideMasterParts.FirstOrDefault(); ImagePart ip = slieMaster.AddImagePart(ImagePartType.Jpeg); FileStream file = new FileStream("c:\\file.jpg", FileMode.Open);

    ip.FeedData(file);



    Thanks


    • Edited by ashar2k11 Saturday, August 13, 2016 10:45 AM
    Saturday, August 13, 2016 10:44 AM

Answers

  • Hi Ashar2k11,

    >> I am trying to add image to default Master slide with below code.Code is working fine without error but no image has been added to slide master

    Based on your code, it inserts a file.jpg to PowerPoint files, if you want to insert it to SlideMaster, you need to add relationship between ImagePart and SlideMaster part.

    Here is a simple code:

            public void InsertImg(string filePath)
            {
                using (PresentationDocument ppt = PresentationDocument.Open(filePath, true))
                {
                    SlideMasterPart smp = ppt.PresentationPart.SlideMasterParts.First();
                    ImagePart ip = smp.AddImagePart(ImagePartType.Png, "rId13");
                    FileStream file = new FileStream(@"C:\Users\v-tazho\Desktop\Excel.png", FileMode.Open);
                    ip.FeedData(file);
    
    
                    ChangeSlideMasterPart(smp);
                }
            }
            public void ChangeSlideMasterPart(SlideMasterPart slideMasterPart1)
            {
                SlideMaster slideMaster1 = slideMasterPart1.SlideMaster;
    
                CommonSlideData commonSlideData1 = slideMaster1.GetFirstChild<CommonSlideData>();
    
                ShapeTree shapeTree1 = commonSlideData1.GetFirstChild<ShapeTree>();
    
                Picture picture1 = new Picture();
    
                NonVisualPictureProperties nonVisualPictureProperties1 = new NonVisualPictureProperties();
                NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)7U, Name = "Picture 6" };
    
                NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new NonVisualPictureDrawingProperties();
                A.PictureLocks pictureLocks1 = new A.PictureLocks() { NoChangeAspect = true };
    
                nonVisualPictureDrawingProperties1.Append(pictureLocks1);
                ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties() { UserDrawn = true };
    
                nonVisualPictureProperties1.Append(nonVisualDrawingProperties1);
                nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);
                nonVisualPictureProperties1.Append(applicationNonVisualDrawingProperties1);
    
                BlipFill blipFill1 = new BlipFill();
    
                A.Blip blip1 = new A.Blip() { Embed = "rId13" };
    
                A.BlipExtensionList blipExtensionList1 = new A.BlipExtensionList();
    
                A.BlipExtension blipExtension1 = new A.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" };
    
                A14.UseLocalDpi useLocalDpi1 = new A14.UseLocalDpi() { Val = false };
                useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
    
                blipExtension1.Append(useLocalDpi1);
    
                blipExtensionList1.Append(blipExtension1);
    
                blip1.Append(blipExtensionList1);
    
                A.Stretch stretch1 = new A.Stretch();
                A.FillRectangle fillRectangle1 = new A.FillRectangle();
    
                stretch1.Append(fillRectangle1);
    
                blipFill1.Append(blip1);
                blipFill1.Append(stretch1);
    
                ShapeProperties shapeProperties1 = new ShapeProperties();
    
                A.Transform2D transform2D1 = new A.Transform2D();
                A.Offset offset1 = new A.Offset() { X = 4985974L, Y = 2680911L };
                A.Extents extents1 = new A.Extents() { Cx = 485843L, Cy = 676369L };
    
                transform2D1.Append(offset1);
                transform2D1.Append(extents1);
    
                A.PresetGeometry presetGeometry1 = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
                A.AdjustValueList adjustValueList1 = new A.AdjustValueList();
    
                presetGeometry1.Append(adjustValueList1);
    
                shapeProperties1.Append(transform2D1);
                shapeProperties1.Append(presetGeometry1);
    
                picture1.Append(nonVisualPictureProperties1);
                picture1.Append(blipFill1);
                picture1.Append(shapeProperties1);
                shapeTree1.Append(picture1);
            }
    

    Best Regards,

    Edward


    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.


    • Marked as answer by ashar2k11 Monday, August 15, 2016 10:40 AM
    Monday, August 15, 2016 5:43 AM

All replies

  • Hi Ashar2k11,

    >> I am trying to add image to default Master slide with below code.Code is working fine without error but no image has been added to slide master

    Based on your code, it inserts a file.jpg to PowerPoint files, if you want to insert it to SlideMaster, you need to add relationship between ImagePart and SlideMaster part.

    Here is a simple code:

            public void InsertImg(string filePath)
            {
                using (PresentationDocument ppt = PresentationDocument.Open(filePath, true))
                {
                    SlideMasterPart smp = ppt.PresentationPart.SlideMasterParts.First();
                    ImagePart ip = smp.AddImagePart(ImagePartType.Png, "rId13");
                    FileStream file = new FileStream(@"C:\Users\v-tazho\Desktop\Excel.png", FileMode.Open);
                    ip.FeedData(file);
    
    
                    ChangeSlideMasterPart(smp);
                }
            }
            public void ChangeSlideMasterPart(SlideMasterPart slideMasterPart1)
            {
                SlideMaster slideMaster1 = slideMasterPart1.SlideMaster;
    
                CommonSlideData commonSlideData1 = slideMaster1.GetFirstChild<CommonSlideData>();
    
                ShapeTree shapeTree1 = commonSlideData1.GetFirstChild<ShapeTree>();
    
                Picture picture1 = new Picture();
    
                NonVisualPictureProperties nonVisualPictureProperties1 = new NonVisualPictureProperties();
                NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)7U, Name = "Picture 6" };
    
                NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new NonVisualPictureDrawingProperties();
                A.PictureLocks pictureLocks1 = new A.PictureLocks() { NoChangeAspect = true };
    
                nonVisualPictureDrawingProperties1.Append(pictureLocks1);
                ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties() { UserDrawn = true };
    
                nonVisualPictureProperties1.Append(nonVisualDrawingProperties1);
                nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);
                nonVisualPictureProperties1.Append(applicationNonVisualDrawingProperties1);
    
                BlipFill blipFill1 = new BlipFill();
    
                A.Blip blip1 = new A.Blip() { Embed = "rId13" };
    
                A.BlipExtensionList blipExtensionList1 = new A.BlipExtensionList();
    
                A.BlipExtension blipExtension1 = new A.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" };
    
                A14.UseLocalDpi useLocalDpi1 = new A14.UseLocalDpi() { Val = false };
                useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
    
                blipExtension1.Append(useLocalDpi1);
    
                blipExtensionList1.Append(blipExtension1);
    
                blip1.Append(blipExtensionList1);
    
                A.Stretch stretch1 = new A.Stretch();
                A.FillRectangle fillRectangle1 = new A.FillRectangle();
    
                stretch1.Append(fillRectangle1);
    
                blipFill1.Append(blip1);
                blipFill1.Append(stretch1);
    
                ShapeProperties shapeProperties1 = new ShapeProperties();
    
                A.Transform2D transform2D1 = new A.Transform2D();
                A.Offset offset1 = new A.Offset() { X = 4985974L, Y = 2680911L };
                A.Extents extents1 = new A.Extents() { Cx = 485843L, Cy = 676369L };
    
                transform2D1.Append(offset1);
                transform2D1.Append(extents1);
    
                A.PresetGeometry presetGeometry1 = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
                A.AdjustValueList adjustValueList1 = new A.AdjustValueList();
    
                presetGeometry1.Append(adjustValueList1);
    
                shapeProperties1.Append(transform2D1);
                shapeProperties1.Append(presetGeometry1);
    
                picture1.Append(nonVisualPictureProperties1);
                picture1.Append(blipFill1);
                picture1.Append(shapeProperties1);
                shapeTree1.Append(picture1);
            }
    

    Best Regards,

    Edward


    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.


    • Marked as answer by ashar2k11 Monday, August 15, 2016 10:40 AM
    Monday, August 15, 2016 5:43 AM
  • Thanks Edward I was missing the proper relation between  ImagePart and SlideMaster part

    Thanks

    Monday, August 15, 2016 10:41 AM
  • Here is another solution to add image to PowerPoint SlideMaster, check:

    Presentation ppt = new Presentation();
    ppt.LoadFromFile(FilePath + FileName);
    IMasterSlide master = ppt.Masters[0];
    String image = Path + Name;
    RectangleF rff = new RectangleF(40, 40, 100, 80);
    IEmbedImage pic = master.Shapes.AppendEmbedImage(ShapeType.Rectangle, image, rff);
    pic.Line.FillFormat.FillType = FillFormatType.None;
    ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
    Note that this solution is based on  a free ppt library.

    Tuesday, August 16, 2016 9:37 AM