Skip to content

Commit 27385bd

Browse files
author
Alexander Weidt
committed
Small Fixes
1 parent 22e9d16 commit 27385bd

3 files changed

Lines changed: 98 additions & 40 deletions

File tree

examples/js/AcroForm.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,75 @@
1-
// Create instance of JSPDF
2-
var doc = new jsPDF();
1+
var doc = new jsPDF();//{unit: 'px'}
2+
3+
doc.setFontSize(12);
4+
doc.text(10, 105, 'ComboBox:');
35

46
var d = new ComboBox();
57
d.T = "ChoiceField1";
68
d.TI = 1;
7-
d.Rect = [50, 750, 200, 50];
9+
d.Rect = [50, 100, 30, 10];
810
d.Opt = "[(a)(b)(c)]";
911
d.V = '(b)';
1012
d.DV = '(b)';
1113
doc.addField(d);
1214

13-
15+
doc.text(10, 115, 'ListBox:');
1416
var d2 = new ListBox();
1517
d2.edit = false;
1618
d2.T = "ChoiceField2";
1719
d2.TI = 2;
18-
d2.Rect = [50, 700, 200, 50];
20+
d2.Rect = [50, 110, 30, 10];
1921
d2.Opt = "[(c)(a)(d)(f)(b)(s)]";
2022
d2.V = '(s)';
2123
d2.BG = [0, 1, 1];
2224
doc.addField(d2);
2325

26+
doc.text(10, 125, 'CheckBox:');
2427
var checkBox = new CheckBox();
2528
checkBox.T = "CheckBox1";
26-
checkBox.Rect = [50, 650, 50, 50];
29+
checkBox.Rect = [50, 120, 30, 10];
2730
doc.addField(checkBox);
2831

32+
doc.text(10, 135, 'PushButton:');
2933
var pushButton = new PushButton();
3034
pushButton.T = "PushButton1";
31-
pushButton.Rect = [50, 600, 200, 50];
35+
pushButton.Rect = [50, 130, 30, 10];
3236
pushButton.BG = [1, 0, 0];
3337
doc.addField(pushButton);
3438

39+
doc.text(10, 145, 'TextField:');
3540
var textField = new TextField();
36-
textField.Rect = [50, 550, 200, 50];
41+
textField.Rect = [50, 140, 30, 10];
3742
textField.multiline = true;
38-
textField.V = "The quick brown fox ate the lazy mouse";//
43+
//textField.DV = "The quick brown fox ate the lazy mouse";
44+
textField.V = "The quick brown fox ate the lazy mouse The quick brown fox ate the lazy mouse The quick brown fox ate the lazy mouse";//
3945
textField.T = "TestTextBox";
40-
textField.Q = 2; // Text-Alignment
46+
//textField.Q = 2; // Text-Alignment
47+
//textField.F = 4;
48+
//textField.hasAppearanceStream = true;
4149
doc.addField(textField);
4250

51+
doc.text(10, 155, 'Password:');
4352
var passwordField = new PasswordField();
44-
passwordField.Rect = [50, 500, 200, 50];
53+
passwordField.Rect = [50, 150, 30, 10];
4554
doc.addField(passwordField);
4655

47-
var radioGroup = new RadioButton();
56+
doc.text(50, 165, 'RadioGroup:');
57+
var radioGroup = new RadioButton();//new RadioButton();
58+
//radioGroup.Rect = [50, 160, 30, 50];
4859
radioGroup.V = "/Test";
4960
//radioGroup.noToggleToOff = true;
5061
radioGroup.Subtype = "Form";
5162

5263
doc.addField(radioGroup);
5364

5465
var radioButton1 = radioGroup.createOption("Test");
55-
radioButton1.Rect = [50, 450, 200, 50];
66+
radioButton1.Rect = [50, 170, 30, 10];
5667
radioButton1.AS = "/Test";
5768

5869
var radioButton2 = radioGroup.createOption("Test2");
59-
radioButton2.Rect = [50, 400, 200, 50];
70+
radioButton2.Rect = [50, 180, 30, 10];
6071

6172
var radioButton3 = radioGroup.createOption("Test3");
62-
radioButton3.Rect = [50, 350, 200, 50];
73+
radioButton3.Rect = [50, 190, 20, 10];
6374

64-
radioGroup.setAppearance(AcroForm.Appearance.RadioButton.Circle);
75+
radioGroup.setAppearance(AcroForm.Appearance.RadioButton.Cross);

jspdf.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,6 @@ var jsPDF = (function (global) {
901901

902902
outToPages = true;
903903

904-
events.publish
905-
906904
return content.join('\n');
907905
},
908906
getStyle = function (style) {

plugins/acroform.js

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
(AcroForm = function (jsPDFAPI) {
22
'use strict';
33

4+
AcroForm.scale = function (x) {
5+
return (x * (acroformPlugin.internal.scaleFactor / 1));// 1 = (96 / 72)
6+
};
7+
AcroForm.antiScale = function (x) {
8+
return ((1 / acroformPlugin.internal.scaleFactor ) * x);
9+
};
10+
411
var acroformPlugin = {
512
fields: [],
613
xForms: [],
@@ -119,9 +126,17 @@
119126
}
120127

121128
var fieldArray = fieldArray || this.acroformPlugin.acroFormDictionaryRoot.Kids;
129+
122130
for (var i in fieldArray) {
123131
var key = i;
124132
var form = fieldArray[i];
133+
134+
var oldRect = form.Rect;
135+
136+
if (form.Rect) {
137+
form.Rect = AcroForm.internal.calculateCoordinates.call(this, form.Rect);
138+
}
139+
125140
// Start Writing the Object
126141
this.internal.newObjectDeferredBegin(form.objId);
127142

@@ -130,6 +145,8 @@
130145

131146
content += ("<<\n" + form.getContent());
132147

148+
form.Rect = oldRect;
149+
133150
if (form.hasAppearanceStream && !form.appearanceStreamContent) {
134151
// Calculate Appearance
135152
var appearance = AcroForm.internal.calculateAppearanceStream.call(this, form);
@@ -197,6 +214,8 @@
197214
var content = "";
198215
content += (form.getString());
199216
this.internal.out(content);
217+
218+
delete fieldArray[key];
200219
}
201220
};
202221

@@ -379,8 +398,8 @@ AcroForm.internal = {};
379398

380399
AcroForm.createFormXObject = function (formObject) {
381400
var xobj = new AcroForm.FormXObject;
382-
var height = formObject.Rect[3] - formObject.Rect[1] || 0;
383-
var width = formObject.Rect[2] - formObject.Rect[0] || 0;
401+
var height = AcroForm.Appearance.internal.getHeight(formObject) || 0;
402+
var width = AcroForm.Appearance.internal.getWidth(formObject) || 0;
384403
xobj.BBox = [0, 0, width, height];
385404
return xobj;
386405
};
@@ -663,6 +682,7 @@ AcroForm.Appearance.internal = {
663682
var width = AcroForm.Appearance.internal.getWidth(formObject);
664683
var height = AcroForm.Appearance.internal.getHeight(formObject);
665684
var a = min(width, height);
685+
var crossSize = a;
666686
var borderPadding = 2; // The Padding in px
667687

668688

@@ -689,10 +709,10 @@ AcroForm.Appearance.internal = {
689709
},
690710
};
691711
AcroForm.Appearance.internal.getWidth = function (formObject) {
692-
return formObject.Rect[2] - formObject.Rect[0] || 0;
712+
return formObject.Rect[2];//(formObject.Rect[2] - formObject.Rect[0]) || 0;
693713
};
694714
AcroForm.Appearance.internal.getHeight = function (formObject) {
695-
return formObject.Rect[3] - formObject.Rect[1] || 0;
715+
return formObject.Rect[3];//(formObject.Rect[1] - formObject.Rect[3]) || 0;
696716
};
697717

698718
// ##########################
@@ -802,6 +822,10 @@ AcroForm.PDFObject.prototype.getContent = function () {
802822
var key = keys[i];
803823
var value = fieldObject[key];
804824

825+
/*if (key == 'Rect' && value) {
826+
value = AcroForm.internal.calculateCoordinates.call(jsPDF.API.acroformPlugin.internal, value);
827+
}*/
828+
805829
if (value) {
806830
if (Array.isArray(value)) {
807831
content += '/' + key + ' ' + AcroForm.internal.arrayToPdfArray(value) + "\n";
@@ -901,8 +925,8 @@ AcroForm.Field = function () {
901925
return;
902926
}
903927
var tmp = _Rect;
904-
var calculatedRes = AcroForm.internal.calculateCoordinates(_Rect);
905-
return calculatedRes
928+
//var calculatedRes = AcroForm.internal.calculateCoordinates(_Rect); // do later!
929+
return tmp
906930
},
907931
set: function (val) {
908932
_Rect = val;
@@ -1343,9 +1367,9 @@ AcroForm.internal.calculateX = function (formObject, text, font, maxFontSize) {
13431367
var borderPadding = 2;
13441368

13451369

1346-
var height = formObject.Rect[3] - formObject.Rect[1] || 0;
1370+
var height = AcroForm.Appearance.internal.getHeight(formObject) || 0;
13471371
height = (height < 0) ? -height : height;
1348-
var width = formObject.Rect[2] - formObject.Rect[0] || 0;
1372+
var width = AcroForm.Appearance.internal.getWidth(formObject) || 0;
13491373
width = (width < 0) ? -width : width;
13501374

13511375
var isSmallerThanWidth = function (i, lastLine, fontSize) {
@@ -1367,7 +1391,7 @@ AcroForm.internal.calculateX = function (formObject, text, font, maxFontSize) {
13671391
var textHeight = AcroForm.internal.calculateFontSpace("3", fontSize + "px", font).height;
13681392
var startY = (formObject.multiline) ? height - fontSize : (height - textHeight) / 2;
13691393
startY += lineSpacing;
1370-
var startX = borderPadding;
1394+
var startX = -borderPadding;
13711395

13721396
var lastX = startX, lastY = startY;
13731397
var firstWordInLine = 0, lastWordInLine = 0;
@@ -1526,20 +1550,45 @@ AcroForm.internal.calculateAppearanceStream = function (formObject) {
15261550
*/
15271551
AcroForm.internal.calculateCoordinates = function (x, y, w, h) {
15281552
var coordinates = {};
1529-
if (Array.isArray(x)) { // in case, the parameters are sent in as an array
1530-
/*coordinates.lowerLeft_X = x[0] | 0;
1531-
coordinates.lowerLeft_Y = x[1] | 0;
1532-
coordinates.upperRight_X = x[2] | 0;
1533-
coordinates.upperRight_Y = x[3] | 0;*/
1534-
coordinates.lowerLeft_X = x[0] | 0;
1535-
coordinates.lowerLeft_Y = x[1] | 0;
1536-
coordinates.upperRight_X = x[0] + x[2] | 0;
1537-
coordinates.upperRight_Y = x[1] + x[3] | 0;
1553+
1554+
if (this.internal) {
1555+
function mmtopx(x) {
1556+
return (x * this.internal.scaleFactor);
1557+
}
1558+
1559+
if (Array.isArray(x)) {
1560+
x[0] = AcroForm.scale(x[0]);
1561+
x[1] = AcroForm.scale(x[1]);
1562+
x[2] = AcroForm.scale(x[2]);
1563+
x[3] = AcroForm.scale(x[3]);
1564+
1565+
coordinates.lowerLeft_X = x[0] | 0;
1566+
coordinates.lowerLeft_Y = (mmtopx.call(this, this.internal.pageSize.height) - x[3] - x[1]) | 0;
1567+
coordinates.upperRight_X = x[0] + x[2] | 0;
1568+
coordinates.upperRight_Y = (mmtopx.call(this, this.internal.pageSize.height) - x[1]) | 0;
1569+
} else {
1570+
x = AcroForm.scale(x);
1571+
y = AcroForm.scale(y);
1572+
w = AcroForm.scale(w);
1573+
h = AcroForm.scale(h);
1574+
coordinates.lowerLeft_X = x | 0;
1575+
coordinates.lowerLeft_Y = this.internal.pageSize.height - y | 0;
1576+
coordinates.upperRight_X = x + w | 0;
1577+
coordinates.upperRight_Y = this.internal.pageSize.height - y + h | 0;
1578+
}
15381579
} else {
1539-
coordinates.lowerLeft_X = x | 0;
1540-
coordinates.lowerLeft_Y = y | 0;
1541-
coordinates.upperRight_X = x + w | 0;
1542-
coordinates.upperRight_Y = y + h | 0;
1580+
// old method, that is fallback, if we can't get the pageheight, the coordinate-system starts from lower left
1581+
if (Array.isArray(x)) {
1582+
coordinates.lowerLeft_X = x[0] | 0;
1583+
coordinates.lowerLeft_Y = x[1] | 0;
1584+
coordinates.upperRight_X = x[0] + x[2] | 0;
1585+
coordinates.upperRight_Y = x[1] + x[3] | 0;
1586+
} else {
1587+
coordinates.lowerLeft_X = x | 0;
1588+
coordinates.lowerLeft_Y = y | 0;
1589+
coordinates.upperRight_X = x + w | 0;
1590+
coordinates.upperRight_Y = y + h | 0;
1591+
}
15431592
}
15441593

15451594
return [coordinates.lowerLeft_X, coordinates.lowerLeft_Y, coordinates.upperRight_X, coordinates.upperRight_Y];

0 commit comments

Comments
 (0)