Link Annotation Class

Table of Contents

  1. Introduction
    1. Methods
      1. Example

        Introduction

        The link annotation represents either a hypertext link to a destination in the document or an action to be performed.

        Methods

        The class implements getter and setter methods to access link annotation data:

        getAction()

        Get the action of the item.

        getDestination()

        Get the destination of the item.

        getQuadPoints()

        Get the quad points.

        setAction()

        Set the action of the annotation.

        setDestination()

        Set the destination of the item.

        setQuadPoints()

        Set the quad points.

        Example

        PHP
        <?php
        require_once('library/SetaPDF/Autoload.php');
        
        $writer = new \SetaPDF_Core_Writer_Http('link-annotation.pdf', true);
        $document = new \SetaPDF_Core_Document($writer);
        
        // create a font instance
        $font = \SetaPDF_Core_Font_Standard_Helvetica::create($document);
        
        // let's create a page
        $pages = $document->getCatalog()->getPages();
        $page = $pages->create(\SetaPDF_Core_PageFormats::A4);
        
        // get access to the annotations helper
        $annotations = $page->getAnnotations();
        
        // lets' keep track of the text
        $canvas = $page->getCanvas();
        $canvas->setGraphicStateSync(
            \SetaPDF_Core_Canvas::GS_SYNC_CURRENT_TRANSFORMATION_MATRIX |
            \SetaPDF_Core_Canvas::GS_SYNC_TEXT
        );
        
        // write the link text and remember its start and end points
        $text = $canvas->text()
            ->begin()
            ->setFont($font, 20)
            ->moveToNextLine(50, 700);
        	
        $start = $canvas->graphicState()->text()->getBottomUserSpace();
        $text->showText('A link to https://www.setasign.com');
        $end = $canvas->graphicState()->text()->getTopUserSpace();
        $text->end();
        
        // create the link annotation
        $link = new \SetaPDF_Core_Document_Page_Annotation_Link(
            array($start->getX(), $start->getY(), $end->getX(), $end->getY()),
            new \SetaPDF_Core_Document_Action_Uri('https://www.setasign.com')
        );
        // define its color
        $link->setColor(array(1, 0, 0));
        // define a border and style
        $borderStyle = $link->getBorderStyle();
        $borderStyle->setWidth(1);
        $borderStyle->setStyle(\SetaPDF_Core_Document_Page_Annotation_BorderStyle::SOLID);
        // add the new link annotation
        $annotations->add($link);
        
        // write another link text and remember its start and end points
        $text = $canvas->text()
            ->begin()
            ->setFont($font, 20)
            ->moveToNextLine(50, 650);
        	
        $start = $canvas->graphicState()->text()->getBottomUserSpace();
        $text->showText('This is a JavaScript action!');
        $end = $canvas->graphicState()->text()->getTopUserSpace();
        $text->end();
        
        // create another link annotation
        $link2 = new \SetaPDF_Core_Document_Page_Annotation_Link(
            array($start->getX(), $start->getY(), $end->getX(), $end->getY()),
            new \SetaPDF_Core_Document_Action_JavaScript('app.alert("SetaPDF rockz!");')
        );
        // define its color
        $link2->setColor(array(0, 0, 1));
        // define a border and style
        $borderStyle = $link2->getBorderStyle();
        $borderStyle->setWidth(4);
        $borderStyle->setStyle(\SetaPDF_Core_Document_Page_Annotation_BorderStyle::DASHED);
        $borderStyle->setDashPattern(array(2, 2));
        // add the new link annotation
        $annotations->add($link2);
        
        $document->save()->finish();

        If a scalar value is passed as action or destination parameter in the constructor it will automatically become an Uri action.