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

Free Support Forum - aspose.com

How to Find & Replace Hyperlinks in pdf

Hello Aspose Team

Can I get help to know How to Find & Replace Hyperlinks in pdf.

I used the below sample code to get the hyperlink.

Document document = new Document("c:/pdftest/TempFile.pdf");
// Traverse through all the page of PDF
foreach (Aspose.Pdf.Page page in document.Pages)
{
    // Get the link annotations from particular page
    AnnotationSelector selector = new AnnotationSelector(new Aspose.Pdf.InteractiveFeatures.Annotations.LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));
page.Accept(selector);
<span class="code-comment" style="color: rgb(128, 128, 128); background-color: inherit;">// Create list holding all the links

IList list = selector.Selected;
// Iterate through invidiaul item inside list
foreach (LinkAnnotation a in list)
{
// Print the destination URL
Console.WriteLine("
Destination: "
+ (a.Action as Aspose.Pdf.InteractiveFeatures.GoToURIAction).URI +
);
}
}


But I got compile time error
Error 1 Using the generic type ‘System.Collections.Generic.IList’ requires 1 type arguments

Kindly assist to find and replace the hyper-link in pdf.

Thanks in advance !

Regards
-David

Hi David,


Thanks for your inquiry. We will appreciate it if you please share your sample PDF document here, we will look into it and will guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,

Hi,


We don’t do it for specific pdf files. We may need to do (find and replace hyperlinks) this for any pdf files.

It will be great if you show some example or sample code for the same.

Regards,
David.

Hi David,


Thanks for sharing the details.

I have tested the scenario using following code snippet (based on code snippet which you have shared in your earlier post) where I have used one of my sample PDF files and as per my observations, the hyperlinks information is properly being extracted.

[C#]

Document document = new Document(“c:/pdftest/HyperLInks_Highlighted.pdf”);<o:p></o:p>

// Traverse through all the page of PDF

foreach (Aspose.Pdf.Page page in document.Pages)

{

// Get the link annotations from particular page

Aspose.Pdf.InteractiveFeatures.Annotations.AnnotationSelector selector = new Aspose.Pdf.InteractiveFeatures.Annotations.AnnotationSelector(new Aspose.Pdf.InteractiveFeatures.Annotations.LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));

page.Accept(selector);

// Create list holding all the links

System.Collections.IList list = selector.Selected;

// Iterate through invidiaul item inside list

foreach (Aspose.Pdf.InteractiveFeatures.Annotations.LinkAnnotation a in list)

{

// Print the destination URL

Console.WriteLine("
Destination: "
+ (a.Action as Aspose.Pdf.InteractiveFeatures.GoToURIAction).URI + "");

}

}

Hi,


Thanks for the response.

Is there any way to replace the hyperlink in the pdf file. If so can help me out with sample code?

Also I tried to find the hyperlinks in pdf using your sample code with the attached pdf files.
Though I am able to get the hyperlinks in the first pdf file (pdfurl-guide.pdf), I am getting some exception in the the second pdf file(sample-link_1.pdf). The Exception error message is "Object reference not set to an instance of an object."

Kindly assist for the same.

Regards,
David.


DavidNaveen:

Also I tried to find the hyperlinks in pdf using your sample code with the attached pdf files.
Though I am able to get the hyperlinks in the first pdf file (pdfurl-guide.pdf), I am getting some exception in the the second pdf file(sample-link_1.pdf). The Exception error message is "Object reference not set to an instance of an object."
Hi David,

Thanks for sharing the resource files. I have tested the scenario and have observed that links information is being retrieved from pdfurl-guide.pdf and a NullReferenceException is being generated when trying to read links information from sample-lnk_1.pdf file. For the sake of correction, I have logged it as PDFNEWNET-40537 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Hi,


Thanks for the response.

Is there any way to replace the External link or hyperlink in the pdf file. If so can help me out with sample code?

Regards,
David.

Hi David,


In order to update/replace links inside PDF document, first we need to read them and in earlier attempts, we encountered issues while retrieving links information from PDF file, Therefore, first we need to fix the issues related to links extraction and then we will be able to replace links in existing PDF files.

Meanwhile if you need to have details regarding how to accomplish this requirement, please visit Update Links in a PDF File