1

I am trying to extract color of the font from a PPTx file , I am a beginner using Open XML SDK. My aim is to extract the font color, replace the color and save the file. Here is what I have tried so far.

using Drawing = DocumentFormat.OpenXml.Drawing;
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;
            Console.WriteLine(relId);

            // Get the slide part from the relationship ID.
            SlidePart slidepart = (SlidePart)part.GetPartById(relId);
            
            
            if (slidepart != null && slidepart.Slide!=null)
            {
                // Get the shape tree that contains the shape to change
                ShapeTree tree = slidepart.Slide.CommonSlideData.ShapeTree;


                // Get the first shape in the shape tree.
                Drawing.Shape shape = tree.GetFirstChild<Drawing.Shape>();
               
                  if (shape != null)
                  {

                    // Get the style of the shape.
                    Drawing.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.
                    slidepart.Slide.Save();
                } 
            }

        }
        
    }

Please let me know if anybody knows how to extract the font color. The above code does not seem to extract the color of the font.

  • 1
    The *"OpenXML Productivity Tool"* (downloadable from the Microsoft site) is your friend. Make a copy of the PPTX you are interested in, change the font color in the copy, save it, and then open both files using the tools *Diff* feature. It will point you to the correct code – Flydog57 Jul 03 '20 at 03:58
  • Hi @Flydog57 Thank you so much!!!. It worked for me. – Priyanka Adiga Jul 06 '20 at 14:40
  • Glad it worked. Keep that tool around. It's indispensable when working with the OpenXML SDK. – Flydog57 Jul 06 '20 at 15:02

0 Answers0