Get All links from a PDF file

Hi,


I’ve noticed that some pdfs have different types of links (GoToRemoteAction, LaunchAction). Is there an easy way to get all hyperlinks regardless of what action type they are?

At the moment i’m declaring the link as :

var link = (annotation.Action as GoToRemoteAction);

var link2 = (annotation.Action as LaunchAction);

and simply doing a null check…

Is there a better way ?

Thanks.

Hi There,

Thanks for contacting support.

The links in the PDF document are represented as Annotation. You may use AnnotationSelector class to get all LinkAnnotations in a PDF document. Please check the following code snippet to add links with different actions in the document and access them after.

// Create new document
Document pdfDocument = new Document();
pdfDocument.Pages.Add();
pdfDocument.Save("AddExtractLink.pdf");

// Load created document
pdfDocument = new Document("AddExtractLink.pdf");
Page page = pdfDocument.Pages[1];

// Add links
LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(10, 800, 30, 820));
link.Action = new LaunchAction("test.txt");
page.Annotations.Add(link);

link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(40, 800, 60, 820));
link.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
link.Action = new GoToRemoteAction("AddExtractLink.pdf", 1);
page.Annotations.Add(link);

// Save documents with link
pdfDocument.Save("AddExtractLink.pdf");

// Load document to get links
pdfDocument = new Document("AddExtractLink.pdf");
page = pdfDocument.Pages[1];

// Get all links
AnnotationSelector selector = new AnnotationSelector(new LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));
page.Accept(selector);
System.Collections.IList list = selector.Selected;

foreach (var annot in list)
{
    LinkAnnotation linkannot = (LinkAnnotation)annot;
    Console.WriteLine("link type is : " + linkannot.Action.GetType().Name);
}

I am also attaching the output file generated by the above code. I hope this will help. In case of any further assistance please feel free to contact us.

Best Regards,

Hi,


Thanks for your response. How do I get the URI/Destination/File name regardless of what action type it is? I want to extract all links from a document and only covering the following actions (using if condition):

if(action == GoToRemoteAction)
{
var linkRemote = (annot.Action as GoToRemoteAction)
return linkRemote.File.Name //this returns the filename
else if (action == LaunchAction)
var linkLaunch = (annot.Action as LaunchAction)
return linkLaunch.File //this returns the filename
else if (action == GoToURIAction)
var linkURI = (annot.Action) as GoToURIAction)
return linkURI.Url // returns url
}

I’m only covering these 3 scenarios, are there any other scenarios where a hyperlink links to a document or url?

Thanks

Hi There,

Thanks for your inquiry and sharing code snippet. There is another type of Action which is GoToAction. With this type of action you can set link target to a particular page of a document within a document. Please check the following code snippet to add and access the GoToAction of LinkAnnotation.

// Create new document
Document pdfDocument = new Document();
pdfDocument.Pages.Add();
pdfDocument.Pages.Add();
pdfDocument.Save(dataDir + "AddExtractLink.pdf");

// Load document
pdfDocument = new Document("AddExtractLink.pdf");
Page page = pdfDocument.Pages[1];
Page destpage = pdfDocument.Pages[2];

LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(70, 800, 90, 820));
link.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Yellow);
link.Action = new GoToAction(destpage);
page.Annotations.Add(link);

// Save documents with link
pdfDocument.Save("AddExtractLink.pdf");

// Load document to get links
pdfDocument = new Document("AddExtractLink.pdf");
page = pdfDocument.Pages[1];

// Get all links
AnnotationSelector selector = new AnnotationSelector(new LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));
page.Accept(selector);
System.Collections.IList list = selector.Selected;

foreach (var annot in list)
{
    LinkAnnotation linkannot = (LinkAnnotation)annot;

    if (linkannot.Action is GoToAction)
    {
        GoToAction action = (GoToAction)linkannot.Action;
        ExplicitDestination destination = (ExplicitDestination)action.Destination;
        Console.WriteLine("Page Number is : " + destination.PageNumber);
    }
}

I have also attached an output file generated by above code. In case if you need further assistance please feel free to contact us.

Best Regards,