chrisan
chrisan

Reputation: 4292

How to exact a PDF Web Link Rectangle coordinates?

When creating a PDF in Acrobat a user can create "Web or Document Links" which brings up this prompt

Create Link dialogue

This PDF was created with 3 such links. You'll have to download as Github's viewer doesnt display the rectangles.

Is there a tool/library that can read and extract the x,y WxH of these rectangles and the links they contain?

Linux command line, python, php?

I've tried poppler pdftohtml -xml test3.pdf however it only gets 2 of the Link Rectangles

?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pdf2xml SYSTEM "pdf2xml.dtd">

<pdf2xml producer="poppler" version="0.49.0">
<page number="1" position="absolute" top="0" left="0" height="1294" width="646">
    <fontspec id="0" size="30" family="Times" color="#000000"/>
<image top="0" left="0" width="647" height="1295" src="test3-1_1.jpg"/>
<text top="163" left="89" width="105" height="47" font="0"><a href="http://www.google.com"><b>test 1 </b></a></text>
<text top="425" left="155" width="97" height="46" font="0"><a href="[email protected]"><b>test 2</b></a></text>
</page>
</pdf2xml>

Upvotes: 1

Views: 488

Answers (1)

Jan Slabon
Jan Slabon

Reputation: 5058

We offer a commercial tool in PHP which would allow you to access link annotations. It is possible with the SetaPDF-Core component:

<?php
// load and register the autoload function
require_once('library/SetaPDF/Autoload.php');

// create a document instance
$document = SetaPDF_Core_Document::loadByFilename('document-with-links.pdf');

// Get the pages helper
$pages = $document->getCatalog()->getPages();

for ($pageNo = 1, $pageCount = $pages->count(); $pageNo <= $pageCount; $pageNo++) {
    $page = $pages->getPage($pageNo);
    $annotationsHelper = $page->getAnnotations();
    $linkAnnotations = $annotationsHelper->getAll(SetaPDF_Core_Document_Page_Annotation::TYPE_LINK);
    foreach ($linkAnnotations AS $linkAnnotation) {
        // $linkAnnotation is an instance of SetaPDF_Core_Document_Page_Annotation_Link
        $rect = $linkAnnotation->getRect();
        $llx = $rect->getLlx();
        $lly = $rect->getLly();
        $width = $rect->getWidht();
        $height = $rect->getHeight();
        // ...
    }
}

See here for the API doc of a link annotation.

This simple demo script doesn't care about rotated pages. The returned values are the values defined in the annotation itself.

Upvotes: 0

Related Questions