2 /*************************************************************************
3 * http://www.fpdf.org/en/script/script1.php
7 * This extension adds bookmark support. The method to add a bookmark is:
9 * function Bookmark(string txt [, int level [, float y]])
11 * txt: the bookmark title.
12 * level: the bookmark level (0 is top level, 1 is just below, and so on).
13 * y: the y position of the bookmark destination in the current page. -1 means the current position. Default value: 0.
15 * The title must be encoded in ISO Latin-1.
16 ************************************************************************/
17 /*************************************************************************
18 * http://www.fpdf.org/en/script/script13.php
22 * This class prints an index from the created bookmarks.
23 ************************************************************************/
25 if (!defined('__CLASS_FPDF_BOOKMARK__'))
27 define('__CLASS_FPDF_BOOKMARK__', true);
29 require_once(dirname(__FILE__
).'/00_fpdf_codebar.class.php');
31 class FPDF_BookMark
extends FPDF_Codebar
33 var $outlines=array();
36 function FPDF_BookMark($orientation='P',$unit='mm',$format='A4')
38 $this->FPDF_Codebar($orientation,$unit,$format);
42 function Bookmark($txt, $level=0, $y=0)
44 if($y==-1) $y=$this->GetY();
45 $this->outlines
[]=array('t'=>$txt, 'l'=>$level, 'y'=>($this->h
-$y)*$this->k
, 'p'=>$this->PageNo());
48 function _putbookmarks()
50 $nb=count($this->outlines
);
54 foreach($this->outlines
as $i=>$o)
58 $parent=$lru[$o['l']-1];
59 //Set parent and last pointers
60 $this->outlines
[$i]['parent']=$parent;
61 $this->outlines
[$parent]['last']=$i;
64 //Level increasing: set first pointer
65 $this->outlines
[$parent]['first']=$i;
69 $this->outlines
[$i]['parent']=$nb;
71 if($o['l']<=$level and $i>0)
73 //Set prev and next pointers
75 $this->outlines
[$prev]['next']=$i;
76 $this->outlines
[$i]['prev']=$prev;
84 foreach($this->outlines
as $i=>$o)
87 $this->_out('<</Title '.$this->_textstring($o['t']));
88 $this->_out('/Parent '.($n+
$o['parent']).' 0 R');
90 $this->_out('/Prev '.($n+
$o['prev']).' 0 R');
92 $this->_out('/Next '.($n+
$o['next']).' 0 R');
93 if(isset($o['first']))
94 $this->_out('/First '.($n+
$o['first']).' 0 R');
96 $this->_out('/Last '.($n+
$o['last']).' 0 R');
97 $this->_out(sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]',1+
2*$o['p'],$o['y']));
98 $this->_out('/Count 0>>');
99 $this->_out('endobj');
104 $this->OutlineRoot
=$this->n
;
105 $this->_out('<</Type /Outlines /First '.$n.' 0 R');
106 $this->_out('/Last '.($n+
$lru[0]).' 0 R>>');
107 $this->_out('endobj');
110 function _putresources()
112 parent
::_putresources();
113 $this->_putbookmarks();
116 function _putcatalog()
118 parent
::_putcatalog();
119 if(count($this->outlines
)>0)
121 $this->_out('/Outlines '.$this->OutlineRoot
.' 0 R');
122 $this->_out('/PageMode /UseOutlines');
126 function CreateIndex(&$obj, $titre = 'Index', $size_title = 20, $size_bookmark = 15, $bookmark_title = true, $display_page = true, $page = null)
128 if ($bookmark_title) $this->Bookmark($titre, 0, -1);
131 $this->SetFontSize($size_title);
132 $this->Cell(0,5,$titre,0,1,'C');
133 $this->SetFontSize($size_bookmark);
136 $size=sizeof($this->outlines
);
137 $PageCellSize=$this->GetStringWidth('p. '.$this->outlines
[$size-1]['p'])+
2;
138 for ($i=0;$i<$size;$i++
)
140 if ($this->getY()+
$this->FontSize
>=($this->h
- $this->bMargin
))
142 $obj->INDEX_NewPage($page);
143 $this->SetFontSize($size_bookmark);
147 $level=$this->outlines
[$i]['l'];
148 if($level>0) $this->Cell($level*8);
151 $str=$this->outlines
[$i]['t'];
152 $strsize=$this->GetStringWidth($str);
153 $avail_size=$this->w
-$this->lMargin
-$this->rMargin
-$PageCellSize-($level*8)-4;
154 while ($strsize>=$avail_size)
156 $str=substr($str,0,-1);
157 $strsize=$this->GetStringWidth($str);
161 $this->Cell($strsize+
2,$this->FontSize+
2,$str);
164 $w=$this->w
-$this->lMargin
-$this->rMargin
-$PageCellSize-($level*8)-($strsize+
2);
165 $nb=$w/$this->GetStringWidth('.');
166 $dots=str_repeat('.',$nb);
167 $this->Cell($w,$this->FontSize+
2,$dots,0,0,'R');
170 $this->Cell($PageCellSize,$this->FontSize+
2,'p. '.$this->outlines
[$i]['p'],0,1,'R');
174 $this->Cell($strsize+
2,$this->FontSize+
2,$str, 0, 1);