To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 23 / 234b87239a907c3661f562d48ce5a22b13fe4163.svn-base @ 1298:4f746d8966dd

History | View | Annotate | Download (2.7 KB)

1
# Translation of the bookmark class from the PHP FPDF script from Olivier Plathey
2
# Translated by Sylvain Lafleur and ?? with the help of Brian Ollenberger
3
#
4
# First added in 1.53b
5
#
6
# Usage is as follows:
7
#
8
# require 'fpdf'
9
# require 'bookmark'
10
# pdf = FPDF.new
11
# pdf.extend(PDF_Bookmark)
12
#
13
# This allows it to be combined with other extensions, such as the Chinese
14
# module.
15

    
16
module PDF_Bookmark
17
    def PDF_Bookmark.extend_object(o)
18
        o.instance_eval('@outlines,@OutlineRoot=[],0')
19
        super(o)
20
    end
21

    
22
    def Bookmark(txt,level=0,y=0)
23
        y=self.GetY() if y==-1
24
        @outlines.push({'t'=>txt,'l'=>level,'y'=>y,'p'=>self.PageNo()})
25
    end
26

    
27
    def putbookmarks
28
        @nb=@outlines.size
29
        return if @nb==0
30
        lru=[]
31
        level=0
32
        @outlines.each_index do |i|
33
            o=@outlines[i]
34
            if o['l']>0
35
                parent=lru[o['l']-1]
36
                # Set parent and last pointers
37
                @outlines[i]['parent']=parent
38
                @outlines[parent]['last']=i
39
                if o['l']>level
40
                    # Level increasing: set first pointer
41
                    @outlines[parent]['first']=i
42
                end
43
            else
44
                @outlines[i]['parent']=@nb
45
            end
46
            if o['l']<=level and i>0
47
                # Set prev and next pointers
48
                prev=lru[o['l']]
49
                @outlines[prev]['next']=i
50
                @outlines[i]['prev']=prev
51
            end
52
            lru[o['l']]=i
53
            level=o['l']
54
        end
55
        # Outline items
56
        n=@n+1
57
        @outlines.each_index do |i|
58
            o=@outlines[i]
59
            newobj
60
            out('<</Title '+(textstring(o['t'])))
61
            out('/Parent '+(n+o['parent']).to_s+' 0 R')
62
            if o['prev']
63
                out('/Prev '+(n+o['prev']).to_s+' 0 R')
64
            end
65
            if o['next']
66
                out('/Next '+(n+o['next']).to_s+' 0 R')
67
            end
68
            if o['first']
69
                out('/First '+(n+o['first']).to_s+' 0 R')
70
            end
71
            if o['last']
72
                out('/Last '+(n+o['last']).to_s+' 0 R')
73
            end
74
            out(sprintf('/Dest [%d 0 R /XYZ 0 %.2f
75
null]',1+2*o['p'],(@h-o['y'])*@k))
76
            out('/Count 0>>')
77
            out('endobj')
78
        end
79
        # Outline root
80
        newobj
81
        @OutlineRoot=@n
82
        out('<</Type /Outlines /First '+n.to_s+' 0 R')
83
           out('/Last '+(n+lru[0]).to_s+' 0 R>>')
84
           out('endobj')
85
    end
86

    
87
    def putresources
88
        super
89
        putbookmarks
90
    end
91

    
92
    def putcatalog
93
        super
94
        if not @outlines.empty?
95
            out('/Outlines '+@OutlineRoot.to_s+' 0 R')
96
            out('/PageMode /UseOutlines')
97
        end
98
    end
99
end