Change text in Powerpoint keeping style in c#

Home Forums Open-Xml-Sdk Change text in Powerpoint keeping style in c#

This topic contains 1 reply, has 2 voices, and was last updated by  Eric White 8 years, 2 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #3550

    cpieret
    Participant

    Hi Guys,

    I just started playing with this SDK today.

    I want in my job to generate automatically powerpoint presentation for business intelligence reports.

    I struggled to change some text in a powerpoint presentation keeping the style in C#.

    I just want to share it here as I found so many topics on stack overflow etc… without any answers :

    This code might be optimized as I know in advance the ID of the slide ( I put as a parameter in my method) and use ChildElement[0] hard coded ( a for loop and checking the type = DocumentFormat.OpenXml.Drawing.RunProperties night do the trick )

    
     private void replaceTextInSlide (PresentationPart presPart, int slideID, string oldText, string newText)
            {
                Presentation presentation = presPart.Presentation;            
                if (presentation.SlideIdList != null)
                {
                    // Get the collection of slide IDs from the slide ID list.
                    var slideIds = presentation.SlideIdList.ChildElements;
                    string slidePartRelationshipId = (slideIds[slideID] as SlideId).RelationshipId;
                    SlidePart slidePart = (SlidePart)presPart.GetPartById(slidePartRelationshipId);
                    foreach (var paragraph in slidePart.Slide.Descendants<DocumentFormat.OpenXml.Drawing.Paragraph>())
                    {
                        if (paragraph.InnerText.Contains(oldText))
                        {
                            string newString = paragraph.InnerText;
                            newString = newString.Replace(oldText, newText);
                            foreach (var child in paragraph.ChildElements)
                            {
                                if (child.InnerText.Equals(oldText))
                                {
                                    DocumentFormat.OpenXml.Drawing.RunProperties styleRun = (DocumentFormat.OpenXml.Drawing.RunProperties)child.ChildElements[0].Clone();
                                    child.RemoveAllChildren();
                                    child.Append(styleRun);
                                    child.Append(new DocumentFormat.OpenXml.Drawing.Text(newString));
                                }
                            }
                        }
                    }
                }
                
            }
    
    • This topic was modified 8 years, 4 months ago by  cpieret. Reason: remove one useless line of code
    #3694

    Eric White
    Keymaster

    Hi,

    Sorry for the slow response – was on vacation, traveling, other issues…

    Please check out this blog post and video:

    Generate Open XML Presentations using a Presentation Template

    It presents my recommended approach for generating presentations from a template.

    Cheers, Eric

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.