Extract Links from the PDF File
Contents
[
Hide
]
The following code snippet also work with Aspose.PDF.Drawing library.
Extract Links from the PDF File
Links are represented as annotations in a PDF file, so to extract links, extract all the LinkAnnotation objects.
- Create a Document object.
- Get the Page you want to extract links from.
- Use the AnnotationSelector class to extract all the LinkAnnotation objects from the specified page.
- Pass the AnnotationSelector object to the Page object’s Accept method.
- Get all the selected link annotations into an IList object using the AnnotationSelector object’s Selected property.
The following code snippet shows you how to extract links from a PDF file.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_LinksActions();
// Open document
Document document = new Document(dataDir+ "ExtractLinks.pdf");
// Extract actions
Page page = document.Pages[1];
AnnotationSelector selector = new AnnotationSelector(new LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));
page.Accept(selector);
IList<Annotation> list = selector.Selected;
Annotation annotation = (Annotation)list[0];
dataDir = dataDir + "ExtractLinks_out.pdf";
// Save updated document
document.Save(dataDir);