How to sets the PPT background use openxml

qiangang 40 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.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,309 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,532 questions
Office Open Specifications
Office Open Specifications
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Open Specifications: Technical documents for protocols, computer languages, standards support, and data portability. The goal with Open Specifications is to help developers open new opportunities to interoperate with Windows, SQL, Office, and SharePoint.
120 questions
Office Management
Office Management
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Management: The act or process of organizing, handling, directing or controlling something.
2,012 questions
{count} votes

Accepted answer
  1. Mike Bowen 1,276 Reputation points Microsoft Employee
    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