|
| 1 | +/** |
| 2 | + * jsPDF Annotations PlugIn |
| 3 | + * Copyright (c) 2014 Steven Spungin (TwelveTone LLC) [email protected] |
| 4 | + * |
| 5 | + * Licensed under the MIT License. |
| 6 | + * http://opensource.org/licenses/mit-license |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * There are many types of annotations in a PDF document. Annotations are placed |
| 11 | + * on a page at a particular location. They are not 'attached' to an object. |
| 12 | + * <br /> |
| 13 | + * This plugin current supports <br /> |
| 14 | + * <li> Goto Page (set pageNumber in options) |
| 15 | + * <li> Goto URL (set url in options) |
| 16 | + * |
| 17 | + * <p> |
| 18 | + * Options In PDF spec Not Implemented Yet |
| 19 | + * <li> link border |
| 20 | + * <li> named target |
| 21 | + * <li> page coordinates |
| 22 | + * <li> destination page scaling and layout |
| 23 | + * <li> actions other than URL and GotoPage |
| 24 | + * <li> background / hover actions |
| 25 | + * </p> |
| 26 | + */ |
| 27 | + |
| 28 | +function notEmpty(obj) { |
| 29 | + if (typeof obj != 'undefined') { |
| 30 | + if (obj != '') { |
| 31 | + return true; |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +(function(jsPDFAPI) { |
| 37 | + 'use strict'; |
| 38 | + |
| 39 | + var annotationPlugin = { |
| 40 | + |
| 41 | + /** |
| 42 | + * An array of arrays, indexed by <em>pageNumber</em>. |
| 43 | + */ |
| 44 | + annotations : [], |
| 45 | + |
| 46 | + f2 : function(number) { |
| 47 | + return number.toFixed(2); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + jsPDF.API.annotationPlugin = annotationPlugin; |
| 52 | + |
| 53 | + jsPDF.API.events.push([ |
| 54 | + 'addPage', function(info) { |
| 55 | + this.annotationPlugin.annotations[info.pageNumber] = []; |
| 56 | + } |
| 57 | + ]); |
| 58 | + |
| 59 | + jsPDFAPI.events.push([ |
| 60 | + 'putPage', function(info) { |
| 61 | + var pageAnnos = this.annotationPlugin.annotations[info.pageNumber]; |
| 62 | + |
| 63 | + var found = false; |
| 64 | + for (var a = 0; a < pageAnnos.length; a++) { |
| 65 | + var anno = pageAnnos[a]; |
| 66 | + if (anno.type === 'link') { |
| 67 | + if (notEmpty(anno.options.url) || notEmpty(anno.options.pageNumber)) { |
| 68 | + found = true; |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + if (found == false) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + this.internal.write("/Annots ["); |
| 78 | + var f2 = this.annotationPlugin.f2; |
| 79 | + for (var a = 0; a < pageAnnos.length; a++) { |
| 80 | + var anno = pageAnnos[a]; |
| 81 | + var k = this.internal.scaleFactor; |
| 82 | + var pageHeight = this.internal.pageSize.height; |
| 83 | + 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) + "] "; |
| 84 | + if (anno.options.url) { |
| 85 | + this.internal.write('<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /A <</S /URI /URI (' + anno.options.url + ') >> >>') |
| 86 | + } else if (anno.options.pageNumber) { |
| 87 | + // 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] >>') |
| 89 | + } else { |
| 90 | + // TODO error - should not be here |
| 91 | + } |
| 92 | + } |
| 93 | + this.internal.write("]"); |
| 94 | + } |
| 95 | + ]); |
| 96 | + |
| 97 | + /** |
| 98 | + * valid options |
| 99 | + * <li> pageNumber or url [required] |
| 100 | + */ |
| 101 | + jsPDFAPI.link = function(x,y,w,h,options) { |
| 102 | + 'use strict'; |
| 103 | + this.annotationPlugin.annotations[this.internal.getNumberOfPages()].push({ |
| 104 | + x : x, |
| 105 | + y : y, |
| 106 | + w : w, |
| 107 | + h : h, |
| 108 | + options : options, |
| 109 | + type : 'link' |
| 110 | + }); |
| 111 | + }; |
| 112 | + |
| 113 | + /** |
| 114 | + * Currently only supports single line text. |
| 115 | + */ |
| 116 | + jsPDFAPI.textWithLink = function(text,x,y,options) { |
| 117 | + 'use strict'; |
| 118 | + var width = this.getTextWidth(text); |
| 119 | + var height = this.internal.getLineHeight(); |
| 120 | + this.text(text, x, y); |
| 121 | + //TODO We really need the text baseline height to do this correctly. |
| 122 | + // Or ability to draw text on top, bottom, center, or baseline. |
| 123 | + y += height * .2; |
| 124 | + this.link(x, y - height, width, height, options); |
| 125 | + return this; |
| 126 | + }; |
| 127 | + |
| 128 | + //TODO move into external library |
| 129 | + jsPDFAPI.getTextWidth = function(text) { |
| 130 | + 'use strict'; |
| 131 | + var fontSize = this.internal.getFontSize(); |
| 132 | + var txtWidth = this.getStringUnitWidth(text) * fontSize / this.internal.scaleFactor; |
| 133 | + return txtWidth; |
| 134 | + }; |
| 135 | + |
| 136 | + //TODO move into external library |
| 137 | + jsPDFAPI.getLineHeight = function() { |
| 138 | + return this.internal.getLineHeight(); |
| 139 | + }; |
| 140 | + |
| 141 | + return this; |
| 142 | + |
| 143 | +})(jsPDF.API); |
0 commit comments