# Translation of the bookmark class from the PHP FPDF script from Olivier Plathey # Translated by Sylvain Lafleur and ?? with the help of Brian Ollenberger # # First added in 1.53b # # Usage is as follows: # # require 'fpdf' # require 'bookmark' # pdf = FPDF.new # pdf.extend(PDF_Bookmark) # # This allows it to be combined with other extensions, such as the Chinese # module. module PDF_Bookmark def PDF_Bookmark.extend_object(o) o.instance_eval('@outlines,@OutlineRoot=[],0') super(o) end def Bookmark(txt,level=0,y=0) y=self.GetY() if y==-1 @outlines.push({'t'=>txt,'l'=>level,'y'=>y,'p'=>self.PageNo()}) end def putbookmarks @nb=@outlines.size return if @nb==0 lru=[] level=0 @outlines.each_index do |i| o=@outlines[i] if o['l']>0 parent=lru[o['l']-1] # Set parent and last pointers @outlines[i]['parent']=parent @outlines[parent]['last']=i if o['l']>level # Level increasing: set first pointer @outlines[parent]['first']=i end else @outlines[i]['parent']=@nb end if o['l']<=level and i>0 # Set prev and next pointers prev=lru[o['l']] @outlines[prev]['next']=i @outlines[i]['prev']=prev end lru[o['l']]=i level=o['l'] end # Outline items n=@n+1 @outlines.each_index do |i| o=@outlines[i] newobj out('<>') out('endobj') end # Outline root newobj @OutlineRoot=@n out('<>') out('endobj') end def putresources super putbookmarks end def putcatalog super if not @outlines.empty? out('/Outlines '+@OutlineRoot.to_s+' 0 R') out('/PageMode /UseOutlines') end end end