Extract Links from the PDF File
Contents
[
Hide
]
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.
void ExtractLinksFromThePDFFile() {
// Load the PDF file
String _dataDir("C:\\Samples\\");
// Create Document instance
auto document = MakeObject<Document>(_dataDir + u"UpdateLinks.pdf");
// Add page to pages collection of PDF file
auto page = document->get_Pages()->idx_get(1);
auto selector = MakeObject<Aspose::Pdf::Annotations::AnnotationSelector>(MakeObject<Aspose::Pdf::Annotations::LinkAnnotation>(page, Rectangle::get_Trivial()));
page->Accept(selector);
auto list = selector->get_Selected();
for (auto annot : list)
{
Console::WriteLine(u"Annotation located: {0}", annot->get_Rect());
}
}