Extract hyperlinks from document page area
Leave feedback
On this page
GroupDocs.Parser allows extracting hyperlinks from specific rectangular areas of document pages, enabling precise link extraction from defined regions.
Prerequisites
GroupDocs.Parser for Python via .NET installed
Sample documents with hyperlinks
Understanding of coordinate systems
Extract hyperlinks from page area
To extract hyperlinks from a specific rectangular area:
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportPageAreaOptionsfromgroupdocs.parser.dataimportRectangle,Point,Size# Create an instance of Parser classwithParser("./document.pdf")asparser:# Check if hyperlink extraction is supportedifnotparser.features.hyperlinks:print("Hyperlink extraction not supported")else:# Define the area (upper portion, 600x200 pixels)area=Rectangle(Point(0,0),Size(600,200))# Create options for the areaoptions=PageAreaOptions(area)# Extract hyperlinks from the specified areahyperlinks=parser.get_hyperlinks(options)ifhyperlinks:print("Hyperlinks found in area:")forlinkinhyperlinks:print(f" {link.text} -> {link.url}")else:print("No hyperlinks found in the specified area")
The following sample file is used in this example: document.pdf
Hyperlinks found in area:
Investor relations -> https://harborlane.example/investors
Expected behavior: Returns only hyperlinks located within the specified rectangular area.
Extract hyperlinks from specific page area
To extract hyperlinks from an area on a specific page:
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportPageAreaOptionsfromgroupdocs.parser.dataimportRectangle,Point,Size# Create an instance of Parser classwithParser("document.pdf")asparser:ifnotparser.features.hyperlinks:print("Hyperlink extraction not supported")else:# Define area (sidebar region)area=Rectangle(Point(400,0),Size(200,800))options=PageAreaOptions(area)# Get document infoinfo=parser.get_document_info()# Process first pagepage_index=0ifpage_index<info.page_count:# Extract hyperlinks from area on specific pagehyperlinks=parser.get_hyperlinks(page_index,options)ifhyperlinks:print(f"Hyperlinks in sidebar (page {page_index+1}):")forlinkinhyperlinks:print(f" {link.text}: {link.url}")else:print(f"No hyperlinks in sidebar (page {page_index+1})")
The following sample file is used in this example: document.pdf
Hyperlinks in sidebar (page 1):
Investor relations: https://harborlane.example/investors
Expected behavior: Extracts navigation or menu links typically found in document headers.
Extract links from multiple regions
To extract hyperlinks from several predefined areas:
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportPageAreaOptionsfromgroupdocs.parser.dataimportRectangle,Point,Sizedefextract_links_from_regions(file_path,regions):"""
Extract hyperlinks from multiple regions.
Args:
file_path: Path to document
regions: Dict of region names to (x, y, width, height) tuples
"""withParser(file_path)asparser:ifnotparser.features.hyperlinks:print("Hyperlink extraction not supported")return{}results={}forregion_name,(x,y,w,h)inregions.items():# Create rectangle for this regionarea=Rectangle(Point(x,y),Size(w,h))options=PageAreaOptions(area)# Extract hyperlinks from this regionhyperlinks=parser.get_hyperlinks(options)ifhyperlinks:results[region_name]=[link.urlforlinkinhyperlinks]else:results[region_name]=[]returnresults# Define regionsregions={'header':(0,0,600,100),'body':(0,100,600,650),'footer':(0,780,600,62)}# Extract links from regionslinks_by_region=extract_links_from_regions("document.pdf",regions)forregion,urlsinlinks_by_region.items():print(f"{region.upper()}: {len(urls)} links")forurlinurls[:5]:# Show first 5print(f" - {url}")
The following sample file is used in this example: document.pdf
Expected behavior: Extracts links commonly found in document footers (e.g., copyright, contact, social media).
Filter links by area and URL pattern
To extract links from an area matching a URL pattern:
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportPageAreaOptionsfromgroupdocs.parser.dataimportRectangle,Point,Sizeimportredefextract_links_by_area_and_pattern(file_path,area_rect,url_pattern):"""
Extract links from area matching URL pattern.
Args:
file_path: Path to document
area_rect: Tuple (x, y, width, height)
url_pattern: Regex pattern for URLs
"""x,y,w,h=area_rectarea=Rectangle(Point(x,y),Size(w,h))options=PageAreaOptions(area)withParser(file_path)asparser:ifnotparser.features.hyperlinks:print("Hyperlink extraction not supported")return[]hyperlinks=parser.get_hyperlinks(options)ifnothyperlinks:return[]# Filter by URL patternmatching_links=[]pattern=re.compile(url_pattern,re.IGNORECASE)forlinkinhyperlinks:iflink.urlandpattern.search(link.url):matching_links.append({'text':link.text,'url':link.url})returnmatching_links# Usage - extract the investor and supplier links from the page bodybody_area=(0,100,600,650)section_links=extract_links_by_area_and_pattern("document.pdf",body_area,r'/(investors|suppliers)/')print(f"Investor and supplier links in the body: {len(section_links)}\n")forlinkinsection_links:print(f" {link['text']}: {link['url']}")
The following sample file is used in this example: document.pdf
Investor and supplier links in the body: 2
harborlane.example/investors/2025: https://harborlane.example/investors/2025
harborlane.example/suppliers/charter: https://harborlane.example/suppliers/charter
Expected behavior: Extracts only links from the specified area that match the URL pattern.
Extract social media links from specific area
To extract social media links from a defined region:
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportPageAreaOptionsfromgroupdocs.parser.dataimportRectangle,Point,Sizedefextract_social_media_links(file_path,area_rect):"""
Extract social media links from a specific area.
"""x,y,w,h=area_rectarea=Rectangle(Point(x,y),Size(w,h))options=PageAreaOptions(area)# Social media domainssocial_domains=['facebook.com','twitter.com','linkedin.com','instagram.com','youtube.com','tiktok.com']withParser(file_path)asparser:ifnotparser.features.hyperlinks:print("Hyperlink extraction not supported")return{}hyperlinks=parser.get_hyperlinks(options)ifnothyperlinks:return{}# Categorize by platformsocial_links={}forlinkinhyperlinks:iflink.url:fordomaininsocial_domains:ifdomaininlink.url:platform=domain.split('.')[0].capitalize()urls=social_links.setdefault(platform,[])iflink.urlnotinurls:# the footer repeats on every pageurls.append(link.url)breakreturnsocial_links# Usage - extract from the footer area of an A4 pagefooter_area=(0,780,600,62)social_links=extract_social_media_links("document.pdf",footer_area)print("Social Media Links:")forplatform,urlsinsocial_links.items():print(f" {platform}: {', '.join(urls)}")ifnotsocial_links:print(" none in this area")
The following sample file is used in this example: document.pdf
Social Media Links:
Linkedin: https://www.linkedin.com/company/harbor-lane-provisions-example
Instagram: https://www.instagram.com/harborlane.example/
Youtube: https://www.youtube.com/@harborlane-example