We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace text through presentation

I’m having difficulty finding and replacing text in all slides of a presentation. I’ve found some different code on the forums but none seem to be working for me yet. My issue is that I have some special characters set up and they’re still being split in to different portions even though the formatting is the same. Here’s one of my examples:

Text to find: [Rheum_Simple_Treatment_AntiInflammatoryDrugs_History]
Portion 1: [
Portion 2: Rheum_Simple_Treatment_AntiInflammatoryDrugs_History]

Below is my code (SlideMergeField is just a key/value pair):

    private void ReplacePresentationText(Presentation p, List<SlideMergeField> fields)
    {
        p.JoinPortionsWithSameFormatting();

        ITextFrame[] tb = SlideUtil.GetAllTextFrames(p, true);
        for (int i = 0; i < tb.Length; i++)
        {
            TextFrame tf = tb[i] as TextFrame;
            for (int j = 0; j < tf.Paragraphs.Count; j++)
            {
                Paragraph para = tf.Paragraphs[j] as Paragraph;
                foreach (Portion port in para.Portions)
                {
                    foreach (SlideMergeField f in fields)
                    {
                        if (port.Text.Contains(f.Key))
                        {
                            if (f.Value.Contains("\r"))
                            {
                                bool isFirst = true;
                                foreach (string line in f.Value.Split(new string[1] { "\r" }, StringSplitOptions.None))
                                {
                                    if (isFirst)
                                    {
                                        isFirst = false;
                                        port.Text = port.Text.Replace(f.Key, line);
                                    }
                                    else
                                    {
                                        j++;
                                        tb[i].Paragraphs.Insert(j, new Paragraph(para) { Text = line });
                                    }
                                }
                            }
                            else
                            {
                                port.Text = port.Text.Replace(f.Key, f.Value);
                            }
                        }
                    }
                }
            }
        }
    }

@miles-1,

I have observed your comments. Can you please share source presentation so that we may further investigate to help you out.

@miles-1,

I also suggest you to please use Aspose.Slides with license on your end as without license Aspose.Slides puts limitation on accessing text.

A single slide is attached (this is not the full presentation due to privacy concerns). If we can make this thread private, I can share it there but that’s not an option when I reply.

The merge fields causing an issue are:

  • [Rheum_Simple_Treatment_AntiInflammatoryDrugs_History]
  • [Rheum_Simple_Treatment_AntiInflammatoryDrugs_Current]

Single slide test.zip (47.9 KB)

@miles-1,

You can send the complete file reproducing the issue privately to me by sending a new private message on this page.

I get an access denied message when attempting to view that link

@miles-1,

I have shared following private message with you. Can you please provide requested information there. Also, if the size of presentation is greater than 3 MB then you can upload that on some File server (dropbox or google drive) and provide download link to us.

https://forum.aspose.com/t/replace-text-through-presentation/181854