@@ -86,9 +86,10 @@ public PdfPagesTree(PdfCatalog pdfCatalog) {
86
86
this .pages = new ArrayList <>();
87
87
if (pdfCatalog .getPdfObject ().containsKey (PdfName .Pages )) {
88
88
PdfDictionary pages = pdfCatalog .getPdfObject ().getAsDictionary (PdfName .Pages );
89
- if (pages == null )
89
+ if (pages == null ) {
90
90
throw new PdfException (
91
91
KernelExceptionMessageConstant .INVALID_PAGE_STRUCTURE_PAGES_MUST_BE_PDF_DICTIONARY );
92
+ }
92
93
this .root = new PdfPages (0 , Integer .MAX_VALUE , pages , null );
93
94
parents .add (this .root );
94
95
for (int i = 0 ; i < this .root .getCount (); i ++) {
@@ -107,6 +108,7 @@ public PdfPagesTree(PdfCatalog pdfCatalog) {
107
108
* Returns the {@link PdfPage} at the specified position in this list.
108
109
*
109
110
* @param pageNum one-based index of the element to return
111
+ *
110
112
* @return the {@link PdfPage} at the specified position in this list
111
113
*/
112
114
public PdfPage getPage (int pageNum ) {
@@ -135,13 +137,19 @@ public PdfPage getPage(int pageNum) {
135
137
}
136
138
pages .set (pageNum , pdfPage );
137
139
}
140
+ if (pdfPage == null ) {
141
+ throw new PdfException (
142
+ MessageFormatUtil .format (IoLogMessageConstant .PAGE_TREE_IS_BROKEN_FAILED_TO_RETRIEVE_PAGE ,
143
+ pageNum + 1 ));
144
+ }
138
145
return pdfPage ;
139
146
}
140
147
141
148
/**
142
149
* Returns the {@link PdfPage} by page's PdfDictionary.
143
150
*
144
151
* @param pageDictionary page's PdfDictionary
152
+ *
145
153
* @return the {@code PdfPage} object, that wraps {@code pageDictionary}.
146
154
*/
147
155
public PdfPage getPage (PdfDictionary pageDictionary ) {
@@ -215,7 +223,6 @@ public void addPage(PdfPage pdfPage) {
215
223
}
216
224
}
217
225
218
-
219
226
pdfPage .makeIndirect (document );
220
227
pdfPages .addPage (pdfPage .getPdfObject ());
221
228
pdfPage .parentPages = pdfPages ;
@@ -231,8 +238,9 @@ public void addPage(PdfPage pdfPage) {
231
238
*/
232
239
public void addPage (int index , PdfPage pdfPage ) {
233
240
--index ;
234
- if (index > pageRefs .size ())
241
+ if (index > pageRefs .size ()) {
235
242
throw new IndexOutOfBoundsException ("index" );
243
+ }
236
244
if (index == pageRefs .size ()) {
237
245
addPage (pdfPage );
238
246
return ;
@@ -254,6 +262,7 @@ public void addPage(int index, PdfPage pdfPage) {
254
262
* indices).
255
263
*
256
264
* @param pageNum the one-based index of the PdfPage to be removed
265
+ *
257
266
* @return the page that was removed from the list
258
267
*/
259
268
public PdfPage removePage (int pageNum ) {
@@ -281,6 +290,7 @@ void releasePage(int pageNumber) {
281
290
* Generate PdfPages tree.
282
291
*
283
292
* @return root {@link PdfPages}
293
+ *
284
294
* @throws PdfException in case empty document
285
295
*/
286
296
protected PdfObject generateTree () {
@@ -346,14 +356,15 @@ private void loadPage(int pageNum) {
346
356
/**
347
357
* Load page from pages tree node structure
348
358
*
349
- * @param pageNum page number to load
359
+ * @param pageNum page number to load
350
360
* @param processedParents set with already processed parents object reference numbers
351
- * if this method was called recursively to avoid infinite recursion.
361
+ * if this method was called recursively to avoid infinite recursion.
352
362
*/
353
363
private void loadPage (int pageNum , Set <PdfIndirectReference > processedParents ) {
354
364
PdfIndirectReference targetPage = pageRefs .get (pageNum );
355
- if (targetPage != null )
365
+ if (targetPage != null ) {
356
366
return ;
367
+ }
357
368
358
369
//if we go here, we have to split PdfPages that contains pageNum
359
370
int parentIndex = findPageParent (pageNum );
@@ -408,12 +419,16 @@ private void loadPage(int pageNum, Set<PdfIndirectReference> processedParents) {
408
419
PdfPages lastPdfPages = null ;
409
420
for (int i = 0 ; i < kids .size () && kidsCount > 0 ; i ++) {
410
421
/*
411
- * We don't release pdfPagesObject in the end of each loop because we enter this for-cycle only when parent has PdfPages kids.
412
- * If all of the kids are PdfPages, then there's nothing to release, because we don't release PdfPages at this point.
422
+ * We don't release pdfPagesObject in the end of each loop because we enter this for-cycle only when
423
+ * parent has PdfPages kids.
424
+ * If all of the kids are PdfPages, then there's nothing to release, because we don't release
425
+ * PdfPages at this point.
413
426
* If there are kids that are instances of PdfPage, then there's no sense in releasing them:
414
- * in this case ParentTreeStructure is being rebuilt by inserting an intermediate PdfPages between the parent and a PdfPage,
415
- * thus modifying the page object by resetting its parent, thus making it impossible to release the object.
416
- */
427
+ * in this case ParentTreeStructure is being rebuilt by inserting an intermediate PdfPages between
428
+ * the parent and a PdfPage,
429
+ * thus modifying the page object by resetting its parent, thus making it impossible to release the
430
+ * object.
431
+ */
417
432
PdfDictionary pdfPagesObject = kids .getAsDictionary (i );
418
433
if (pdfPagesObject .getAsArray (PdfName .Kids ) == null ) {
419
434
// pdfPagesObject is PdfPage
0 commit comments