Change the fill color of a shape in a presentation

  • OpenXML SDK
  • Spire.Presentation
  • Download Sample Code

class Program
    {
        static void Main(string[] args)
        {
            string docName = @"..\..\Documents\Myppt3.pptx";
            SetPPTShapeColor(docName);
        }
        // Change the fill color of a shape.
        // The test file must have a filled shape as the first shape on the first slide.
        public static void SetPPTShapeColor(string docName)
        {
            using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
            {
                // Get the relationship ID of the first slide.
                PresentationPart part = ppt.PresentationPart;
                OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;
                string relId = (slideIds[0] as SlideId).RelationshipId;

                // Get the slide part from the relationship ID.
                SlidePart slide = (SlidePart)part.GetPartById(relId);

                if (slide != null)
                {
                    // Get the shape tree that contains the shape to change.
                    ShapeTree tree = slide.Slide.CommonSlideData.ShapeTree;

                    // Get the first shape in the shape tree.
                    Shape shape = tree.GetFirstChild();

                    if (shape != null)
                    {
                        // Get the style of the shape.
                        ShapeStyle style = shape.ShapeStyle;

                        // Get the fill reference.
                        Drawing.FillReference fillRef = style.FillReference;

                        // Set the fill color to SchemeColor Accent 6;
                        fillRef.SchemeColor = new Drawing.SchemeColor();
                        fillRef.SchemeColor.Val = Drawing.SchemeColorValues.Accent6;

                        // Save the modified slide.
                        slide.Slide.Save();
                    }
                }
            }
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            string docName = @"..\..\Documents\Myppt3.pptx";
            SetPPTShapeColor(docName);
        }
        public static void SetPPTShapeColor(string docName)
        {
            //Initialize an instances of Presentation class and load the sample PowerPoint file
            Presentation presentation = new Presentation();
            presentation.LoadFromFile(docName);

            //Get the shape
            IAutoShape shape = presentation.Slides[0].Shapes[0] as IAutoShape;

            //Change the fill color of the shape
            shape.Fill.FillType = FillFormatType.Solid;
            shape.Fill.SolidColor.Color = Color.Yellow;

            //Save PPT document
            presentation.SaveToFile(@"..\..\Documents\result.pptx", FileFormat.Pptx2010);
        }
    }