Many PDF documents can contain so-called Link Annotations (see PDF32000 specification, p.394, 220.127.116.11 Link Annotations). For readers of the PDF document these objects may be presented as links to other pages within the same document or external destinations. Combined with bookmarks which are used to build a table of contents for PDF document, links help document authors to create a solid doc viewing experience.
When it comes to own PDF viewer implementation or if you just want to read whether there are any links on PDF page, Apitron PDF Rasterizer for .NET provides a uniform way to read, highlight or navigate using page links.
In short, a PDF Link Annotation object used to create a link is just a marker that defines which region of the page can interact with the used and trigger navigation. It doesn’t contain any text or other data except its “hot” region and target definition that in PDF terms is called Destination. But you don’t have to dive into the details of PDF format to use our API, because we provided a very clear and simple way to use this feature.
Product package that you can download from our website contains the sample called LinksUsageSample, it demonstrates how to find all links on PDF page, highlight them using our product and render linked pages.
Assuming that you have stored current PDF page in variable named currentPage:
// enumerate through the page links collection and render linked pages
for(int i=0;i< currentPage.Links.Count;i++ )
// try to navigate using given link and save linked page as bitmap
// we use default PDF dpi settings for image as well as default rendering settings
using (Bitmap bitmap = document.Navigator.CurrentPage.Render(new Resolution(72,72), settings))
Highlight all links on PDF page:
private static void SavePageAsBitmapWithHightlightedLinks(Page page, RenderingSettings settings)
int pageWidth = (int)page.Width;
int pageHeight = (int)page.Height;
// render page
using (Bitmap bitmap = page.Render(pageWidth,pageHeight, settings))
// create graphics object that we will use for drawing of the highlighting rects
using (Graphics g = Graphics.FromImage(bitmap))
using(Brush highlightBrush = new SolidBrush(Color.FromArgb(0x5FFFFF00)))
foreach (Link link in page.Links)
Apitron.PDF.Rasterizer.Rectangle linkLocation = link.GetLocationRectangle(pageWidth,pageHeight,settings);
// PDF coordinate system has Y axis inverted in comparison to GDI, so transform the Y coordinate of the rect here
// because link object coordinates will be returned using PDF coordinate system.
g.FillRectangle(highlightBrush, new RectangleF((float) linkLocation.Left, pageHeight-(float) linkLocation.Top,(float) linkLocation.Width,(float) linkLocation.Height));
|Page with highlighted links|
As it can be seen from the sample, getting all links from page and highlighting them is a quite simple task and you don’t have to write much code to do this using our component.
If you take a look at the first piece of code you’ll notice that there we use document.Navigator property - it’s a part of build-in navigation mechanism and also can be used to navigate using bookmarks, search results, links, page indices, page instances or to get to the next our previous page of the document.
It exposes DocumentNavigator object and is a must use for custom pdf viewer implementations. You can also read our other posts describing how to read pdf bookmarks (table of contents) and how to search text in PDF file.