PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Document Outline (bookmarks)

A PDF document may optionally display a document outline on the screen, allowing the user to navigate interactively from one part of the document to another. The outline consists of a tree-structured hierarchy of outline items (sometimes called bookmarks), which serve as a visual table of contents to display the document's structure to the user. The user can interactively open and close individual items by clicking them with the mouse. When an item is open, its immediate children in the hierarchy become visible on the screen; each child may in turn be open or closed, selectively revealing or hiding further parts of the hierarchy. When an item is closed, all of its descendants in the hierarchy are hidden. Clicking the text of any visible item activates the item, causing the viewer application to jump to a destination or trigger an action associated with the item.

Zend_Pdf class provides public property $outlines which is an array of Zend_Pdf_Outline objects.

<?php
$pdf 
Zend_Pdf::load($path);

// Remove outline item
unset($pdf->outlines[0]->childOutlines[1]);

// Set Outline to be displayed in bold
$pdf->outlines[0]->childOutlines[3]->setIsBold(true);

// Add outline entry
$pdf->outlines[0]->childOutlines[5]->childOutlines[] =
    
Zend_Pdf_Outline::create('Chapter 2''chapter_2');

$pdf->save($pathtrue);

Outline attributes may be retrieved or set using the following methods:

  • string getTitle() - get outline item title.

  • setTitle(string $title) - set outline item title.

  • boolean isOpen() - TRUE if outline is open by default.

  • setIsOpen(boolean $isOpen) - set isOpen state.

  • boolean isItalic() - TRUE if outline item is displayed in italic.

  • setIsItalic(boolean $isItalic) - set isItalic state.

  • boolean isBold() - TRUE if outline item is displayed in bold.

  • setIsBold(boolean $isBold) - set isBold state.

  • Zend_Pdf_Color_Rgb getColor() - get outline text color (NULL means black).

  • setColor(Zend_Pdf_Color_Rgb $color) - set outline text color (NULL means black).

  • Zend_Pdf_Target getTarget() - get outline target (action or explicit or named destination object).

  • setTarget(Zend_Pdf_Target|string $target) - set outline target (action or destination). String may be used to identify named destination. NULL means 'no target'.

  • array getOptions() - get outline attributes as an array.

  • setOptions(array $options) - set outline options. The following options are recognized: 'title', 'open', 'color', 'italic', 'bold', and 'target'.

New outline may be created in two ways:

  • Zend_Pdf_Outline::create(string $title[, Zend_Pdf_Target|string $target])

  • Zend_Pdf_Outline::create(array $options)

Each outline object may have child outline items listed in Zend_Pdf_Outline::$childOutlines public property. It's an array of Zend_Pdf_Outline objects, so outlines are organized in a tree.

Zend_Pdf_Outline class implements RecursiveArray interface, so child outlines may be recursively iterated using RecursiveIteratorIterator:

<?php
$pdf 
Zend_Pdf::load($path);

foreach (
$pdf->outlines as $documentRootOutlineEntry) {
    
$iterator = new RecursiveIteratorIterator(
                    
$documentRootOutlineEntry,
                    
RecursiveIteratorIterator::SELF_FIRST
                
);
    foreach (
$iterator as $childOutlineItem) {
        
$OutlineItemTarget $childOutlineItem->getTarget();
        if (
$OutlineItemTarget instanceof Zend_Pdf_Destination) {
            if (
$pdf->resolveDestination($OutlineItemTarget) === null) {
                
// Mark Outline item with unresolvable destination
                // using RED color
                
$childOutlineItem->setColor(new Zend_Pdf_Color_Rgb(100));
            }
        } else if (
$OutlineItemTarget instanceof Zend_Pdf_Action_GoTo) {
            
$OutlineItemTarget->setDestination();
            if (
$pdf->resolveDestination($OutlineItemTarget) === null) {
                
// Mark Outline item with unresolvable destination
                // using RED color
                
$childOutlineItem->setColor(new Zend_Pdf_Color_Rgb(100));
            }
        }
    }
}

$pdf->save($pathtrue);

Note

All outline items with unresolved destinations (or destinations of GoTo actions) are updated while document saving by setting their targets to NULL. So document will not be corrupted by removing pages referenced by outlines.

Zend Framework