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

Free Support Forum - aspose.com

Find and Replace in slides using Java

I’m trying to find and replace words in a MS Powerpoint (pptx) file. To do that I have done the following. But the file doesn’t change


ITextFrame[] tb = SlideUtil.getAllTextFrames(presentation, true);
String strToFind = “{{” + documentTagDTO.getName() + “}}”;

for (int i = 0; i < tb.length; i++)
for (IParagraph ipParagraph : tb[i].getParagraphs())
for (IPortion iPortion : ipParagraph.getPortions())
if (iPortion.getText().contains(strToFind)) {
iPortion.getText().replace(strToFind, strToReplace);
}

Whats wrong there?

Hi Ravindu,


Thank you for your interest in Aspose.Slides.

I have observed your comments and worked with the code shared by you. I have made necessary changes to the code and it is working fine now. Please try using it on your side and then share your kind feedback with us.

Presentation presentation = new Presentation(“test.pptx”);
String strToReplace = “Replaced”;
ITextFrame[] tb = SlideUtil.getAllTextFrames(presentation, true);
String strToFind = “test”;
for (int i = 0; i < tb.length; i++)
for (IParagraph ipParagraph : tb[i].getParagraphs())
for (IPortion iPortion : ipParagraph.getPortions())
if (iPortion.getText().contains(strToFind))
{
iPortion.setText(iPortion.getText().replace(strToFind,strToReplace));
}
presentation.save(“test.pptx”,SaveFormat.Pptx);

Please let us know if the issue persists. We will be happy to assist you further.

Best Regards,

Thanks Muhammada,


It only works partially. I have attached the file it edited. In the 2nd slide, only the word next to Industry got changed as Private.
When I debugged the code, iPortion.getText() dont get the fully word, It takes partial words, like “{{SALES”. Because of that I cant use contains() properly. What is the reason for that? (I hope you can understand the problem I’m facing)

Thanks in advance.


Hi Ravindu,


The code is working perfectly and the problem is with the file. The portions are read as different portions. If you print each portion individually, then you will observe the output as in the attached image portions.JPG, so I request you to please add the following line of code on your end to get the portions as per your expectations (see attached image expected.JPG).

presentation.joinPortionsWithSameFormatting();

I hope this will be helpful. Please share if I may help you further in this regard.

Best Regards,

Hi Adnan,

What do you mean by problem in the file? Why does it read the portions as different portions?
FYI - Your solution worked. Thanks for that.

There is another problem, The replaced text are all capital. Why is that? Can’t I just replace the word as it is?

And also, the edited files cant be shown in Google Slide too. I tried to upload and view it in Google Slides. But it gives a error msg like this "Whoops! There was a problem previewing this document"

Thank You

Hi Ravindu,


Thank you for sharing your kind feedback.

We are glad to know that your issue is resolved. About the file, I meant that there is no role of Aspose.Slides for portions being read as different portion. It is the internal implementation of PowerPoint which changes the portions in a paragraph based on the formatting of the portion. For example, if a word was changed to bold and then to normal. PowerPoint treats it as separate portion for which Aspose.Slides provides the method joinPortionsWithSameFormatting(). This is why you were seeing the difference. I hope this will clarify the concept.

For the issue with Google Slides, please make a separate thread and share with us the source file and the steps to reproduce the issue so that we may proceed further to help you out.

Best Regards,

Thanks Ahmad,


I realized the errors I did. joinPortionsWithSameFormatting() worked. As for the other problem I had with Google Slides also due to the same error and it’s now fixed. Thanks alot

Hi Ravindu,


Thank you for your kind feedback.

We are glad to know that your issue is resolved and things have started working on your end.

Please feel free to contact us if we could be of any help to you.

Best Regards,