|
74 | 74 |
|
75 | 75 | jsPDF.API.annotationPlugin = annotationPlugin; |
76 | 76 |
|
77 | | - jsPDF.API.events.push([ |
78 | | - 'addPage', function(info) { |
79 | | - this.annotationPlugin.annotations[info.pageNumber] = []; |
80 | | - } |
81 | | - ]); |
| 77 | + jsPDF.API.events.push([ 'addPage', function(info) { |
| 78 | + this.annotationPlugin.annotations[info.pageNumber] = []; |
| 79 | + } ]); |
82 | 80 |
|
83 | | - jsPDFAPI.events.push([ |
84 | | - 'putPage', function(info) { |
85 | | - //TODO store annotations in pageContext so reorder/remove will not affect them. |
86 | | - var pageAnnos = this.annotationPlugin.annotations[info.pageNumber]; |
| 81 | + jsPDFAPI.events.push([ 'putPage', function(info) { |
| 82 | + //TODO store annotations in pageContext so reorder/remove will not affect them. |
| 83 | + var pageAnnos = this.annotationPlugin.annotations[info.pageNumber]; |
87 | 84 |
|
88 | | - var found = false; |
89 | | - for (var a = 0; a < pageAnnos.length && !found; a++) { |
90 | | - var anno = pageAnnos[a]; |
91 | | - switch (anno.type) { |
92 | | - case 'link': |
93 | | - if (annotationPlugin.notEmpty(anno.options.url) || annotationPlugin.notEmpty(anno.options.pageNumber)) { |
94 | | - found = true; |
95 | | - break; |
96 | | - } |
97 | | - case 'text': |
98 | | - case 'freetext': |
99 | | - found = true; |
100 | | - break; |
101 | | - } |
102 | | - } |
103 | | - if (found == false) { |
104 | | - return; |
| 85 | + var found = false; |
| 86 | + for (var a = 0; a < pageAnnos.length && !found; a++) { |
| 87 | + var anno = pageAnnos[a]; |
| 88 | + switch (anno.type) { |
| 89 | + case 'link': |
| 90 | + if (annotationPlugin.notEmpty(anno.options.url) || annotationPlugin.notEmpty(anno.options.pageNumber)) { |
| 91 | + found = true; |
| 92 | + break; |
105 | 93 | } |
| 94 | + case 'text': |
| 95 | + case 'freetext': |
| 96 | + found = true; |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + if (found == false) { |
| 101 | + return; |
| 102 | + } |
106 | 103 |
|
107 | | - this.internal.write("/Annots ["); |
108 | | - var f2 = this.annotationPlugin.f2; |
109 | | - var k = this.internal.scaleFactor; |
110 | | - var pageHeight = this.internal.pageSize.height; |
111 | | - var pageInfo = this.internal.getPageInfo(info.pageNumber); |
112 | | - for (var a = 0; a < pageAnnos.length; a++) { |
113 | | - var anno = pageAnnos[a]; |
| 104 | + this.internal.write("/Annots ["); |
| 105 | + var f2 = this.annotationPlugin.f2; |
| 106 | + var k = this.internal.scaleFactor; |
| 107 | + var pageHeight = this.internal.pageSize.height; |
| 108 | + var pageInfo = this.internal.getPageInfo(info.pageNumber); |
| 109 | + for (var a = 0; a < pageAnnos.length; a++) { |
| 110 | + var anno = pageAnnos[a]; |
114 | 111 |
|
115 | | - switch (anno.type) { |
116 | | - case 'text': |
117 | | - // Create a an object for both the text and the popup |
118 | | - var objText = this.internal.newAdditionalObject(); |
119 | | - var objPopup = this.internal.newAdditionalObject(); |
| 112 | + switch (anno.type) { |
| 113 | + case 'text': |
| 114 | + // Create a an object for both the text and the popup |
| 115 | + var objText = this.internal.newAdditionalObject(); |
| 116 | + var objPopup = this.internal.newAdditionalObject(); |
120 | 117 |
|
121 | | - var title = anno.title || 'Note'; |
122 | | - var rect = "/Rect [" + f2(anno.bounds.x * k) + " " + f2(pageHeight - (anno.bounds.y + anno.bounds.h) * k) + " " + f2((anno.bounds.x + anno.bounds.w) * k) + " " + f2((pageHeight - anno.bounds.y) * k) + "] "; |
123 | | - line = '<</Type /Annot /Subtype /' + 'Text' + ' ' + rect + '/Contents (' + anno.contents + ')'; |
124 | | - line += ' /Popup ' + objPopup.objId + " 0 R"; |
125 | | - line += ' /P ' + pageInfo.objId + " 0 R"; |
126 | | - line += ' /T (' + title + ') >>'; |
127 | | - objText.content = line; |
| 118 | + var title = anno.title || 'Note'; |
| 119 | + var rect = "/Rect [" + f2(anno.bounds.x * k) + " " + f2(pageHeight - (anno.bounds.y + anno.bounds.h) * k) + " " + f2((anno.bounds.x + anno.bounds.w) * k) + " " + f2((pageHeight - anno.bounds.y) * k) + "] "; |
| 120 | + line = '<</Type /Annot /Subtype /' + 'Text' + ' ' + rect + '/Contents (' + anno.contents + ')'; |
| 121 | + line += ' /Popup ' + objPopup.objId + " 0 R"; |
| 122 | + line += ' /P ' + pageInfo.objId + " 0 R"; |
| 123 | + line += ' /T (' + title + ') >>'; |
| 124 | + objText.content = line; |
128 | 125 |
|
129 | | - var parent = objText.objId + ' 0 R'; |
130 | | - var popoff = 30; |
131 | | - var rect = "/Rect [" + f2((anno.bounds.x + popoff) * k) + " " + f2(pageHeight - (anno.bounds.y + anno.bounds.h) * k) + " " + f2((anno.bounds.x + anno.bounds.w + popoff) * k) + " " + f2((pageHeight - anno.bounds.y) * k) + "] "; |
132 | | - //var rect2 = "/Rect [" + f2(anno.bounds.x * k) + " " + f2((pageHeight - anno.bounds.y) * k) + " " + f2(anno.bounds.x + anno.bounds.w * k) + " " + f2(pageHeight - (anno.bounds.y + anno.bounds.h) * k) + "] "; |
133 | | - line = '<</Type /Annot /Subtype /' + 'Popup' + ' ' + rect + ' /Parent ' + parent; |
134 | | - if (anno.open) { |
135 | | - line += ' /Open true'; |
136 | | - } |
137 | | - line += ' >>'; |
138 | | - objPopup.content = line; |
| 126 | + var parent = objText.objId + ' 0 R'; |
| 127 | + var popoff = 30; |
| 128 | + var rect = "/Rect [" + f2((anno.bounds.x + popoff) * k) + " " + f2(pageHeight - (anno.bounds.y + anno.bounds.h) * k) + " " + f2((anno.bounds.x + anno.bounds.w + popoff) * k) + " " + f2((pageHeight - anno.bounds.y) * k) + "] "; |
| 129 | + //var rect2 = "/Rect [" + f2(anno.bounds.x * k) + " " + f2((pageHeight - anno.bounds.y) * k) + " " + f2(anno.bounds.x + anno.bounds.w * k) + " " + f2(pageHeight - (anno.bounds.y + anno.bounds.h) * k) + "] "; |
| 130 | + line = '<</Type /Annot /Subtype /' + 'Popup' + ' ' + rect + ' /Parent ' + parent; |
| 131 | + if (anno.open) { |
| 132 | + line += ' /Open true'; |
| 133 | + } |
| 134 | + line += ' >>'; |
| 135 | + objPopup.content = line; |
139 | 136 |
|
140 | | - this.internal.write(objText.objId, '0 R', objPopup.objId, '0 R'); |
| 137 | + this.internal.write(objText.objId, '0 R', objPopup.objId, '0 R'); |
141 | 138 |
|
142 | | - break; |
143 | | - case 'freetext': |
144 | | - var rect = "/Rect [" + f2(anno.bounds.x * k) + " " + f2((pageHeight - anno.bounds.y) * k) + " " + f2(anno.bounds.x + anno.bounds.w * k) + " " + f2(pageHeight - (anno.bounds.y + anno.bounds.h) * k) + "] "; |
145 | | - var color = anno.color || '#000000'; |
146 | | - line = '<</Type /Annot /Subtype /' + 'FreeText' + ' ' + rect + '/Contents (' + anno.contents + ')'; |
147 | | - line += ' /DS(font: Helvetica,sans-serif 12.0pt; text-align:left; color:#' + color + ')'; |
148 | | - line += ' /Border [0 0 0]'; |
149 | | - line += ' >>'; |
150 | | - this.internal.write(line); |
151 | | - break; |
152 | | - case 'link': |
153 | | - if (anno.options.name) { |
154 | | - var loc = this.annotations._nameMap[anno.options.name]; |
155 | | - anno.options.pageNumber = loc.page; |
156 | | - anno.options.top = loc.y; |
157 | | - } |
| 139 | + break; |
| 140 | + case 'freetext': |
| 141 | + var rect = "/Rect [" + f2(anno.bounds.x * k) + " " + f2((pageHeight - anno.bounds.y) * k) + " " + f2(anno.bounds.x + anno.bounds.w * k) + " " + f2(pageHeight - (anno.bounds.y + anno.bounds.h) * k) + "] "; |
| 142 | + var color = anno.color || '#000000'; |
| 143 | + line = '<</Type /Annot /Subtype /' + 'FreeText' + ' ' + rect + '/Contents (' + anno.contents + ')'; |
| 144 | + line += ' /DS(font: Helvetica,sans-serif 12.0pt; text-align:left; color:#' + color + ')'; |
| 145 | + line += ' /Border [0 0 0]'; |
| 146 | + line += ' >>'; |
| 147 | + this.internal.write(line); |
| 148 | + break; |
| 149 | + case 'link': |
| 150 | + if (anno.options.name) { |
| 151 | + var loc = this.annotations._nameMap[anno.options.name]; |
| 152 | + anno.options.pageNumber = loc.page; |
| 153 | + anno.options.top = loc.y; |
| 154 | + } else { |
| 155 | + if (!anno.options.top) { |
| 156 | + anno.options.top = 0; |
| 157 | + } |
| 158 | + } |
158 | 159 |
|
159 | | - //var pageHeight = this.internal.pageSize.height * this.internal.scaleFactor; |
160 | | - 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) + "] "; |
| 160 | + //var pageHeight = this.internal.pageSize.height * this.internal.scaleFactor; |
| 161 | + 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) + "] "; |
161 | 162 |
|
162 | | - var line = ''; |
163 | | - if (anno.options.url) { |
164 | | - line = '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /A <</S /URI /URI (' + anno.options.url + ') >>'; |
165 | | - } else if (anno.options.pageNumber) { |
166 | | - // first page is 0 |
167 | | - var info = this.internal.getPageInfo(anno.options.pageNumber); |
168 | | - line = '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /Dest [' + info.objId + " 0 R"; |
169 | | - anno.options.magFactor = anno.options.magFactor || "XYZ"; |
170 | | - switch (anno.options.magFactor) { |
171 | | - case 'Fit': |
172 | | - line += ' /Fit]'; |
173 | | - break; |
174 | | - case 'FitH': |
175 | | - anno.options.top = anno.options.top || f2(pageHeight * k); |
176 | | - line += ' /FitH ' + anno.options.top + ']'; |
177 | | - break; |
178 | | - case 'FitV': |
179 | | - anno.options.left = anno.options.left || 0; |
180 | | - line += ' /FitV ' + anno.options.left + ']'; |
181 | | - break; |
182 | | - case 'XYZ': |
183 | | - default: |
184 | | - var top = f2((pageHeight - anno.options.top) * k) || f2(pageHeight * k); |
185 | | - anno.options.left = anno.options.left || 0; |
186 | | - // 0 or null zoom will not change zoom factor |
187 | | - if (typeof anno.options.zoom === 'undefined') { |
188 | | - anno.options.zoom = 0; |
189 | | - } |
190 | | - line += ' /XYZ ' + anno.options.left + ' ' + top + ' ' + anno.options.zoom + ']'; |
191 | | - break; |
192 | | - } |
193 | | - } else { |
194 | | - // TODO error - should not be here |
195 | | - } |
196 | | - if (line != '') { |
197 | | - line += " >>"; |
198 | | - this.internal.write(line); |
| 163 | + var line = ''; |
| 164 | + if (anno.options.url) { |
| 165 | + line = '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /A <</S /URI /URI (' + anno.options.url + ') >>'; |
| 166 | + } else if (anno.options.pageNumber) { |
| 167 | + // first page is 0 |
| 168 | + var info = this.internal.getPageInfo(anno.options.pageNumber); |
| 169 | + line = '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /Dest [' + info.objId + " 0 R"; |
| 170 | + anno.options.magFactor = anno.options.magFactor || "XYZ"; |
| 171 | + switch (anno.options.magFactor) { |
| 172 | + case 'Fit': |
| 173 | + line += ' /Fit]'; |
| 174 | + break; |
| 175 | + case 'FitH': |
| 176 | + //anno.options.top = anno.options.top || f2(pageHeight * k); |
| 177 | + line += ' /FitH ' + anno.options.top + ']'; |
| 178 | + break; |
| 179 | + case 'FitV': |
| 180 | + anno.options.left = anno.options.left || 0; |
| 181 | + line += ' /FitV ' + anno.options.left + ']'; |
| 182 | + break; |
| 183 | + case 'XYZ': |
| 184 | + default: |
| 185 | + var top = f2((pageHeight - anno.options.top) * k);// || f2(pageHeight * k); |
| 186 | + anno.options.left = anno.options.left || 0; |
| 187 | + // 0 or null zoom will not change zoom factor |
| 188 | + if (typeof anno.options.zoom === 'undefined') { |
| 189 | + anno.options.zoom = 0; |
199 | 190 | } |
| 191 | + line += ' /XYZ ' + anno.options.left + ' ' + top + ' ' + anno.options.zoom + ']'; |
200 | 192 | break; |
201 | 193 | } |
202 | | - |
| 194 | + } else { |
| 195 | + // TODO error - should not be here |
| 196 | + } |
| 197 | + if (line != '') { |
| 198 | + line += " >>"; |
| 199 | + this.internal.write(line); |
203 | 200 | } |
204 | | - this.internal.write("]"); |
| 201 | + break; |
205 | 202 | } |
206 | | - ]); |
| 203 | + |
| 204 | + } |
| 205 | + this.internal.write("]"); |
| 206 | + } ]); |
207 | 207 |
|
208 | 208 | jsPDFAPI.createAnnotation = function(options) { |
209 | 209 | switch (options.type) { |
|
0 commit comments