Skip to content

Commit 340fb7f

Browse files
committed
Added "Core > Images > Add Below Annotation" demo
1 parent b2add04 commit 340fb7f

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
2.05 KB
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Add Image Below Link Annotation",
3+
"teaserText": "Add an image at the position of a link annotation.",
4+
"requires": [
5+
"SetaPDF-Core"
6+
],
7+
"faIcon": "",
8+
"faIcon2": ""
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<p>
2+
This demo searches for link annotations on a page and places images on the found positions and removes the
3+
link annotation afterwards.
4+
</p>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
// load and register the autoload function
4+
require_once '../../../../../bootstrap.php';
5+
6+
$writer = new \SetaPDF_Core_Writer_Http('result.pdf', true);
7+
$document = \SetaPDF_Core_Document::loadByFilename(
8+
$assetsDirectory . '/pdfs/misc/link-annotation-placeholders.pdf',
9+
$writer
10+
);
11+
12+
// let's prepare some kind of mapping
13+
$signatrues = [
14+
'NameOfPersonX' => $assetsDirectory . '/images/Handwritten-Signature.png',
15+
'NameOfPersonY' => $assetsDirectory . '/images/seal.png',
16+
];
17+
18+
// get access to the pages object
19+
$pages = $document->getCatalog()->getPages();
20+
21+
// get the first page
22+
$pageOne = $pages->getPage(1);
23+
24+
// make sure that we have a clean graphic state
25+
$pageOne->getContents()->encapsulateExistingContentInGraphicState();
26+
27+
// get the canvas
28+
$canvas = $pageOne->getCanvas();
29+
30+
$annotations = $pageOne->getAnnotations();
31+
/** @var SetaPDF_Core_Document_Page_Annotation_Link[] $linkAnnotations */
32+
$linkAnnotations = $annotations->getAll(SetaPDF_Core_Document_Page_Annotation::TYPE_LINK);
33+
foreach ($linkAnnotations as $linkAnnotation) {
34+
$action = $linkAnnotation->getAction();
35+
if ($action instanceof SetaPDF_Core_Document_Action_Uri) {
36+
// let's parse the uri/url and ensure some keys/values. The URLs in the example document look like:
37+
// signature://yourDomain.com#nameOfPerson
38+
$uri = parse_url($action->getUri());
39+
if (
40+
!is_array($uri)
41+
|| !isset($uri['scheme'], $uri['fragment'], $signatrues[$uri['fragment']])
42+
|| $uri['scheme'] !== 'signature'
43+
) {
44+
continue;
45+
}
46+
47+
$imgPath = $signatrues[$uri['fragment']];
48+
$image = \SetaPDF_Core_Image::getByPath($imgPath)->toXObject($document);
49+
50+
// let's create a new XObject to scale/fit the signature image:
51+
$rect = $linkAnnotation->getRect();
52+
$height = $rect->getHeight();
53+
$width = $rect->getWidth();
54+
$xObject = SetaPDF_Core_XObject_Form::create($document, [0, 0, $width, $height]);
55+
$xObjectCanvas = $xObject->getCanvas();
56+
57+
// fit the image into the size of the annotation
58+
$maxWidth = $image->getWidth($height);
59+
$maxHeight = $image->getHeight($width);
60+
61+
$x = 0;
62+
$y = 0;
63+
if ($maxHeight > $height) {
64+
$x += $width / 2 - $maxWidth / 2;
65+
$image->draw($xObjectCanvas, $x, $y, null, $height);
66+
} else {
67+
$y += $height / 2 - $maxHeight / 2;
68+
$image->draw($xObjectCanvas, $x, $y, $width, null);
69+
}
70+
71+
// draw the new xObject onto the main canvas
72+
$xObject->draw($canvas, $rect->llx, $rect->lly);
73+
74+
// ...and remove the annotation
75+
$annotations->remove($linkAnnotation);
76+
}
77+
}
78+
79+
// save and finish
80+
$document->save()->finish();

0 commit comments

Comments
 (0)