Could not Find and Replace text In PPT (Java)

@Kushal.20,

Please check following sample code that i have shared with you. If there is still an issue than please share feedback with us.

Presentation presentation = new Presentation(“samplepptx.pptx”);
presentation.joinPortionsWithSameFormatting();
String strToReplace = “Done”;
ITextFrame[] tb = SlideUtil.getAllTextFrames(presentation, true);
String find = “sample”;
String strToFind= “(?i)\b”+find+"\b";
// System.out.println(“Before for :”+strToFind);
for (int i = 0; i < tb.length; i++)
{
for (IParagraph ipParagraph : tb[i].getParagraphs())
{
for (IPortion iPortion : ipParagraph.getPortions())
{
if(iPortion.getText().toLowerCase().contains(find.toLowerCase()))
{
iPortion.setText(iPortion.getText().replaceAll(strToFind, strToReplace));
System.out.println(“replaced”);
}
}
}
}
presentation.save(“Output.pptx”,SaveFormat.Pptx);

@Adnan.Ahmad
Thanks!
But, It’s not working still. Output is same as before.
Only ‘sample’ is getting replaced and not ‘Sample’.
Please find the attached output file. Output.zip (361.7 KB)

Also, here is the input file : samplepptx.zip (395.0 KB)

@Kushal.20,

Can you please share if you are using full license or using trial version.

I am using 30 days temporary license, @Adnan.Ahmad , and I guess, it allows 30 days complete usage with no limitations, right ?

@Kushal.20,

The 30 days license is a full working license too. My request to you is to use the sample code that I previously shared with you as it is.

You are using:

String strToFind= “(?i)\b”+find+“\b”;

Please use:

String find = "sample";

@mudassir.fayyaz
Yeah, that’s why I preferred to use 30 days licensed version instead.
I am using the above mentioned code too for testing purpose. Buddy, that’s working fine, but my same issue is still not resolved. It is unable to handle the CASES. When I give ‘sample’, it only replaces ‘sample’ not ‘Sample’.
That’s why I tried using

as you suggested, but it’s not working yet.
I hope you understand my issue, replacing has been resolved, but this case problem persists, that limits replacing the text with case sensitivity.
Please help me get through this challenge.
Thanks !

@Kushal.20,

I have observed your comments. I have shared sample code with you. In my case this sample code is working fine and Replacing case sensitive words as well. I have shared my generated result with you for your kind reference. Please check and share feedback with us if there is still an issue. Also please use latest version if you are using any old version.Output72.zip (361.6 KB)

Presentation presentation = new Presentation(path+ “samplepptx1.pptx”);
presentation.joinPortionsWithSameFormatting();
String strToReplace = “Done”;
ITextFrame[] tb = SlideUtil.getAllTextFrames(presentation, true);
String find = “sample”;
String strToFind= “(?i)\b”+find+"\b";
// System.out.println(“Before for :”+strToFind);
for (int i = 0; i < tb.length; i++)
{
for (IParagraph ipParagraph : tb[i].getParagraphs())
{
for (IPortion iPortion : ipParagraph.getPortions())
{
if(iPortion.getText().toLowerCase().contains(find.toLowerCase()))
{
iPortion.setText(iPortion.getText().replaceAll(strToFind, strToReplace));
System.out.println(“replaced”);
}
}
}
}
presentation.save(path+ “Output72.pptx”,SaveFormat.Pptx);

Hello, @Adnan.Ahmad !
I used your code as it is, but still no progress, stuck at the same thing, same result.

I am using these .jar files for Slides :

  • aspose-slides-19.5-javadoc.jar

  • aspose-slides-19.5-jdk16.jar

Though, I would like to tell you one thing, that in the console, am getting ‘replaced’ 5 times (although, it is also not correct, it should be 6, as there are total 6 occurrences of ‘sample’ in the document, still if it could help at first), but in the output document, it’s behavior is case sensitive. Am attaching the screenshot of the console here : outputSlides.PNG (39.6 KB)
Also , find the output file here, which is same as before :
Output.zip (362.0 KB)

Now, I have tried almost everything. Applied license, using latest versions, etc. Am stuck !
How to proceed now ?

@Kushal.20,

I have observed the images shared by you and like to share that the issue is not related to Aspose.Slides. As long as portion text is getting loaded well in String class object (which is not Aspose propriety), it is not termed as Aspose.Slides issue. The basis problem is when comparison of sub-string is performed to find and replace a sub-string inside a big String (which is portion of text).

In sample code, you are performing comparison based on contains() method offered by String class. This method find first instance of sub-string in String and returns true or false. In your case, on slide 1, the following text has two instances of sample in single string. By design, contains() method will return true whenever it will find first instance of string to find.

Hello, I am a sample. I work as sample. I am useless !

There are total 6 instances of sample in all slides but comparison done will be 5 as two of instance of samples happen to be available in text of slide 1 (as mentioned above). I suggest you to please try using following sample as it is on your end and share results with us. Please only change the path of presentation file and nothing more.

public static void ReplacePresentationText()
{
    String path="C:\\Aspose Data\\samplepptx\\";
    Presentation presentation = new Presentation(path+"samplepptx.pptx");
    presentation.joinPortionsWithSameFormatting();
    String strToReplace = "Done";
    ITextFrame[] tb = SlideUtil.getAllTextFrames(presentation, true);
    String strToFind = "Sample";
    System.out.println("Before for");
    for (int i = 0; i < tb.length; i++)
    {
        for (IParagraph ipParagraph : tb[i].getParagraphs())
        {
            for (IPortion iPortion : ipParagraph.getPortions())
            {
                if (iPortion.getText().toLowerCase().contains(strToFind.toLowerCase()))
                {
//                        iPortion.setText(iPortion.getText().replaceAll("(?i)"+strToFind, strToReplace));
                    iPortion.setText(iPortion.getText().toLowerCase().replaceAll(strToFind.toLowerCase(), strToReplace));
                    System.out.println("replaced");
                }
            }
        }
    }
    presentation.save(path+"Output.pptx",SaveFormat.Pptx);
}

I have attached the generated presentation as well as comparison images for your reference.

Output.zip (361.5 KB)
Replace Text.zip (1.9 MB)

@mudassir.fayyaz
Thanks !
Finally, I have got this working and most importantly, I got to know the issue. Thanks buddy ! :slight_smile:

@Kushal.20,

It’s good to know things are finally fine on your end. :slight_smile: