I want to insert hyperlink to a text in a text box.
for e.g.
My text box contains “click here”
Here, I want to insert hyperlink in “here”, so when I click on “here” it redirects to a link.
Please share the approach to achieve this.
Thanks
I want to insert hyperlink to a text in a text box.
for e.g.
My text box contains “click here”
Here, I want to insert hyperlink in “here”, so when I click on “here” it redirects to a link.
Please share the approach to achieve this.
Thanks
@manavjaiswal,
Thank you for contacting support. I am working on the question and will get back to you soon.
@manavjaiswal,
Thank you for your patience.
Please try using the following approach:
The following code example shows how to do this:
final var searchText = "click here";
final var linkText = "here";
final var url = "https://www.google.com/";
// Let's say the text is located in the first paragraph in the first shape on the first slide.
var presentation = new Presentation("sample.pptx");
var slide = presentation.getSlides().get_Item(0);
var textBox = (IAutoShape) slide.getShapes().get_Item(0);
var paragraph = textBox.getTextFrame().getParagraphs().get_Item(0);
var textPortions = new ArrayList<IPortion>();
paragraph.getPortions().forEach(textPortions::add);
for (int i = 0; i < textPortions.size(); i++) {
var textPortion = textPortions.get(i);
var startPosition = textPortion.getText().indexOf(searchText);
if (startPosition > -1) {
var linkPosition = textPortion.getText().indexOf(linkText, startPosition);
var endPosition = linkPosition + linkText.length();
var firstPart = new Portion((Portion) textPortion);
firstPart.setText(textPortion.getText().substring(0, linkPosition));
var linkPart = new Portion((Portion) textPortion);
linkPart.setText(textPortion.getText().substring(linkPosition, endPosition));
linkPart.getPortionFormat().setHyperlinkClick(new Hyperlink(url));
var lastPart = new Portion((Portion) textPortion);
lastPart.setText(textPortion.getText().substring(endPosition));
paragraph.getPortions().remove(textPortion);
paragraph.getPortions().insert(i, firstPart);
paragraph.getPortions().insert(i + 1, linkPart);
paragraph.getPortions().insert(i + 2, lastPart);
break;
}
}
presentation.save("output.pptx", SaveFormat.Pptx);
presentation.dispose();
Manage Hyperlinks|Aspose.Slides Documentation
Please note that this code will not work if the texts “click” and “here” are formatted differently (they will be in different text portions). In this case, you will need to apply a slightly more complex algorithm.