@syncfusion/ej2-pdf 24.2.7 → 24.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8906,6 +8906,13 @@ class PdfField {
8906
8906
  this._textAlignment = value;
8907
8907
  this._stringFormat = new PdfStringFormat(value, PdfVerticalAlignment.middle);
8908
8908
  }
8909
+ _parseItems() {
8910
+ const collection = [];
8911
+ for (let i = 0; i < this.itemsCount; i++) {
8912
+ collection.push(this.itemAt(i));
8913
+ }
8914
+ return collection;
8915
+ }
8909
8916
  }
8910
8917
  /**
8911
8918
  * `PdfTextBoxField` class represents the text box field objects.
@@ -39361,18 +39368,24 @@ class _PdfCrossReference {
39361
39368
  }
39362
39369
  }
39363
39370
  let hasRoot = false;
39371
+ let root;
39364
39372
  try {
39365
- const root = trailerDictionary.get('Root');
39366
- if (root) {
39373
+ root = trailerDictionary.get('Root');
39374
+ }
39375
+ catch (e) {
39376
+ throw new BaseException('Invalid cross reference', 'XRefParseException');
39377
+ }
39378
+ if (root) {
39379
+ try {
39367
39380
  const pagesEntry = root.get('Pages');
39368
39381
  if (pagesEntry) {
39369
39382
  this._root = root;
39370
39383
  hasRoot = true;
39371
39384
  }
39372
39385
  }
39373
- }
39374
- catch (ex) {
39375
- throw new BaseException('Invalid cross reference', 'InvalidXRef');
39386
+ catch (ex) {
39387
+ throw new BaseException('Invalid cross reference', 'InvalidXRef');
39388
+ }
39376
39389
  }
39377
39390
  if (!hasRoot) {
39378
39391
  if (!recoveryMode) {
@@ -40696,7 +40709,7 @@ class PdfForm {
40696
40709
  for (let i = 0; i < this._fieldCollection.length; i++) {
40697
40710
  const field = this._fieldCollection[Number.parseInt(i.toString(), 10)];
40698
40711
  if (field.page) {
40699
- const index = _getPageIndex(document, field.page._pageDictionary);
40712
+ const index = _getPageIndex(document, this._sortItemByPageIndex(field, true)._pageDictionary);
40700
40713
  if (index >= 0) {
40701
40714
  if (fieldCollection.has(index)) {
40702
40715
  value = fieldCollection.get(index);
@@ -40720,16 +40733,18 @@ class PdfForm {
40720
40733
  let fieldsCount = 0;
40721
40734
  fieldCollection.forEach((value, key) => {
40722
40735
  this._tabOrder = this._tabCollection.get(key);
40723
- const fields = value;
40724
- fields.sort((pdfField1, pdfField2) => {
40725
- return this._compareFields(pdfField1, pdfField2);
40726
- });
40727
- for (let j = 0; j < fields.length; j++) {
40728
- const fieldIndex = this._fieldCollection.indexOf(fields[Number.parseInt(j.toString(), 10)]);
40729
- if (fieldIndex !== -1 && fieldIndex !== fieldsCount + j) {
40730
- const field = this._fieldCollection[Number.parseInt(fieldIndex.toString(), 10)];
40731
- this._fieldCollection.splice(fieldIndex, 1);
40732
- this._fieldCollection.splice(fieldsCount + j, 0, field);
40736
+ if (this._tabOrder !== PdfFormFieldsTabOrder.structure) {
40737
+ const fields = value;
40738
+ fields.sort((pdfField1, pdfField2) => {
40739
+ return this._compareFields(pdfField1, pdfField2);
40740
+ });
40741
+ for (let j = 0; j < fields.length; j++) {
40742
+ const fieldIndex = this._fieldCollection.indexOf(fields[Number.parseInt(j.toString(), 10)]);
40743
+ if (fieldIndex !== -1 && fieldIndex !== fieldsCount + j) {
40744
+ const field = this._fieldCollection[Number.parseInt(fieldIndex.toString(), 10)];
40745
+ this._fieldCollection.splice(fieldIndex, 1);
40746
+ this._fieldCollection.splice(fieldsCount + j, 0, field);
40747
+ }
40733
40748
  }
40734
40749
  }
40735
40750
  fieldsCount += value.length;
@@ -40968,10 +40983,24 @@ class PdfForm {
40968
40983
  const page1 = field1.page;
40969
40984
  const page2 = field2.page;
40970
40985
  if (page1 && page1 instanceof PdfPage && page2 && page2 instanceof PdfPage) {
40971
- const page1Index = page1._pageIndex;
40972
- const page2Index = page2._pageIndex;
40973
- const rectangle1 = this._getRectangle(field1._dictionary);
40974
- const rectangle2 = this._getRectangle(field2._dictionary);
40986
+ const page1Index = this._sortItemByPageIndex(field1, false)._pageIndex;
40987
+ const page2Index = this._sortItemByPageIndex(field2, false)._pageIndex;
40988
+ let rectangle1;
40989
+ if (field1._dictionary.has('Kids')) {
40990
+ rectangle1 = this._getItemRectangle(field1);
40991
+ }
40992
+ else {
40993
+ rectangle1 = this._getRectangle(field1._dictionary);
40994
+ }
40995
+ let rectangle2;
40996
+ if (field2._dictionary.has('Kids')) {
40997
+ rectangle2 = this._getItemRectangle(field2);
40998
+ }
40999
+ else {
41000
+ rectangle2 = this._getRectangle(field2._dictionary);
41001
+ }
41002
+ const firstHeight = rectangle1[3] - rectangle1[1];
41003
+ const secondHeight = rectangle2[3] - rectangle2[1];
40975
41004
  if (rectangle1 && rectangle1.length >= 2 && rectangle2 && rectangle2.length >= 2) {
40976
41005
  const x1 = rectangle1[0];
40977
41006
  const y1 = rectangle1[1];
@@ -40982,6 +41011,13 @@ class PdfForm {
40982
41011
  index = page1Index - page2Index;
40983
41012
  if (this._tabOrder === PdfFormFieldsTabOrder.row) {
40984
41013
  xdiff = this._compare(y2, y1);
41014
+ if (xdiff !== 0) {
41015
+ let isValid = xdiff === -1 && y1 > y2 && (y1 - firstHeight / 2) < y2;
41016
+ isValid = isValid || (xdiff === 1 && y2 > y1 && (y2 - secondHeight / 2) < y1);
41017
+ if (isValid) {
41018
+ xdiff = 0;
41019
+ }
41020
+ }
40985
41021
  if (index !== 0) {
40986
41022
  result = index;
40987
41023
  }
@@ -41024,27 +41060,32 @@ class PdfForm {
41024
41060
  return result;
41025
41061
  }
41026
41062
  _getRectangle(dictionary) {
41063
+ let rect;
41027
41064
  if (dictionary.has('Rect')) {
41028
- const rect = dictionary.get('Rect');
41029
- if (rect) {
41030
- return rect;
41031
- }
41065
+ rect = dictionary.getArray('Rect');
41032
41066
  }
41033
- else {
41034
- if (dictionary.has('Kids')) {
41035
- const kidsArray = dictionary.get('Kids');
41036
- if (kidsArray) {
41037
- if (kidsArray.length > 1) {
41038
- kidsArray.sort((x, y) => {
41039
- return this._compareKidsElement(x, y);
41040
- });
41067
+ return rect;
41068
+ }
41069
+ _getItemRectangle(field) {
41070
+ let result;
41071
+ const dictionary = field._dictionary;
41072
+ if (dictionary.has('Kids')) {
41073
+ const kids = dictionary.getArray('Kids');
41074
+ if (kids && kids.length >= 1) {
41075
+ if (kids.length === 1) {
41076
+ result = this._getRectangle(kids[0]);
41077
+ }
41078
+ else {
41079
+ if (field && field.itemsCount > 1) {
41080
+ result = this._getRectangle(field.itemAt(0)._dictionary);
41081
+ }
41082
+ else {
41083
+ result = this._getRectangle(kids[0]);
41041
41084
  }
41042
- const dictionary = this._crossReference._fetch(kidsArray[0]);
41043
- return this._getRectangle(dictionary);
41044
41085
  }
41045
41086
  }
41046
41087
  }
41047
- return null;
41088
+ return result;
41048
41089
  }
41049
41090
  _compare(x, y) {
41050
41091
  if (x > y) {
@@ -41062,6 +41103,7 @@ class PdfForm {
41062
41103
  const yDictionary = this._crossReference._fetch(y);
41063
41104
  const xRect = this._getRectangle(xDictionary);
41064
41105
  const yRect = this._getRectangle(yDictionary);
41106
+ let result;
41065
41107
  if (xRect && xRect.length >= 2 && yRect && yRect.length >= 2) {
41066
41108
  const x1 = xRect[0];
41067
41109
  const y1 = xRect[1];
@@ -41069,7 +41111,6 @@ class PdfForm {
41069
41111
  const y2 = yRect[1];
41070
41112
  if (typeof x1 === 'number' && typeof x2 === 'number' &&
41071
41113
  typeof y1 === 'number' && typeof y2 === 'number') {
41072
- let result = 0;
41073
41114
  let xdiff;
41074
41115
  if (this._tabOrder === PdfFormFieldsTabOrder.row) {
41075
41116
  xdiff = this._compare(y2, y1);
@@ -41089,10 +41130,89 @@ class PdfForm {
41089
41130
  result = this._compare(y2, y1);
41090
41131
  }
41091
41132
  }
41133
+ else {
41134
+ result = 0;
41135
+ }
41092
41136
  return result;
41093
41137
  }
41094
41138
  }
41095
- return null;
41139
+ return result;
41140
+ }
41141
+ _sortItemByPageIndex(field, hasPageTabOrder) {
41142
+ let page = field.page;
41143
+ const tabOrder = this._tabOrder;
41144
+ this._tabOrder = hasPageTabOrder ? field.page.tabOrder : tabOrder;
41145
+ this._sortFieldItems(field);
41146
+ if (field._isLoaded && field._kidsCount > 1) {
41147
+ page = field.itemAt(0).page;
41148
+ }
41149
+ this._tabOrder = tabOrder;
41150
+ if (typeof page === 'undefined') {
41151
+ page = field.page;
41152
+ }
41153
+ return page;
41154
+ }
41155
+ _sortFieldItems(field) {
41156
+ if (field._isLoaded && (field instanceof PdfTextBoxField ||
41157
+ field instanceof PdfListBoxField ||
41158
+ field instanceof PdfCheckBoxField ||
41159
+ field instanceof PdfRadioButtonListField)) {
41160
+ const collection = field._parseItems(); // eslint-disable-line
41161
+ collection.sort((item1, item2) => {
41162
+ return this._compareFieldItem(item1, item2);
41163
+ });
41164
+ field._parsedItems.clear();
41165
+ for (let i = 0; i < collection.length; i++) {
41166
+ field._parsedItems.set(i, collection[Number.parseInt(i.toString(), 10)]);
41167
+ }
41168
+ }
41169
+ }
41170
+ _compareFieldItem(item1, item2) {
41171
+ let result = 0;
41172
+ if (typeof item1 !== 'undefined' && typeof item2 !== 'undefined') {
41173
+ const page1 = item1.page;
41174
+ const page2 = item2.page;
41175
+ const array1 = this._getRectangle(item1._dictionary);
41176
+ const array2 = this._getRectangle(item2._dictionary);
41177
+ if (array1 && array2) {
41178
+ const x1 = array1[0];
41179
+ const y1 = array1[1];
41180
+ const x2 = array2[0];
41181
+ const y2 = array2[1];
41182
+ let xdiff;
41183
+ if (this._tabOrder === PdfFormFieldsTabOrder.row) {
41184
+ xdiff = this._compare(page1._pageIndex, page2._pageIndex);
41185
+ if (xdiff !== 0) {
41186
+ result = xdiff;
41187
+ }
41188
+ else {
41189
+ xdiff = this._compare(y2, y1);
41190
+ if (xdiff !== 0) {
41191
+ result = xdiff;
41192
+ }
41193
+ else {
41194
+ result = this._compare(x1, x2);
41195
+ }
41196
+ }
41197
+ }
41198
+ else if (this._tabOrder === PdfFormFieldsTabOrder.column) {
41199
+ xdiff = this._compare(page1._pageIndex, page2._pageIndex);
41200
+ if (xdiff !== 0) {
41201
+ result = xdiff;
41202
+ }
41203
+ else {
41204
+ xdiff = this._compare(x1, x2);
41205
+ if (xdiff !== 0) {
41206
+ result = xdiff;
41207
+ }
41208
+ else {
41209
+ result = this._compare(y2, y1);
41210
+ }
41211
+ }
41212
+ }
41213
+ }
41214
+ }
41215
+ return result;
41096
41216
  }
41097
41217
  _clear() {
41098
41218
  this._fields = [];