none
Search and Highlight text in ppt - openxml RRS feed

  • Question

  • I am using openXML to search particular text and highlight it (change its color) in ppt using openxml. Below is the code , how ever it is not highlighting the text in the ppt. Please suggest

    Highlighting Text :

    if (text.Text == "John")
                                    {
    
                                        A.Run run1 = new A.Run();
                                        A.RunProperties runProperties1 = new A.RunProperties() { Language = "en-US", FontSize = 6000, Dirty = false };//Set Font-Size to 60px.
                                        A.SolidFill solidFill1 = new A.SolidFill();
                                        A.RgbColorModelHex rgbColorModelHex1 = new A.RgbColorModelHex() { Val = "00B050" };//Set Font-Color to Green (Hex "00B050").
                                        solidFill1.Append(rgbColorModelHex1);
                                        runProperties1.Append(solidFill1);
                                        A.Text text1 = new A.Text();
                                        text1.Text = text.Text;
                                        run1.Append(runProperties1);
                                        run1.Append(text1);
                                        slidePart.Slide.Save();
    
                                    }

    Complete Code : 

    using Text = DocumentFormat.OpenXml.Presentation.Text; using A = DocumentFormat.OpenXml.Drawing; public static void GetSlideIdAndText(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; SlidePart[] slidePartList = part.SlideParts.ToArray(); //string relId = (slideIds[index] as SlideId).RelationshipId;

    }

    Friday, October 26, 2018 4:30 PM

All replies

  • Hi Aryahi,

    According to your code, I suggest you get all the text in a slide first and then go to determine if it is a specific string.

    // Get all the text in a slide.
        public static string[] GetAllTextInSlide(string presentationFile, int slideIndex)
        {
            // Open the presentation as read-only.
            using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, false))
            {
                // Pass the presentation and the slide index
                // to the next GetAllTextInSlide method, and
                // then return the array of strings it returns. 
                return GetAllTextInSlide(presentationDocument, slideIndex);
            }
        }

    For more information, please refer to the following links:

    Using C# & Powerpoint OpenXML, is it possible to change the font size and color of text

    How to: Get all the text in a slide in a presentation (Open XML SDK)

    Finding Text in a PowerPoint slide with C#

    Highlight.Val Property

    Hopefully it helps you.

    Best Regards,

    Lina


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    • Edited by Lina-MSFT Monday, October 29, 2018 7:37 AM
    Monday, October 29, 2018 6:04 AM
  • Hi Aryahi,

    Thanks for your asking. Please remember to mark the replies as answers if they helped and please help us close the thread.

     

    Thank you for understanding. If you have any question, or update, please feel free to let us know.

     

    I wish you a happy life!

     

    Best Regards,

     

    Lina


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Wednesday, October 31, 2018 9:36 AM
  • Hi,  am here to recommend you an alternative sulotion based on Spire.Presentation

    using Spire.Presentation;
    using System.Drawing;
    
    namespace HighlightTextInPPT
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Presentation presentation = new Presentation();
                presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\test2.pptx");
    
                IAutoShape ishape;
                string wholeRangeText;
                string[] strs;
                TextRange range;
                var targetStr = "Hello";//text to be highlighted
                foreach (ISlide slid in presentation.Slides)
                {
                    foreach (Shape shape in slid.Shapes)
                    {
                        if (shape is IAutoShape)
                        {
                            ishape = shape as IAutoShape;
    
                            for (int p = 0; p < ishape.TextFrame.Paragraphs.Count; p++)
                            {
                                TextParagraph paragraph = ishape.TextFrame.Paragraphs[p];
                                TextParagraph newPara = new TextParagraph();
    
                                if (paragraph.Text.ToLower().Contains(targetStr.ToLower()))
                                {
                                    wholeRangeText = paragraph.Text;
                                    strs = wholeRangeText.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                                    for (int k = 0; k < strs.Count(); k++)
                                    {
                                        range = new TextRange(paragraph.TextRanges[0]);
                                        if (k != strs.Count() - 1)
                                        {
                                            range.Text = strs[k] + " ";
    
                                            if (strs[k].ToLower().Trim() == targetStr.ToLower())
                                            {
                                                range.Text = strs[k];
                                                range.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                                                range.Fill.SolidColor.Color = Color.Red; 
                                                newPara.TextRanges.Append(range);
                                                range = new TextRange(" ");
                                                newPara.TextRanges.Append(range);
                                            }
                                            else if (IsCheck(strs[k]))
                                            {
                                                if (strs[k].Trim().Remove(strs[k].Trim().Length - 1).ToLower() == targetStr.ToLower())
                                                {
                                                    range.Text = strs[k].Trim().Remove(strs[k].Trim().Length - 1);
                                                    range.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                                                    range.Fill.SolidColor.Color = Color.Red; 
                                                    newPara.TextRanges.Append(range);
                                                    range = new TextRange(strs[k].Trim()[strs[k].Trim().Length - 1] + " ");
                                                    newPara.TextRanges.Append(range);
                                                }
                                                else
                                                {
                                                    newPara.TextRanges.Append(range);
                                                }
                                            }
                                            else
                                            {
                                                newPara.TextRanges.Append(range);
                                            }
    
                                        }
                                        else
                                        {
                                            range.Text = strs[k];
                                            if (strs[k].Trim().ToLower() == targetStr.ToLower())
                                            {
                                                range.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                                                range.Fill.SolidColor.Color = Color.Red; 
                                                newPara.TextRanges.Append(range);
                                            }
                                            else if (IsCheck(strs[k]))
                                            {
                                                if (strs[k].Trim().Remove(strs[k].Trim().Length - 1).ToLower() == targetStr.ToLower())
                                                {
                                                    range.Text = strs[k].Trim().Remove(strs[k].Trim().Length - 1);
                                                    range.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                                                    range.Fill.SolidColor.Color = Color.Red; 
                                                    newPara.TextRanges.Append(range);
                                                    range = new TextRange(strs[k].Trim()[strs[k].Trim().Length - 1] + " ");
                                                    newPara.TextRanges.Append(range);
                                                }
                                                else
                                                {
                                                    newPara.TextRanges.Append(range);
                                                }
                                            }
                                            else
                                            {
                                                newPara.TextRanges.Append(range);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    range = new TextRange(paragraph.TextRanges[0]);
                                    newPara.TextRanges.Append(range);
                                }
                                ishape.TextFrame.Paragraphs.Insert(p, newPara);
                                ishape.TextFrame.Paragraphs.RemoveAt(p + 1);
                            }
                        }
                    }
                }
                presentation.SaveToFile("result.pptx", FileFormat.Pptx2013);
                System.Diagnostics.Process.Start("result.pptx");
            }
            public static bool IsCheck(string text)
            {
                String[] filterStr = new String[] { ",", "." };
    
                bool find = false;
    
                foreach (string c in filterStr)
                {
                    if (text.Contains(c))
                    {
                        find = true;
                        break;
                    }
                }
                return find;
            }
        }
    }

    Output



    • Edited by Scott4DotNet Monday, November 19, 2018 9:13 AM
    • Proposed as answer by Scott4DotNet Tuesday, November 20, 2018 8:38 AM
    • Unproposed as answer by Scott4DotNet Tuesday, November 20, 2018 8:39 AM
    Monday, November 19, 2018 9:13 AM