Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I Am trying to add title and image to the PPT using Open XML

Here I am able to add the title and subtitle to the ppt template slide, My problem is for the same slide I need to add the Image.

Below is my code

C#
protected void Page_Load(object sender, EventArgs e)
        {
            
            InsertNewSlide(@"D:\PPTTEMPLATE\PPTTemplate.pptx", 1, "Title Only");
        }
public static void InsertNewSlide(string presentationFile, int position, string slideTitle)
        {
            // Open the source document as read/write. 
            using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, true))
            {
                // Pass the source document and the position and title of the slide to be inserted to the next method.
                InsertNewSlide(presentationDocument, position, slideTitle);
            }
        }

public static void InsertNewSlide(PresentationDocument presentationDocument, int position, string slideTitle)
        {

            if (presentationDocument == null)
            {
                throw new ArgumentNullException("presentationDocument");
            }

            if (slideTitle == null)
            {
                throw new ArgumentNullException("slideTitle");
            }

            PresentationPart presentationPart = presentationDocument.PresentationPart;

            // Verify that the presentation is not empty.
            if (presentationPart == null)
            {
                throw new InvalidOperationException("The presentation document is empty.");
            }

            // Declare and instantiate a new slide.
            Slide slide = new Slide(new CommonSlideData(new ShapeTree()));
            uint drawingObjectId = 1;

            // Construct the slide content.            
            // Specify the non-visual properties of the new slide.
            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());

            // Declare and instantiate the title shape of the new slide.
            Shape titleShape = slide.CommonSlideData.ShapeTree.AppendChild(new Shape());

            drawingObjectId++;

            // Specify the required shape properties for the title shape. 
            titleShape.NonVisualShapeProperties = new NonVisualShapeProperties
                (new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Title" },
                new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }),
                new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.Title }));
            titleShape.ShapeProperties = new ShapeProperties();

            // Specify the text of the title shape.
            titleShape.TextBody = new TextBody(new Drawing.BodyProperties(),
                    new Drawing.ListStyle(),
                    new Drawing.Paragraph(new Drawing.Run(new Drawing.Text() { Text = "hi" })));

            // Declare and instantiate the body shape of the new slide.
            Shape bodyShape = slide.CommonSlideData.ShapeTree.AppendChild(new Shape());
            drawingObjectId++;

            // Specify the required shape properties for the body shape.
            bodyShape.NonVisualShapeProperties = new NonVisualShapeProperties(new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Content Placeholder" },
                    new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }),
                    new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Index = 1 }));
            bodyShape.ShapeProperties = new ShapeProperties();

            // Specify the text of the body shape.
            bodyShape.TextBody = new TextBody(new Drawing.BodyProperties(),
                    new Drawing.ListStyle(),
                    new Drawing.Paragraph(new Drawing.Run(new Drawing.Text() { Text = "hi" })));
            
            // Create the slide part for the new slide.
            SlidePart slidePart = presentationPart.AddNewPart<SlidePart>();

            // Save the new slide part.
            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 presentation.
            presentationPart.Presentation.Save();
            
        }


How can I add image to this slide dynamically, please help me.
Posted
Updated 20-May-21 1:44am
v2

1 solution

passing in a byte[] named as image

add this above
// Insert the new slide into the slide list after the previous slide.
SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId);

C#
if (image!=null)
			{
                var part = slidePart.AddImagePart(ImagePartType.Png);
                part.FeedData(new MemoryStream(image));
				var tree = slidePart.Slide.Descendants<P.ShapeTree>().First();

				var picture = new P.Picture();

				picture.NonVisualPictureProperties = new P.NonVisualPictureProperties();
				picture.NonVisualPictureProperties.Append(new P.NonVisualDrawingProperties
				{
					Name = "My Shape",
					Id = (UInt32)tree.ChildElements.Count - 1
				});

				var nonVisualPictureDrawingProperties = new P.NonVisualPictureDrawingProperties();
				nonVisualPictureDrawingProperties.Append(new D.PictureLocks()
				{
					NoChangeAspect = true
				});
				picture.NonVisualPictureProperties.Append(nonVisualPictureDrawingProperties);
				picture.NonVisualPictureProperties.Append(new P.ApplicationNonVisualDrawingProperties());

				var blipFill = new P.BlipFill();
				var blip1 = new D.Blip()
				{
					Embed = slidePart.GetIdOfPart(part)
				};
				var blipExtensionList1 = new D.BlipExtensionList();
				var blipExtension1 = new D.BlipExtension()
				{
					Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"
				};
				var useLocalDpi1 = new DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi()
				{
					Val = false
				};
				useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
				blipExtension1.Append(useLocalDpi1);
				blipExtensionList1.Append(blipExtension1);
				blip1.Append(blipExtensionList1);
				var stretch = new D.Stretch();
				stretch.Append(new D.FillRectangle());
				blipFill.Append(blip1);
				blipFill.Append(stretch);
				picture.Append(blipFill);

				picture.ShapeProperties = new P.ShapeProperties();
				picture.ShapeProperties.Transform2D = new D.Transform2D();
				picture.ShapeProperties.Transform2D.Append(new D.Offset
				{
					X = 2500000,
					Y = 2500000,
				});
				picture.ShapeProperties.Transform2D.Append(new D.Extents
				{
					Cx = 5000000,
					Cy = 5000000,
				});
				picture.ShapeProperties.Append(new D.PresetGeometry
				{
					Preset = D.ShapeTypeValues.Rectangle
				});

				tree.Append(picture);
			}


this was taken and amended from here using "Md. Zakir Hossain" code..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900