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

Free Support Forum - aspose.com

Looking for a simple replace of string anywhere on a slide

Not sure why i am stumbling on this but can’t seem to find an example of how to replace a string found anywhere on a slide. I scoured the forums an altered the latest text replace example code but was still unable to replace the text with desired string. Please advise as to the most efficient method to accomplish this. Thank you in advance for your guidance.

//Iterate through shapes to find the placeholder
foreach (IShape shp in sld.Shapes)
if (shp.Placeholder != null)
{
if (((IAutoShape)shp).TextFrame != null)
for (int paracount = 0; paracount < ((IAutoShape)shp).TextFrame.Paragraphs.Count; paracount++)
{
IParagraph para = ((IAutoShape)shp).TextFrame.Paragraphs[paracount];
foreach (Portion port in para.Portions)
{
port.Text.Replace(“Aspose”, “NameChanged”);
}
}
}

Hi Jeff,


Thanks for your interest in Aspose.Slides.

I have observed the comments shared by you and like to share that the sample code seems fine. I request you to please share the working sample project along with source presentation, generated presentation and snapshot of what has been missed from replacement in saved presentation. I will observe the issue further on my end to help you out.

Many Thanks,

Hi Mudassir,
I have pasted the code below and attached the sample source and output file. When i debug, i see that the text is found within entry but the replace function doesn’t seem to be working. If this is not the correct way to replace text, please let me know the most efficient way to accomplish this replacement of a string anywhere within a slide.

Thanks,
Jeff

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AS = Aspose.Slides;
using ASE = Aspose.Slides.Export;

namespace AsposeDevTest
{
internal class utils
{
internal static void Aply_ASPOSE_Slides_License()
{
//Instantiate the License class
Aspose.Slides.License license = new Aspose.Slides.License();
//Pass only the name of the license file embedded in the assembly
license.SetLicense(@“C:\Program Files (x86)\Aspose\Aspose.Slides for .NET\License\Aspose.Slides.lic”);
}
}
public class pmods
{
internal static int ReplaceText(AS.Presentation myPrezo, string LookupString, string NewString)
{
int resultCode = 0;
if (string.IsNullOrEmpty(LookupString))
resultCode = -1;
else
{
AS.ISlide sld = myPrezo.Slides[0];
foreach (AS.IShape shp in sld.Shapes)
if (shp.Placeholder != null)
{
if (((AS.IAutoShape)shp).TextFrame != null)
{
for (int paracount = 0; paracount < ((AS.IAutoShape)shp).TextFrame.Paragraphs.Count; paracount++)
{
AS.IParagraph para = ((AS.IAutoShape)shp).TextFrame.Paragraphs[paracount];
foreach (AS.Portion port in para.Portions)
{
string entry = port.Text.ToString();
if (entry.IndexOf(LookupString)>0)
Console.Write(“Specified Text Exists within entry variable”);
port.Text.Replace(LookupString, NewString);
}
}
}
}
}
return 0;
}
internal static int ReplaceText(string strMyPPTX, string strOutput,string LookupString, string NewString)
{
using (AS.Presentation myPrezo = new AS.Presentation(strMyPPTX))
{
int myResult = ReplaceText(myPrezo, LookupString, NewString);
if (myResult == 0)
myPrezo.Save(strOutput, ASE.SaveFormat.Pptx);
else
return 1;
}
return 0;
}
}
class Program
{
static void Main(string[] args)
{
string strSourceFile=@“C:\temp\AsposeDevTest\ReplacingText.pptx”;
string strOutputFile=@“C:\temp\AsposeDevTest\Output.pptx”;
utils.Aply_ASPOSE_Slides_License();
pmods.ReplaceText(strSourceFile,strOutputFile,“Aspose”,“Replacement”);
}
}
}


I did find that i wasn’t returning the presentation back from the replace function but i still don’t see that TEXT.REPLACE function working, below is the updated code and i believes it shows that the text isn’t changing, might i be using the function incorrectly?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AS = Aspose.Slides;
using ASE = Aspose.Slides.Export;

namespace AsposeDevTest
{
internal class utils
{
internal static void Aply_ASPOSE_Slides_License()
{
Aspose.Slides.License license = new Aspose.Slides.License();
license.SetLicense(@“C:\Program Files (x86)\Aspose\Aspose.Slides for .NET\License\Aspose.Slides.lic”);
}
}
public class pmods
{
internal static AS.Presentation ReplaceText(AS.Presentation myPrezo, string LookupString, string NewString,ref int resultCount)
{
if (string.IsNullOrEmpty(LookupString))
resultCount = -1;
else
{
AS.ISlide sld = myPrezo.Slides[0];
foreach (AS.IShape shp in sld.Shapes)
if (shp.Placeholder != null)
{
if (((AS.IAutoShape)shp).TextFrame != null)
{
for (int paracount = 0; paracount < ((AS.IAutoShape)shp).TextFrame.Paragraphs.Count; paracount++)
{
AS.IParagraph para = ((AS.IAutoShape)shp).TextFrame.Paragraphs[paracount];
foreach (AS.Portion port in para.Portions)
{
string entry = port.Text.ToString();
if (entry.IndexOf(LookupString) > 0)
{
Console.Write(“Specified Text Exists within entry variable”);
port.Text.Replace(LookupString, NewString);
string resultingchange = port.Text.ToString();
Console.Write(resultingchange);
}

}
}
}
}
}
return myPrezo;
}
internal static void ReplaceText(string strMyPPTX, string strOutput,string LookupString, string NewString)
{
int resultCount = 0;
using (AS.Presentation myPrezo = new AS.Presentation(strMyPPTX))
{
AS.Presentation updatedPrezo = ReplaceText(myPrezo, LookupString, NewString,ref resultCount);
updatedPrezo.Save(strOutput, ASE.SaveFormat.Pptx);
}
}
}
class Program
{
static void Main(string[] args)
{
string strSourceFile=@“C:\temp\AsposeDevTest\ReplacingText.pptx”;
string strOutputFile=@“C:\temp\AsposeDevTest\Output.pptx”;
utils.Aply_ASPOSE_Slides_License();
pmods.ReplaceText(strSourceFile,strOutputFile,“Aspose”,“Replacement”);
}
}
}


Hi Jeff,


Thanks for sharing the sample code. As requested earlier, please provide the source presentation, generated presentation and snapshot of the highlighted area of the slide where the replacement has failed. Please share the requested information so that I may proceed further with my investigation.

Many Thanks,

The attachments haven’t changed and have been attached.
Within the source file (replacingtext.pptx) Aspose is not highlighted or formatted in anyway however I felt it best to do so for the screenshot. this does not change the behavior. I am leveraging Slides 15.5.0

Hi Jeff,

I have worked with the sample presentation and code sample code shared by you. The issue lies in your code where you are replacing the text. You need to please make following modification in your sample code to serve the purpose. Please share, if I may help you further in this regard.

internal static AS.Presentation ReplaceText(AS.Presentation myPrezo, string LookupString, string NewString, ref int resultCount)
{
if (string.IsNullOrEmpty(LookupString))
resultCount = -1;
else
{
// AS.ISlide sld = myPrezo.Slides[0];
foreach(AS.ISlide sld in myPrezo.Slides)
foreach (AS.IShape shp in sld.Shapes)
if (shp.Placeholder != null)
{
if (((AS.IAutoShape)shp).TextFrame != null)
{
for (int paracount = 0; paracount < ((AS.IAutoShape)shp).TextFrame.Paragraphs.Count; paracount++)
{
AS.IParagraph para = ((AS.IAutoShape)shp).TextFrame.Paragraphs[paracount];
foreach (AS.Portion port in para.Portions)
{
string entry = port.Text.ToString();
if (entry.IndexOf(LookupString) > 0)
{
Console.Write(“Specified Text Exists within entry variable”);
port.Text= port.Text.Replace(LookupString, NewString);
string resultingchange = port.Text.ToString();
Console.Write(resultingchange);
}
}
}
}
}
// }//end slide loop
}
return myPrezo;
}


Many Thanks,

Thanks, that worked perfectly, i misunderstood the replace function makes perfect since now that i see the change!
OLD:
port.Text.Replace(LookupString, NewString);
NEW:
port.Text= port.Text.Replace(LookupString, NewString);

Thanks,
Jeff

Hi Jeff.

You are always welcome. Its good to know that things are now working as per aspirations on your end. Please share, if I may help you further in this regard.

Many Thanks,