How to get/set TextToDisplay of hyperlink in PDF document?

Hi Aspose team,

I am currently trying to find a way to identify all hyperlinks in my document.

It works for hyperlinks (URI) but I need to know also what is text that document reader will see.

For example, I have following text in my document:

Go to Google and when you click on this text you will be redirected to www.google.com

I can find www.google.com but I want be to be able to identify also that text visible “Go to Google” and to make change of it.

What would you recommend for this scenario?

Thanks,
Oliver

Hi Oliver,

Thanks for your inquiry. Please check the last section of update hyperlinks in PDF documentation.
It describes how to change hyperlink text color and how you may also use the same code to set the Text property of TextFragment to change the text. Please check the following sample code snippet. Hopefully, it will help you accomplish the task.

Document doc = new Document("input.pdf");
// get the first link annotation from the first page of the document
LinkAnnotation linkAnnot = (LinkAnnotation)doc.Pages[1].Annotations[1];
if (linkAnnot.Action is GoToURIAction)
{
    // Modification link: change link URI
    GoToURIAction goToAction = (GoToURIAction)linkAnnot.Action;
    // specify the URI for the link object
    goToAction.URI = "www.google.com";
    // save the document with the updated link
    // search the text under the annotation
    TextFragmentAbsorber ta = new TextFragmentAbsorber();
    Aspose.Pdf.Rectangle rect = linkAnnot.Rect;
    rect.LLX -= 10;
    rect.LLY -= 10;
    rect.URX += 10;
    rect.URY += 10;
    ta.TextSearchOptions = new TextSearchOptions(rect);
    ta.Visit(doc.Pages[1]);
    // Change color and text.
    foreach (TextFragment tf in ta.TextFragments)
    {
        tf.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
        tf.Text = "ClickHere";
    }
}
doc.Save("output.pdf");

Please feel free to contact us for any further assistance.

Best Regards,

Hi,

I used provided code and following problems will appear (test project attached):

- I can make change of URL => OK
- I can find text fragment which is visible in front of URL => OK
- I can replace found text fragment.Text => OK
- My original TextFragment.Text="vacant", new TextFragment.Text="ThisIsVeryLong CLICK HERE TEXT", replacement = >OK

- Now problems:
1. URL is active only over part of new TextFragment.Text. It will be active only for the size of word vacant which was link in original document

2. There is text overlapping as Aspose.PDF did not adjust text even when I used:

ta.TextReplaceOptions.ReplaceAdjustmentAction = Aspose.Pdf.Text.TextOptions.TextReplaceOptions.ReplaceAdjustment.AdjustSpaceWidth;

ta.TextReplaceOptions.ReplaceAdjustmentAction = Aspose.Pdf.Text.TextOptions.TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation;

Image1 => original document with link
Image2 => changed URL plus changes TextToDisplay with very long text
Image3 => to show that only part of new TextToDisplay is with URL (to be precise only for length of word vacant which was original link)

Sample project attached but coding is that what you provided.

Any option how to:
- adjust text to show changes without overlap?
- adjust that URL is for complete long TextToDisplay

Thanks,
Oliver

I fixed the problem:
- adjust that URL is for complete long TextToDisplay

Simply I set

objAnnotation.Rect = tf.Rectangle;

where tf.Rectangle is new size of extended text.

Still open problem is overlapping :( but I saw that there are also other users having same issue so I hope that you will resolve it quickly as this really impacts changes of texts in PDF by corrupting display/text.

Thanks,
Oliver

Hi Oliver,


Thanks for sharing the details and sorry for the delayed response.

We are glad to hear that you have managed to resolve Adjust URL for complete long TextToDisplay. However we are working on testing text overlap issue and will keep you posted with our findings.

PS, there have been some scenarios where some other customers have also reported text overlap issue but for individual customer documents, we explicitly test the scenario as it depends upon the structure of input file.

Hi Oliver,


Thanks for your patience. We have tested the scenario and noticed the text overlapping issue with long text and logged a ticket PDFNEWNET-40024 in our issue tracking system for further investigation and rectification. We will update you as soon as it is resolved.

We are sorry for the inconvenience caused.

Best Regards,
Hi Aspose team

new news here? It is now 5 months since reporting the bug.

Hi Oliver,


Thanks for your patience.

I am afraid the earlier reported issues is still pending for review as the team has been busy fixing other previously reported priority issues. However the product team will surely consider investigating/fixing it as per development schedule and as soon as we have some definite updates regarding its resolution, we will let you know. Please be patient and spare us little time. We are sorry for this delay and inconvenience.
Hi Aspose team,

any news here? Now is more than 9 months :(.

Thx,
Oliver

Hi Oliver,


Thanks for your patience and sorry for the delayed response.

I am afraid the earlier reported issue is not yet resolved as the team has been recently busy fixing some other previously reported high priority issues. However I have intimated the product team to share the possible time-frame regarding its resolution. As soon as we have some further updates, we will let you know.

The issues you have found earlier (filed as PDFNET-40024) have been fixed in Aspose.PDF for .NET 26.2.