Index
- Installation
- Getting Started
- Memory Usage
- Readers and Writers
- The Document Class
- Metadata
- Pages
- Canvas
- Page Layout and Mode
- Viewer Preferences
- Document Outline
- Page Labels
- Actions
- Destinations
- Annotations
- Embedded File Streams
- Colors and Color Spaces
- Page Formats and Boundaries
- Standard and Public Key Encryption
- Fonts and Encodings
- Corrupted Documents
- Reader Enabled Documents
- Refactor Old SetaPDF Code
- API Reference
SetaPDF-LinkReplacer Implement identical functionality as the SetaPDF-LinkReplacer offered
Table of Contents
Introduction
As of SetaPDF 2 the so called SetaPDF-LinkReplacer API is obsolete, because all features are available in the Core component.
This article will show replacement code for specific methods of the old API.
Error Handling
While in version 1 error objects were returned, version 2 uses Exceptions throughout. More details can be found here.
Loading and Saving PDF Documents
The SetaPDF-LinkReplacer offered only the possibility to load a PDF file from a file. The new system allows a PDF document to be read from various sources through so called Reader objects.
Nearly same things exists for the resulting document: SetaPDF_Core_Writer_WriterInterface.
$filename = 'a/path/to/a/pdf/file.pdf'; $target = 'a/path/to/the/resulting/file.pdf'; // old version $linkReplacer = \SetaPDF_LinkReplacer::factory($filename); // ... $linkReplacer->replaceLinks($target); // will become: $reader = new \SetaPDF_Core_Reader_File($filename); $writer = new \SetaPDF_Core_Writer_File($target); $document = \SetaPDF_Core_Document::load($reader, $writer); // ... $document->save()->finish();
In the new version it is possible to define separate readers or writers, which was impossible for the old version.
Equivalent Code of Methods
The old methods can be refactored to the new version as follows:
setUseUpdate()
The behavior of the setUseUpdate() method is identical to the $update parameter of the SetaPDF_Core_Document::save()
method.
$update = false; // old $linkReplacer->setUseUpdate($update); // new $document->save($update);
getPageCount()
In the new version a helper class has to be used:
// old: $pageCount = $linkReplacer->getPageCount(); // new: $pageCount = $document->getCatalog()->getPages()->count();
getLinks()
In the new version links are represented through annotation objects with specific destinations or actions attached.
These annotations are available through an annotation helper class of individual page objects. The LinkReplacer handled only Uri actions which are represented by the SetaPDF_Core_Document_Action_Uri
class.
$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 $action = $linkAnnotation->getAction(); if ($action && $action instanceof \SetaPDF_Core_Document_Action_Uri) { // Here we have access to the Uri action // ... } } }
getValue()
The object type SetaPDF_Link is not available anymore but the access to the URI is possible through an Uri action instance as resolved in the previous example.
// old $uri = $link->getValue(); // new $uri = $action->getUri();
setValue()
Also setting a new URI is possible through an Uri action instance.
$uri = 'http://www.setasign.com'; // old $link->setValue($uri); // new $action->setUri($uri);