-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new demo "Extractor > Various > Add Text Above Found String"
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
public/demos/2-Extractor/various/add-text-above-found-string/demo.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "Add Text Above Found String", | ||
"teaserText": "This demo finds a string/number, adds a white rectangle and text surrounded by a red border above it.", | ||
"requires": [ | ||
"SetaPDF-Core", "SetaPDF-Extractor" | ||
], | ||
"previewFiles": [ | ||
"/assets/pdfs/tektown/Laboratory-Report.pdf" | ||
] | ||
} |
4 changes: 4 additions & 0 deletions
4
public/demos/2-Extractor/various/add-text-above-found-string/description.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<p> | ||
This demo finds a string/number (labeled as the "Task Number" in the document), adds a white rectangle and text | ||
surrounded by a red border above it. | ||
</p> |
75 changes: 75 additions & 0 deletions
75
public/demos/2-Extractor/various/add-text-above-found-string/script.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
// load and register the autoload function | ||
require_once __DIR__ . '/../../../../../bootstrap.php'; | ||
|
||
$document = \SetaPDF_Core_Document::loadByFilename( | ||
$assetsDirectory . '/pdfs/tektown/Laboratory-Report.pdf' | ||
); | ||
|
||
// initate an extractor instance | ||
$extractor = new \SetaPDF_Extractor($document); | ||
|
||
// define the word strategy | ||
$strategy = new \SetaPDF_Extractor_Strategy_Word(); | ||
$extractor->setStrategy($strategy); | ||
|
||
// get the pages helper | ||
$pages = $document->getCatalog()->getPages(); | ||
|
||
// let's find all placeholders | ||
$matches = []; | ||
for ($pageNo = 1; $pageNo <= $pages->count(); $pageNo++) { | ||
/** | ||
* @var \SetaPDF_Extractor_Result_Words $words | ||
*/ | ||
$words = $extractor->getResultByPageNumber($pageNo); | ||
// we search for the number "11563" | ||
$matches[] = [$pageNo, $words->search('/11563/')]; | ||
} | ||
|
||
if (count($matches)) { | ||
$font = new \SetaPDF_Core_Font_TrueType_Subset( | ||
$document, | ||
$assetsDirectory . '/fonts/DejaVu/ttf/DejaVuSans.ttf' | ||
); | ||
} | ||
|
||
// iterate over the matches | ||
foreach ($matches AS list($pageNo, $results)) { | ||
/** @var \SetaPDF_Extractor_Result_Words $segments */ | ||
foreach ($results as $segments) { | ||
// get the bounds of the found phrase | ||
$bounds = $segments->getBounds(); | ||
$rect = $bounds[0]->getRectangle(); | ||
|
||
// get the page object | ||
$page = $pages->getPage($pageNo); | ||
// make sure that the new content is encapsulated in a seperate content stream | ||
$page->getContents()->encapsulateExistingContentInGraphicState(); | ||
// get the canvas object | ||
$canvas = $page->getCanvas(); | ||
|
||
// get some rect data | ||
$x = $rect->getLl()->getX(); | ||
$y = $rect->getLl()->getY(); | ||
$width = $rect->getWidth(); | ||
$height = $rect->getHeight(); | ||
|
||
// draw a white rectangle | ||
$canvas->draw() | ||
->setNonStrokingColor(1) | ||
->rect($x, $y, $width, $height, \SetaPDF_Core_Canvas_Draw::STYLE_FILL); | ||
|
||
$textBlock = new SetaPDF_Core_Text_Block($font, $height * .7); | ||
$textBlock->setText('875631'); | ||
$textBlock->setAlign(SetaPDF_Core_Text::ALIGN_CENTER); | ||
$textBlock->setBorderColor([1, 0, 0]); | ||
$textBlock->setBorderWidth(1); | ||
$textBlock->draw($canvas, $x, $y); | ||
} | ||
} | ||
|
||
// save and finish the document | ||
$document->setWriter(new \SetaPDF_Core_Writer_Http('document.pdf', true)); | ||
$document->save()->finish(); |