13
13
* This plugin current supports <br />
14
14
* <li> Goto Page (set pageNumber in options)
15
15
* <li> Goto URL (set url in options)
16
- *
16
+ * <p>
17
+ * The destination magnification factor can also be specified when goto is a page number or a named destination. (see documentation below)
18
+ * (set magFactor in options). XYZ is the default.
19
+ * </p>
17
20
* <p>
18
21
* Options In PDF spec Not Implemented Yet
19
22
* <li> link border
25
28
* </p>
26
29
*/
27
30
28
- function notEmpty ( obj ) {
29
- if ( typeof obj != 'undefined' ) {
30
- if ( obj != '' ) {
31
- return true ;
32
- }
33
- }
34
- }
31
+ /*
32
+ Destination Magnification Factors
33
+ See PDF 1.3 Page 386 for meanings and options
34
+
35
+ [supported]
36
+ XYZ (options; left top zoom)
37
+ Fit (no options)
38
+ FitH (options: top)
39
+ FitV (options: left)
40
+
41
+ [not supported]
42
+ FitR
43
+ FitB
44
+ FitBH
45
+ FitBV
46
+ */
35
47
36
48
( function ( jsPDFAPI ) {
37
49
'use strict' ;
@@ -45,6 +57,14 @@ function notEmpty(obj) {
45
57
46
58
f2 : function ( number ) {
47
59
return number . toFixed ( 2 ) ;
60
+ } ,
61
+
62
+ notEmpty : function ( obj ) {
63
+ if ( typeof obj != 'undefined' ) {
64
+ if ( obj != '' ) {
65
+ return true ;
66
+ }
67
+ }
48
68
}
49
69
} ;
50
70
@@ -64,7 +84,7 @@ function notEmpty(obj) {
64
84
for ( var a = 0 ; a < pageAnnos . length ; a ++ ) {
65
85
var anno = pageAnnos [ a ] ;
66
86
if ( anno . type === 'link' ) {
67
- if ( notEmpty ( anno . options . url ) || notEmpty ( anno . options . pageNumber ) ) {
87
+ if ( annotationPlugin . notEmpty ( anno . options . url ) || annotationPlugin . notEmpty ( anno . options . pageNumber ) ) {
68
88
found = true ;
69
89
break ;
70
90
}
@@ -78,17 +98,50 @@ function notEmpty(obj) {
78
98
var f2 = this . annotationPlugin . f2 ;
79
99
for ( var a = 0 ; a < pageAnnos . length ; a ++ ) {
80
100
var anno = pageAnnos [ a ] ;
101
+
81
102
var k = this . internal . scaleFactor ;
82
103
var pageHeight = this . internal . pageSize . height ;
104
+ //var pageHeight = this.internal.pageSize.height * this.internal.scaleFactor;
83
105
var rect = "/Rect [" + f2 ( anno . x * k ) + " " + f2 ( ( pageHeight - anno . y ) * k ) + " " + f2 ( anno . x + anno . w * k ) + " " + f2 ( pageHeight - ( anno . y + anno . h ) * k ) + "] " ;
106
+
107
+ var line = '' ;
84
108
if ( anno . options . url ) {
85
- this . internal . write ( '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /A <</S /URI /URI (' + anno . options . url + ') >> >>' )
109
+ line = '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /A <</S /URI /URI (' + anno . options . url + ') >>' ;
86
110
} else if ( anno . options . pageNumber ) {
87
111
// first page is 0
88
- this . internal . write ( '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /Dest [' + ( anno . options . pageNumber - 1 ) + ' /XYZ 0 ' + pageHeight + ' 0] >>' )
112
+ var pageObjId = anno . options . pageNumber * 2 + 1 ;
113
+ line = '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /Dest [' + pageObjId + " 0 R" ;
114
+ anno . options . magFactor = anno . options . magFactor || "XYZ" ;
115
+ switch ( anno . options . magFactor ) {
116
+ case 'Fit' :
117
+ line += ' /Fit]' ;
118
+ break ;
119
+ case 'FitH' :
120
+ anno . options . top = anno . options . top || f2 ( pageHeight * k ) ;
121
+ line += ' /FitH ' + anno . options . top + ']' ;
122
+ break ;
123
+ case 'FitV' :
124
+ anno . options . left = anno . options . left || 0 ;
125
+ line += ' /FitV ' + anno . options . left + ']' ;
126
+ break ;
127
+ case 'XYZ' :
128
+ default :
129
+ anno . options . top = anno . options . top || f2 ( pageHeight * k ) ;
130
+ anno . options . left = anno . options . left || 0 ;
131
+ // 0 or null zoom will not change zoom factor
132
+ if ( typeof anno . options . zoom === 'undefined' ) {
133
+ anno . options . zoom = 0 ;
134
+ }
135
+ line += ' /XYZ ' + anno . options . left + ' ' + anno . options . top + ' ' + anno . options . zoom + ']' ;
136
+ break ;
137
+ }
89
138
} else {
90
139
// TODO error - should not be here
91
140
}
141
+ if ( line != '' ) {
142
+ line += " >>" ;
143
+ this . internal . write ( line ) ;
144
+ }
92
145
}
93
146
this . internal . write ( "]" ) ;
94
147
}
@@ -112,6 +165,7 @@ function notEmpty(obj) {
112
165
113
166
/**
114
167
* Currently only supports single line text.
168
+ * Returns the width of the text/link
115
169
*/
116
170
jsPDFAPI . textWithLink = function ( text , x , y , options ) {
117
171
'use strict' ;
@@ -122,7 +176,7 @@ function notEmpty(obj) {
122
176
// Or ability to draw text on top, bottom, center, or baseline.
123
177
y += height * .2 ;
124
178
this . link ( x , y - height , width , height , options ) ;
125
- return this ;
179
+ return width ;
126
180
} ;
127
181
128
182
//TODO move into external library
0 commit comments