| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 | 1×
1×
1×
1×
5×
5×
5×
5×
5×
5×
5×
5×
5×
1×
5×
5×
5×
1×
6×
6×
6×
1×
2×
2×
2×
2×
2×
1×
1×
7×
7×
1×
7×
7×
1×
2×
1×
1×
1×
2×
1×
1×
1×
1×
7×
7×
7×
7×
7×
7×
7×
7×
7×
7×
29×
29×
16×
16×
3×
3×
13×
13×
13×
13×
13×
13×
13×
7×
7×
7×
7×
7×
7×
7×
6×
6×
13×
13×
13×
13×
13×
13×
9×
13×
13×
13×
13×
13×
80×
1×
5×
5×
5×
5×
5×
5×
5×
5×
5×
5×
5×
5×
5×
1×
1×
| define(["require", "exports", "./pdf-primitives", "./utils"], function (require, exports, pdf_primitives_1, utils_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _PdfCatalog = (function () {
function _PdfCatalog(xref) {
this._parsedPages = [];
this._pageCache = new Map();
this._crossReference = xref;
this._catalogDictionary = xref._getCatalogObj();
Iif (!(this._catalogDictionary instanceof pdf_primitives_1._PdfDictionary)) {
throw new utils_1.FormatError('Catalog object is not a dictionary.');
}
else {
this._catalogDictionary.isCatalog = true;
}
this._topPagesDictionary = this._catalogDictionary.get('Pages');
this.pageKidsCountCache = new pdf_primitives_1._PdfReferenceSetCache();
this.pageIndexCache = new pdf_primitives_1._PdfReferenceSetCache();
}
Object.defineProperty(_PdfCatalog.prototype, "version", {
get: function () {
var value;
Iif (this._catalogDictionary.has('Version')) {
var version = this._catalogDictionary.get('Version');
if (version) {
value = version.name;
}
}
return value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(_PdfCatalog.prototype, "pageCount", {
get: function () {
var obj = this._topPagesDictionary.get('Count');
Iif (typeof obj === 'undefined' || !Number.isInteger(obj)) {
throw new utils_1.FormatError('Invalid page count');
}
return obj;
},
enumerable: true,
configurable: true
});
Object.defineProperty(_PdfCatalog.prototype, "acroForm", {
get: function () {
var form;
Eif (this._catalogDictionary.has('AcroForm')) {
form = this._catalogDictionary.get('AcroForm');
}
Iif (form === null || typeof form === 'undefined') {
form = this._createForm();
}
return form;
},
enumerable: true,
configurable: true
});
_PdfCatalog.prototype._createForm = function () {
var form = new pdf_primitives_1._PdfDictionary(this._crossReference);
var ref = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(ref, form);
this._catalogDictionary.set('AcroForm', ref);
this._catalogDictionary._updated = true;
this._crossReference._allowCatalog = true;
form._updated = true;
return form;
};
_PdfCatalog.prototype._addToCache = function (pageIndex, dictionary, reference) {
this._pageCache.set(pageIndex, {
dictionary: dictionary,
reference: reference,
parent: dictionary.get('Parent')
});
this._parsedPages.push(pageIndex);
};
_PdfCatalog.prototype._getPageDictionary = function (pageIndex) {
Iif (pageIndex !== 0 && this._checkPageTreeFormat()) {
var nearestIndex = this._findNearestIndex(pageIndex);
if (nearestIndex > 0) {
var nearestCachedPage = this._pageCache.get(nearestIndex);
if (nearestCachedPage) {
var result = this._traverseFromCached(nearestIndex, nearestCachedPage, pageIndex);
if (result) {
return result;
}
}
}
}
return this._traverseFromRoot(pageIndex);
};
_PdfCatalog.prototype._checkPageTreeFormat = function () {
if (typeof this._hasInvalidPageTree === 'undefined') {
var kids = this._topPagesDictionary.get('Kids');
Eif (Array.isArray(kids)) {
this._hasInvalidPageTree = this.pageCount === kids.length;
}
}
return this._hasInvalidPageTree;
};
_PdfCatalog.prototype._findNearestIndex = function (targetIndex) {
var length = this._parsedPages.length;
var nearest = this._parsedPages[0];
var minDistance = Math.abs(nearest - targetIndex);
for (var i = 1; i < length; i++) {
var index = this._parsedPages[i];
var distance = Math.abs(index - targetIndex);
if (distance < minDistance) {
nearest = index;
minDistance = distance;
}
}
return nearest;
};
_PdfCatalog.prototype._traverseFromCached = function (index, cachedPage, targetIndex) {
var visitedNodes = new pdf_primitives_1._PdfReferenceSet();
var direction = targetIndex > index ? 1 : -1;
var currentNode = cachedPage.dictionary;
var currentIndex = index;
var parent = cachedPage.parent;
var currentReference = cachedPage.reference;
if (currentIndex === targetIndex && pdf_primitives_1._isName(currentNode.get('Type'), 'Page')) {
return { dictionary: currentNode, reference: currentReference };
}
while (currentNode && currentIndex !== targetIndex) {
if (currentNode instanceof pdf_primitives_1._PdfReference) {
if (visitedNodes.has(currentNode)) {
throw new utils_1.FormatError('Pages tree contains circular reference.');
}
visitedNodes.put(currentNode);
}
var nextPage = this._findNextPage(currentNode, parent, direction);
if (!nextPage) {
break;
}
currentNode = nextPage.dictionary;
currentReference = nextPage.reference;
currentIndex += direction;
if (currentIndex === targetIndex && pdf_primitives_1._isName(currentNode.get('Type'), 'Page')) {
this._addToCache(currentIndex, nextPage.dictionary, nextPage.reference);
return {
dictionary: currentNode,
reference: currentReference
};
}
}
return null;
};
_PdfCatalog.prototype._findNextPage = function (currentPage, parent, direction) {
var _this = this;
if (!parent || !parent.has('Kids')) {
return null;
}
var kids = parent.getRaw('Kids');
if (kids instanceof pdf_primitives_1._PdfReference) {
kids = this._crossReference._fetch(kids);
}
if (!Array.isArray(kids)) {
throw new utils_1.FormatError('Page dictionary kids object is not an array.');
}
var currentIndex = -1;
if (currentPage._reference) {
currentIndex = kids.indexOf(currentPage._reference);
}
else {
currentIndex = kids.findIndex(function (kid) {
if (kid instanceof pdf_primitives_1._PdfReference) {
kid = _this._crossReference._fetch(kid);
}
return kid === currentPage;
});
}
if (currentIndex === -1) {
return null;
}
var nextIndex = currentIndex + direction;
if (nextIndex < 0 || nextIndex >= kids.length) {
return null;
}
var nextKid = kids[nextIndex];
var nextDict = nextKid instanceof pdf_primitives_1._PdfReference ? this._crossReference._fetch(nextKid) : nextKid;
if (!(nextDict instanceof pdf_primitives_1._PdfDictionary)) {
throw new utils_1.FormatError('Invalid page dictionary type.');
}
return {
dictionary: nextDict,
reference: nextKid instanceof pdf_primitives_1._PdfReference ? nextKid : null
};
};
_PdfCatalog.prototype._traverseFromRoot = function (pageIndex) {
var nodesToVisit = [this._topPagesDictionary];
var visitedNodes = new pdf_primitives_1._PdfReferenceSet();
var pagesRef = this._catalogDictionary.getRaw('Pages');
Eif (pagesRef && pagesRef instanceof pdf_primitives_1._PdfReference) {
visitedNodes.put(pagesRef);
}
var xref = this._crossReference;
var pageKidsCountCache = this.pageKidsCountCache;
var pageIndexCache = this.pageIndexCache;
var currentPageIndex = 0;
while (nodesToVisit.length > 0) {
var currentNode = nodesToVisit.pop();
if (currentNode && currentNode instanceof pdf_primitives_1._PdfReference) {
var count_1 = pageKidsCountCache.get(currentNode);
if (count_1 >= 0 && currentPageIndex + count_1 <= pageIndex) {
currentPageIndex += count_1;
continue;
}
Iif (visitedNodes.has(currentNode)) {
throw new utils_1.FormatError('Pages tree contains circular reference.');
}
visitedNodes.put(currentNode);
var obj = xref._fetch(currentNode);
Eif (obj && obj instanceof pdf_primitives_1._PdfDictionary) {
var type = obj.getRaw('Type');
Iif (type instanceof pdf_primitives_1._PdfReference) {
type = xref._fetch(type);
}
if (pdf_primitives_1._isName(type, 'Page') || !obj.has('Kids')) {
Eif (!pageKidsCountCache.has(currentNode)) {
pageKidsCountCache.put(currentNode, 1);
}
Eif (!pageIndexCache.has(currentNode)) {
pageIndexCache.put(currentNode, currentPageIndex);
}
Eif (currentPageIndex === pageIndex) {
this._addToCache(currentPageIndex, obj, currentNode);
return { dictionary: obj, reference: currentNode };
}
currentPageIndex++;
continue;
}
}
nodesToVisit.push(obj);
continue;
}
Iif (!(currentNode instanceof pdf_primitives_1._PdfDictionary)) {
throw new utils_1.FormatError('Page dictionary kid reference points to wrong type of object.');
}
var objId = currentNode.objId;
var count = currentNode.get('Count');
Iif (count instanceof pdf_primitives_1._PdfReference) {
count = xref._fetch(count);
}
Eif (count !== null && typeof count !== 'undefined' && Number.isInteger(count) && count >= 0) {
if (objId && !pageKidsCountCache.has(objId)) {
pageKidsCountCache.set(objId, count);
}
Iif (currentPageIndex + count <= pageIndex) {
currentPageIndex += count;
continue;
}
}
var kids = currentNode.getRaw('Kids');
Iif (kids instanceof pdf_primitives_1._PdfReference) {
kids = xref._fetch(kids);
}
Iif (!Array.isArray(kids)) {
var type = currentNode.getRaw('Type');
if (type instanceof pdf_primitives_1._PdfReference) {
type = xref._fetch(type);
}
if (pdf_primitives_1._isName(type, 'Page') || !currentNode.has('Kids')) {
if (currentPageIndex === pageIndex) {
this._addToCache(currentPageIndex, currentNode, null);
return { dictionary: currentNode, reference: null };
}
currentPageIndex++;
continue;
}
throw new utils_1.FormatError('Page dictionary kids object is not an array.');
}
for (var last = kids.length - 1; last >= 0; last--) {
nodesToVisit.push(kids[last]);
}
}
throw new Error("Page index " + pageIndex + " not found.");
};
_PdfCatalog.prototype._destroy = function () {
Eif (this._catalogDictionary) {
this._catalogDictionary = undefined;
}
Eif (this._topPagesDictionary) {
this._topPagesDictionary = undefined;
}
Eif (this.pageIndexCache) {
this.pageIndexCache.clear();
this.pageIndexCache = undefined;
}
Eif (this.pageKidsCountCache) {
this.pageKidsCountCache.clear();
this.pageKidsCountCache = undefined;
}
this._hasInvalidPageTree = undefined;
this._pageCache.clear();
this._parsedPages = [];
};
return _PdfCatalog;
}());
exports._PdfCatalog = _PdfCatalog;
});
|