123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>plugins/annotations.js - Documentation</title>
- <script src="scripts/prettify/prettify.js"></script>
- <script src="scripts/prettify/lang-css.js"></script>
- <!--[if lt IE 9]>
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
- <![endif]-->
- <link type="text/css" rel="stylesheet" href="styles/prettify.css">
- <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
- </head>
- <body>
- <input type="checkbox" id="nav-trigger" class="nav-trigger" />
- <label for="nav-trigger" class="navicon-button x">
- <div class="navicon"></div>
- </label>
- <label for="nav-trigger" class="overlay"></label>
- <nav>
- <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="jsPDF.html">jsPDF</a></li></ul><h3>Global</h3><ul><li><a href="global.html#addFont">addFont</a></li><li><a href="global.html#addHTML">addHTML</a></li><li><a href="global.html#addMetadata">addMetadata</a></li><li><a href="global.html#addPage">addPage</a></li><li><a href="global.html#autoPrint">autoPrint</a></li><li><a href="global.html#CapJoinStyles">CapJoinStyles</a></li><li><a href="global.html#circle">circle</a></li><li><a href="global.html#ellipse">ellipse</a></li><li><a href="global.html#getFontList">getFontList</a></li><li><a href="global.html#lines">lines</a></li><li><a href="global.html#lstext">lstext</a></li><li><a href="global.html#output">output</a></li><li><a href="global.html#rect">rect</a></li><li><a href="global.html#roundedRect">roundedRect</a></li><li><a href="global.html#save">save</a></li><li><a href="global.html#setDisplayMode">setDisplayMode</a></li><li><a href="global.html#setDrawColor">setDrawColor</a></li><li><a href="global.html#setFillColor">setFillColor</a></li><li><a href="global.html#setFont">setFont</a></li><li><a href="global.html#setFontSize">setFontSize</a></li><li><a href="global.html#setFontStyle">setFontStyle</a></li><li><a href="global.html#setLineCap">setLineCap</a></li><li><a href="global.html#setLineJoin">setLineJoin</a></li><li><a href="global.html#setLineWidth">setLineWidth</a></li><li><a href="global.html#setPage">setPage</a></li><li><a href="global.html#setProperties">setProperties</a></li><li><a href="global.html#setTextColor">setTextColor</a></li><li><a href="global.html#text">text</a></li><li><a href="global.html#triangle">triangle</a></li></ul>
- </nav>
- <div id="main">
-
- <h1 class="page-title">plugins/annotations.js</h1>
-
-
-
- <section>
- <article>
- <pre class="prettyprint source linenums"><code>/**
- * jsPDF Annotations PlugIn
- * Copyright (c) 2014 Steven Spungin (TwelveTone LLC) steven@twelvetone.tv
- *
- * Licensed under the MIT License.
- * http://opensource.org/licenses/mit-license
- */
- /**
- * There are many types of annotations in a PDF document. Annotations are placed
- * on a page at a particular location. They are not 'attached' to an object.
- * <br />
- * This plugin current supports <br />
- * <li> Goto Page (set pageNumber and top in options)
- * <li> Goto Name (set name and top in options)
- * <li> Goto URL (set url in options)
- * <p>
- * The destination magnification factor can also be specified when goto is a page number or a named destination. (see documentation below)
- * (set magFactor in options). XYZ is the default.
- * </p>
- * <p>
- * Links, Text, Popup, and FreeText are supported.
- * </p>
- * <p>
- * Options In PDF spec Not Implemented Yet
- * <li> link border
- * <li> named target
- * <li> page coordinates
- * <li> destination page scaling and layout
- * <li> actions other than URL and GotoPage
- * <li> background / hover actions
- * </p>
- */
- /*
- Destination Magnification Factors
- See PDF 1.3 Page 386 for meanings and options
- [supported]
- XYZ (options; left top zoom)
- Fit (no options)
- FitH (options: top)
- FitV (options: left)
- [not supported]
- FitR
- FitB
- FitBH
- FitBV
- */
- (function(jsPDFAPI) {
- 'use strict';
- var annotationPlugin = {
- /**
- * An array of arrays, indexed by <em>pageNumber</em>.
- */
- annotations : [],
- f2 : function(number) {
- return number.toFixed(2);
- },
- notEmpty : function(obj) {
- if (typeof obj != 'undefined') {
- if (obj != '') {
- return true;
- }
- }
- }
- };
- jsPDF.API.annotationPlugin = annotationPlugin;
- jsPDF.API.events.push([ 'addPage', function(info) {
- this.annotationPlugin.annotations[info.pageNumber] = [];
- } ]);
- jsPDFAPI.events.push([ 'putPage', function(info) {
- //TODO store annotations in pageContext so reorder/remove will not affect them.
- var pageAnnos = this.annotationPlugin.annotations[info.pageNumber];
- var found = false;
- for (var a = 0; a < pageAnnos.length && !found; a++) {
- var anno = pageAnnos[a];
- switch (anno.type) {
- case 'link':
- if (annotationPlugin.notEmpty(anno.options.url) || annotationPlugin.notEmpty(anno.options.pageNumber)) {
- found = true;
- break;
- }
- case 'reference':
- case 'text':
- case 'freetext':
- found = true;
- break;
- }
- }
- if (found == false) {
- return;
- }
- this.internal.write("/Annots [");
- var f2 = this.annotationPlugin.f2;
- var k = this.internal.scaleFactor;
- var pageHeight = this.internal.pageSize.height;
- var pageInfo = this.internal.getPageInfo(info.pageNumber);
- for (var a = 0; a < pageAnnos.length; a++) {
- var anno = pageAnnos[a];
- switch (anno.type) {
- case 'reference':
- // References to Widget Anotations (for AcroForm Fields)
- this.internal.write(' ' + anno.object.objId + ' 0 R ');
- break;
- case 'text':
- // Create a an object for both the text and the popup
- var objText = this.internal.newAdditionalObject();
- var objPopup = this.internal.newAdditionalObject();
- var title = anno.title || 'Note';
- 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) + "] ";
- line = '<</Type /Annot /Subtype /' + 'Text' + ' ' + rect + '/Contents (' + anno.contents + ')';
- line += ' /Popup ' + objPopup.objId + " 0 R";
- line += ' /P ' + pageInfo.objId + " 0 R";
- line += ' /T (' + title + ') >>';
- objText.content = line;
- var parent = objText.objId + ' 0 R';
- var popoff = 30;
- 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) + "] ";
- //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) + "] ";
- line = '<</Type /Annot /Subtype /' + 'Popup' + ' ' + rect + ' /Parent ' + parent;
- if (anno.open) {
- line += ' /Open true';
- }
- line += ' >>';
- objPopup.content = line;
- this.internal.write(objText.objId, '0 R', objPopup.objId, '0 R');
- break;
- case 'freetext':
- 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) + "] ";
- var color = anno.color || '#000000';
- line = '<</Type /Annot /Subtype /' + 'FreeText' + ' ' + rect + '/Contents (' + anno.contents + ')';
- line += ' /DS(font: Helvetica,sans-serif 12.0pt; text-align:left; color:#' + color + ')';
- line += ' /Border [0 0 0]';
- line += ' >>';
- this.internal.write(line);
- break;
- case 'link':
- if (anno.options.name) {
- var loc = this.annotations._nameMap[anno.options.name];
- anno.options.pageNumber = loc.page;
- anno.options.top = loc.y;
- } else {
- if (!anno.options.top) {
- anno.options.top = 0;
- }
- }
- //var pageHeight = this.internal.pageSize.height * this.internal.scaleFactor;
- 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) + "] ";
- var line = '';
- if (anno.options.url) {
- line = '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /A <</S /URI /URI (' + anno.options.url + ') >>';
- } else if (anno.options.pageNumber) {
- // first page is 0
- var info = this.internal.getPageInfo(anno.options.pageNumber);
- line = '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /Dest [' + info.objId + " 0 R";
- anno.options.magFactor = anno.options.magFactor || "XYZ";
- switch (anno.options.magFactor) {
- case 'Fit':
- line += ' /Fit]';
- break;
- case 'FitH':
- //anno.options.top = anno.options.top || f2(pageHeight * k);
- line += ' /FitH ' + anno.options.top + ']';
- break;
- case 'FitV':
- anno.options.left = anno.options.left || 0;
- line += ' /FitV ' + anno.options.left + ']';
- break;
- case 'XYZ':
- default:
- var top = f2((pageHeight - anno.options.top) * k);// || f2(pageHeight * k);
- anno.options.left = anno.options.left || 0;
- // 0 or null zoom will not change zoom factor
- if (typeof anno.options.zoom === 'undefined') {
- anno.options.zoom = 0;
- }
- line += ' /XYZ ' + anno.options.left + ' ' + top + ' ' + anno.options.zoom + ']';
- break;
- }
- } else {
- // TODO error - should not be here
- }
- if (line != '') {
- line += " >>";
- this.internal.write(line);
- }
- break;
- }
- }
- this.internal.write("]");
- } ]);
- jsPDFAPI.createAnnotation = function(options) {
- switch (options.type) {
- case 'link':
- this.link(options.bounds.x, options.bounds.y, options.bounds.w, options.bounds.h, options);
- break;
- case 'text':
- case 'freetext':
- this.annotationPlugin.annotations[this.internal.getCurrentPageInfo().pageNumber].push(options);
- break;
- }
- }
- /**
- * valid options
- * <li> pageNumber or url [required]
- * <p>If pageNumber is specified, top and zoom may also be specified</p>
- */
- jsPDFAPI.link = function(x,y,w,h,options) {
- 'use strict';
- this.annotationPlugin.annotations[this.internal.getCurrentPageInfo().pageNumber].push({
- x : x,
- y : y,
- w : w,
- h : h,
- options : options,
- type : 'link'
- });
- };
- /**
- * valid options
- * <li> pageNumber or url [required]
- * <p>If pageNumber is specified, top and zoom may also be specified</p>
- */
- jsPDFAPI.link = function(x,y,w,h,options) {
- 'use strict';
- this.annotationPlugin.annotations[this.internal.getCurrentPageInfo().pageNumber].push({
- x : x,
- y : y,
- w : w,
- h : h,
- options : options,
- type : 'link'
- });
- };
- /**
- * Currently only supports single line text.
- * Returns the width of the text/link
- */
- jsPDFAPI.textWithLink = function(text,x,y,options) {
- 'use strict';
- var width = this.getTextWidth(text);
- var height = this.internal.getLineHeight();
- this.text(text, x, y);
- //TODO We really need the text baseline height to do this correctly.
- // Or ability to draw text on top, bottom, center, or baseline.
- y += height * .2;
- this.link(x, y - height, width, height, options);
- return width;
- };
- //TODO move into external library
- jsPDFAPI.getTextWidth = function(text) {
- 'use strict';
- var fontSize = this.internal.getFontSize();
- var txtWidth = this.getStringUnitWidth(text) * fontSize / this.internal.scaleFactor;
- return txtWidth;
- };
- //TODO move into external library
- jsPDFAPI.getLineHeight = function() {
- return this.internal.getLineHeight();
- };
- return this;
- })(jsPDF.API);
- </code></pre>
- </article>
- </section>
- </div>
- <br class="clear">
- <footer>
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.2</a> on Sun Oct 09 2016 11:08:27 GMT+0100 (BST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
- </footer>
- <script>prettyPrint();</script>
- <script src="scripts/linenumber.js"></script>
- </body>
- </html>
|