facebooktwittermenuarrow-up

GemBox Support Forum

Changing font for a textrange

In Presentation I can change the text within a TextRange, eg:

// sh is the current Drawing Shape
TextRange b = sh.TextContent.Find("{value}").First();
b.Replace("{value}","New Value");

but, I can’t find a way to set the font attributes (colour, highlight, bold etc) for that specific block of text.

In Spreadsheet there is the getCharacters option, and in VBA I can use ob.TextFrame.TextRange.Characters(t1, t2 - t1).Font.Bold = True

Have I missed something obvious in the API (or has anyone build a work-around they can share?)

Hi Jeremy,

Try this:

TextRange b = sh.TextContent.Find("{value}").First();
b.LoadTextFormat.Font = "Arial";
b.Replace("{value}", "New Value");

I hope it helps.

Regards,
Mario

thanks Mario,
looks like both the Font face and Bold etc don’t get applied (changing text or not)

the code I’ve got is:

foreach (Shape sh in sl.Content.Drawings.OfType<Shape>()) {
    string a= sh.Text.ToString();
    if (a.Contains("{value}")) {
        TextRange tr = sh.TextContent.Find("{value}").First();
        tr.LoadTextFormat.Bold=true;
        tr.LoadTextFormat.Font="Comic Sans MS";
        tr.Replace("{value}",this.Name);
       }
}

I’ve tried not changing the text, and also changing the text before the font and it doesn’t show up in the saved pptx

Jeremy,

Can you send us a small project that reproduces your issue so that we can investigate it?

Regards,
Mario

will do, thanks! probably about an hour

Hi Jeremy,

As we previously discussed, the problem occurs because Replace method is used instead of LoadText.

This method doesn’t use LoadTextFormat, it can’t because it can make multiple replacements and all of them would need to use that same LoadTextFormat which would result in unexpected behavior.

Anyway, here is the fixed snippet code:

foreach (Shape sh in sl.Content.Drawings.OfType<Shape>())
{
    string a= sh.Text.ToString();
    if (a.Contains("{value}"))
    {
        TextRange tr = sh.TextContent.Find("{value}").First();
        tr.LoadTextFormat.Bold=true;
        tr.LoadTextFormat.Font="Comic Sans MS";
        tr.LoadText(this.Name);
    }
}

Regards,
Mario

extending this just in case others are looking for a similar solution…

I have a textbox in the powerpoint that contains four instances of “{value}”, and I want to replace each in order with the words One, Two, Three, and Four, and change the text to bold red, but only for those words.

Using the code above sets the formatting from the first instance of {value} to the end of the text but, by doing two Range selections and LoadTexts on the same text, it looks like it correctly creates the internal runs and allows me to format:

var presentation = PresentationDocument.Load("s1.pptx");
foreach (Slide sl in presentation.Slides) {
  foreach (Shape sh in sl.Content.Drawings.OfType<Shape>()) {
    string s = "{value}";
    int i = 0;
    string[] repl =new []{"One","Two","Three","Four"};
    while (sh.TextContent.ToString().IndexOf(s) != -1 ) {
      TextRange tr = sh.TextContent.Find(s).First();
       tr.LoadText(tr.ToString());
       TextRange tr2 = sh.TextContent.Find(s).First();
       tr2.LoadTextFormat.Bold = true;
       
       tr2.LoadTextFormat.Fill.SetSolid(Color.FromName(ColorName.Red));
       tr2.LoadText(tr2.ToString().Replace(s,repl[i]));
       i++;
    }
  }
}

Hi Jeremy,

Instead of that, try using this latest bugfix version:
https://www.gemboxsoftware.com/presentation/nightlybuilds/GBP25v1129.zip

Or this latest NuGet package:
Install-Package GemBox.Presentation -Version 25.0.1129-hotfix

And use this:

var presentation = PresentationDocument.Load("s1.pptx");
foreach (Slide sl in presentation.Slides)
{
    foreach (Shape sh in sl.Content.Drawings.OfType<Shape>())
    {
        string s = "{value}";
        int i = 0;
        string[] repl = new[] { "One", "Two", "Three", "Four" };
        TextRange tr = null;
        while ((tr = sh.TextContent.Find(s).FirstOrDefault()) != null)
        {
            tr.LoadTextFormat.Bold = true;
            tr.LoadTextFormat.Fill.SetSolid(Color.FromName(ColorName.Red));
            tr.LoadText(repl[i]);
            i++;
        }
    }
}
presentation.Save("output.pptx");

Does this solve your issue?

Regards,
Mario