From e2ce1501ee964876528daa5d53e95dd50a36cf03 Mon Sep 17 00:00:00 2001
From: Steven Spungin
Date: Mon, 24 Nov 2014 10:01:54 -0500
Subject: [PATCH] Added support for zoom factor in links Moved stray method
into plugin scope Use page object reference instead of pageNumber for
destination. (wider compatibility)
---
jspdf.plugin.annotations.js | 78 +++++++++++++++++++++++++++++++------
test/test_annotation.html | 24 ++++++++++++
2 files changed, 90 insertions(+), 12 deletions(-)
diff --git a/jspdf.plugin.annotations.js b/jspdf.plugin.annotations.js
index 603c84f04..7a681c7b4 100644
--- a/jspdf.plugin.annotations.js
+++ b/jspdf.plugin.annotations.js
@@ -13,7 +13,10 @@
* This plugin current supports
* Goto Page (set pageNumber in options)
* Goto URL (set url in options)
- *
+ *
+ * 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.
+ *
*
* Options In PDF spec Not Implemented Yet
*
link border
@@ -25,13 +28,22 @@
*
*/
-function notEmpty(obj) {
- if (typeof obj != 'undefined') {
- if (obj != '') {
- return true;
- }
- }
-}
+/*
+ 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';
@@ -45,6 +57,14 @@ function notEmpty(obj) {
f2 : function(number) {
return number.toFixed(2);
+ },
+
+ notEmpty : function(obj) {
+ if (typeof obj != 'undefined') {
+ if (obj != '') {
+ return true;
+ }
+ }
}
};
@@ -64,7 +84,7 @@ function notEmpty(obj) {
for (var a = 0; a < pageAnnos.length; a++) {
var anno = pageAnnos[a];
if (anno.type === 'link') {
- if (notEmpty(anno.options.url) || notEmpty(anno.options.pageNumber)) {
+ if (annotationPlugin.notEmpty(anno.options.url) || annotationPlugin.notEmpty(anno.options.pageNumber)) {
found = true;
break;
}
@@ -78,17 +98,50 @@ function notEmpty(obj) {
var f2 = this.annotationPlugin.f2;
for (var a = 0; a < pageAnnos.length; a++) {
var anno = pageAnnos[a];
+
var k = this.internal.scaleFactor;
var pageHeight = this.internal.pageSize.height;
+ //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) {
- this.internal.write('<> >>')
+ line = '<>';
} else if (anno.options.pageNumber) {
// first page is 0
- this.internal.write('<>')
+ var pageObjId = anno.options.pageNumber * 2 + 1;
+ line = '<>";
+ this.internal.write(line);
+ }
}
this.internal.write("]");
}
@@ -112,6 +165,7 @@ function notEmpty(obj) {
/**
* Currently only supports single line text.
+ * Returns the width of the text/link
*/
jsPDFAPI.textWithLink = function(text,x,y,options) {
'use strict';
@@ -122,7 +176,7 @@ function notEmpty(obj) {
// Or ability to draw text on top, bottom, center, or baseline.
y += height * .2;
this.link(x, y - height, width, height, options);
- return this;
+ return width;
};
//TODO move into external library
diff --git a/test/test_annotation.html b/test/test_annotation.html
index cf2d3c876..41ac97e32 100644
--- a/test/test_annotation.html
+++ b/test/test_annotation.html
@@ -31,6 +31,7 @@
// Create pages with a table of contents.
// TOC links to each page
// Each page links back to TOC and to an external URL
+ // Supported magnification Options are included.
var y = 20;
var text = 'Table of Contents';
pdf.text(text, 20, y);
@@ -40,8 +41,24 @@
text = "Page " + i;
pdf.textWithLink(text, 20, y, {pageNumber:i});
y += pdf.getLineHeight();
+
+ var x = 20;
+ var width = pdf.textWithLink(" [100%]", x, y, {pageNumber:i, magFactor:'XYZ', zoom:1});
+ x += width;
+ var width = pdf.textWithLink(" [200%]", x, y, {pageNumber:i, magFactor:'XYZ', zoom:2});
+ x += width;
+ var width = pdf.textWithLink(" [50%]", x, y, {pageNumber:i, magFactor:'XYZ', zoom:.5});
+ x += width;
+ var width = pdf.textWithLink(" [Fit]", x, y, {pageNumber:i, magFactor:'Fit'});
+ x += width;
+ var width = pdf.textWithLink(" [FitH]", x, y, {pageNumber:i, magFactor:'FitH'});
+ x += width;
+ var width = pdf.textWithLink(" [FitV]", x, y, {pageNumber:i, magFactor:'FitV'});
+
+ y += pdf.getLineHeight();
}
+ // Create Test Pages
for (var i=2; i<10; i++){
pdf.addPage();
y = 20;
@@ -99,6 +116,13 @@
+
+ Chrome default PDF reader currently does not support magFactor links, although links still work after manualy changing magFactor.
+
+ Firefox has a bug displaying annotations after the magFactor changes, but links do work.
+
+ To test magFactor links [...] without bugs, use Adobe Reader or compatible application.
+