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

Free Support Forum - aspose.com

How to Remove Hyperlinks from Text in a PDF document?

Hi
Is there a way to iterate text segment and disable hyperlink in PDF document?

Thanks

Hi Oren,


Thanks for your inquiry. You can iterate through the LinkAnnotaions and update the different properties as per your need. You can set an empty URI to disable the LinkAnnotaiton.

Document doc = new Document(“input.pdf”);<o:p></o:p>

// get the first link annotation from first page of document<o:p></o:p>

LinkAnnotation linkAnnot = (LinkAnnotation)doc.Pages[1].Annotations[1];<o:p></o:p>

if (linkAnnot.Action is GoToURIAction)<o:p></o:p>

{<o:p></o:p>

// Modification link: change link URI<o:p></o:p>

GoToURIAction goToAction = (GoToURIAction)linkAnnot.Action;<o:p></o:p>

// specify the URI for link object<o:p></o:p>

goToAction.URI = “”;<o:p></o:p>

// save the document with updated link<o:p></o:p>

// Search the text under the annotation<o:p></o:p>

TextFragmentAbsorber ta = new TextFragmentAbsorber();<o:p></o:p>

Aspose.Pdf.Rectangle rect = linkAnnot.Rect;<o:p></o:p>

rect.LLX -= 10;<o:p></o:p>

rect.LLY -= 10;<o:p></o:p>

rect.URX += 10;<o:p></o:p>

rect.URY += 10;<o:p></o:p>

ta.TextSearchOptions = new TextSearchOptions(rect);<o:p></o:p>

ta.Visit(doc.Pages[1]);<o:p></o:p>

// Change color and text.<o:p></o:p>

foreach (TextFragment tf in ta.TextFragments)<o:p></o:p>

{<o:p></o:p>

tf.TextState.ForegroundColor = Aspose.Pdf.Color.Red;<o:p></o:p>

tf.Text = “Click Here”;<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

doc.Save(“output.pdf”);


Please feel free to contact us for any further assistance.


Best Regards,

Hi
Thank you very much for the example.
How can I disable links that are generated from a text such as:
http://edition.cnn.com . If the text has a pattern of link (e.g. http, www…) the PDF Acrobat link the text.

Thanks

Hi Oren,


Thanks for your feedback. I noticed that Adobe automatically link the text with specified (http,www) pattern. After initial investigation we have logged a ticket PDFNEWNET-38688 in our issue tracking system for further investigation and resolution. We will notify you as soon as we made some progress towards issue resolution progress.

We are sorry for the inconvenience caused.

Best Regards,

any update on this issue?

Hi Camille,


Thanks for your inquiry. I am afraid issue is still not resolved. As we have recently noticed the issue and it is still pending for investigation due to other issues, already under investigation and resolution. We will notify you as soon as we made some significant progress towards issue resolution.

We are sorry for the inconvenience caused.

Best Regards,

Hello,

is there any news on this issue?

Thanks,

Hi Camille,


Thanks for your inquiry. Please note we cannot delete hyperlinks from such text, because there are no any links. Viewer (Adobe or Foxit) converts a text that matches to a link and e-mail format and makes it active automatically. However as a workaround we can override the default link by our LinkAnnotation object configured with empty GoToAction. Please try following sample code snippet, hopefully it will help you to accomplish the task.

// load the PDF file<o:p></o:p>

Document doc = new Document(myDir+"Hyperlink.pdf");

//create TextAbsorber object to find all the phrases matching the regular expression for hyperlink of e-mail

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("(\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])|((([a-zA-Z]|[0-9])|([-]|[_]|[.]))+[@](([a-zA-Z0-9])|([-])){2,63}[.](([a-zA-Z0-9]){2,63})+$)");

//set text search option to specify regular expression usage

TextSearchOptions textSearchOptions = new TextSearchOptions(true);

textFragmentAbsorber.TextSearchOptions=textSearchOptions;

int pageNum = 1;

//accept the absorber for all the pages

Aspose.Pdf.Page page = doc.Pages[pageNum];

page.Accept(textFragmentAbsorber);

//get the extracted text fragments

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

//loop through the fragments

foreach (TextFragment textFragment in textFragmentCollection)

{

LinkAnnotation linkAnnot = new LinkAnnotation(page, textFragment.Rectangle);

linkAnnot.Border=new Border(linkAnnot);

linkAnnot.Border.Width=0;

page.Annotations.Add(linkAnnot);

}

doc.Save(myDir+"HyperlinkRemoved.pdf");


Best Regards,