How to sets the PPT background use openxml

qiangang 45 Reputation points
2023-02-14T08:14:32.7+00:00

I'm using openxml to manipulate ppt, but I don't know how to modify the ppt background, hope to get help, thank you.

Developer technologies | Windows Presentation Foundation
Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Open Specifications
Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Developer technologies | C#
{count} votes

Accepted answer
  1. Mike Bowen 2,051 Reputation points Microsoft Employee Moderator
    2023-02-14T20:08:39.4933333+00:00

    Hi Hi,@乾罡 雷,

    There are different kinds of backgrounds, Solid Fill, Gradient Fill, Picture Fill, etc., so I'm not sure exactly which background effect you want to apply, but if you want to, for example, change the background color of all the slides in a PPTX file, you would need to:

    • Get a list of all slides for the presentation
    • Find the Common Slide Data p:cSld for each slide.
    • Add a [Slide Background] p:bg as the first child element to the Common Slide Data element
    • Add a Background Properties element as the first child of the Slide Background element.
    • Add a Solid Fill element a:solidFill (ISO-OI29500 20.1.8.54 solidFill (Solid Fill)) element as a child of the Background Properties element
    • Add an a:srgbClr element as a child of the Solid Fill element with a val property of the color you want as a hex.

    If you want to do that with the SDK, it would look like this:

    string file = @"C:\path\to\your.pptx";
    
    using (PresentationDocument presentationDocument = PresentationDocument.Open(file, true))
    {
        IEnumerable<SlidePart>? slideParts = presentationDocument?.PresentationPart?.SlideParts;
    
        if (slideParts is not null )
        {
            foreach(SlidePart slidePart in slideParts)
            {
                IEnumerable<CommonSlideData> commonSlideDatas = slidePart.Slide.Elements<CommonSlideData>();
                foreach (var commonSlideData in commonSlideDatas)
                {
                    commonSlideData.PrependChild(new Background(
                        new BackgroundProperties(
                            new SolidFill(
                                new RgbColorModelHex() { Val = "00B050" }))));
                }
            }
        }
    
        if (presentationDocument is not null)
        {
            presentationDocument.Save();
        }
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.