如何精确定位PDF Web Link矩形坐标?

时间:2019-04-15 13:01:24

标签: php python bash pdf poppler

在Acrobat中创建PDF时,用户可以创建“ Web或文档链接”,以显示此提示

Create Link dialogue

This PDF是使用3个此类链接创建的。您必须下载,因为Github的查看器不会显示矩形。

有没有可以读取和提取这些矩形的x,y WxH及其包含的链接的工具/库?

Linux命令行,python,php?

我尝试过poppler pdftohtml -xml test3.pdf,但是它只能获得2个链接矩形

?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="larry@google.com"><b>test 2</b></a></text>
</page>
</pdf2xml>

1 个答案:

答案 0 :(得分:0)

我们提供了一个PHP商业工具,该工具可让您访问链接注释。 SetaPDF-Core组件是可能的:

<?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();
        // ...
    }
}

有关链接注释的API文档,请参见here

这个简单的演示脚本并不关心旋转的页面。返回的值是注释本身中定义的值。