@@ -333,7 +333,18 @@ public int getNumberOfPdfObjects() {
333
333
*/
334
334
public PdfPage getPage (int pageNum ) {
335
335
checkClosingStatus ();
336
- return catalog .getPage (pageNum );
336
+ return catalog .getPageTree ().getPage (pageNum );
337
+ }
338
+
339
+ /**
340
+ * Gets the {@link PdfPage} instance by {@link PdfDictionary}.
341
+ *
342
+ * @param pageDictionary {@link PdfDictionary} that present page.
343
+ * @return page by {@link PdfDictionary}.
344
+ */
345
+ public PdfPage getPage (PdfDictionary pageDictionary ) {
346
+ checkClosingStatus ();
347
+ return catalog .getPageTree ().getPage (pageDictionary );
337
348
}
338
349
339
350
/**
@@ -373,7 +384,7 @@ public PdfPage addNewPage() {
373
384
public PdfPage addNewPage (PageSize pageSize ) {
374
385
checkClosingStatus ();
375
386
PdfPage page = new PdfPage (this , pageSize );
376
- catalog . addPage (page );
387
+ checkAndAddPage (page );
377
388
dispatchEvent (new PdfDocumentEvent (PdfDocumentEvent .START_PAGE , page ));
378
389
dispatchEvent (new PdfDocumentEvent (PdfDocumentEvent .INSERT_PAGE , page ));
379
390
return page ;
@@ -401,7 +412,7 @@ public PdfPage addNewPage(int index) {
401
412
public PdfPage addNewPage (int index , PageSize pageSize ) {
402
413
checkClosingStatus ();
403
414
PdfPage page = new PdfPage (this , pageSize );
404
- catalog . addPage (index , page );
415
+ checkAndAddPage (index , page );
405
416
currentPage = page ;
406
417
dispatchEvent (new PdfDocumentEvent (PdfDocumentEvent .START_PAGE , page ));
407
418
dispatchEvent (new PdfDocumentEvent (PdfDocumentEvent .INSERT_PAGE , page ));
@@ -417,7 +428,7 @@ public PdfPage addNewPage(int index, PageSize pageSize) {
417
428
*/
418
429
public PdfPage addPage (PdfPage page ) {
419
430
checkClosingStatus ();
420
- catalog . addPage (page );
431
+ checkAndAddPage (page );
421
432
dispatchEvent (new PdfDocumentEvent (PdfDocumentEvent .INSERT_PAGE , page ));
422
433
return page ;
423
434
}
@@ -432,7 +443,7 @@ public PdfPage addPage(PdfPage page) {
432
443
*/
433
444
public PdfPage addPage (int index , PdfPage page ) {
434
445
checkClosingStatus ();
435
- catalog . addPage (index , page );
446
+ checkAndAddPage (index , page );
436
447
currentPage = page ;
437
448
dispatchEvent (new PdfDocumentEvent (PdfDocumentEvent .INSERT_PAGE , page ));
438
449
return currentPage ;
@@ -445,7 +456,7 @@ public PdfPage addPage(int index, PdfPage page) {
445
456
*/
446
457
public int getNumberOfPages () {
447
458
checkClosingStatus ();
448
- return catalog .getNumberOfPages ();
459
+ return catalog .getPageTree (). getNumberOfPages ();
449
460
}
450
461
451
462
/**
@@ -456,7 +467,17 @@ public int getNumberOfPages() {
456
467
*/
457
468
public int getPageNumber (PdfPage page ) {
458
469
checkClosingStatus ();
459
- return catalog .getPageNumber (page );
470
+ return catalog .getPageTree ().getPageNumber (page );
471
+ }
472
+
473
+ /**
474
+ * Gets page number by {@link PdfDictionary}.
475
+ *
476
+ * @param pageDictionary {@link PdfDictionary} that present page.
477
+ * @return page number by {@link PdfDictionary}.
478
+ */
479
+ public int getPageNumber (PdfDictionary pageDictionary ) {
480
+ return catalog .getPageTree ().getPageNumber (pageDictionary );
460
481
}
461
482
462
483
/**
@@ -471,9 +492,7 @@ public int getPageNumber(PdfPage page) {
471
492
public boolean removePage (PdfPage page ) {
472
493
checkClosingStatus ();
473
494
int pageNum = getPageNumber (page );
474
- if (pageNum < 1 )
475
- return false ;
476
- return removePage (pageNum ) != null ;
495
+ return pageNum >= 1 && removePage (pageNum ) != null ;
477
496
}
478
497
479
498
/**
@@ -484,7 +503,7 @@ public boolean removePage(PdfPage page) {
484
503
*/
485
504
public PdfPage removePage (int pageNum ) {
486
505
checkClosingStatus ();
487
- PdfPage removedPage = catalog .removePage (pageNum );
506
+ PdfPage removedPage = catalog .getPageTree (). removePage (pageNum );
488
507
489
508
if (removedPage != null ) {
490
509
catalog .removeOutlines (removedPage );
@@ -659,7 +678,7 @@ public void close() {
659
678
catalog .put (PdfName .PageLabels , catalog .pageLabels .buildTree ());
660
679
}
661
680
662
- PdfObject pageRoot = catalog .pageTree .generateTree ();
681
+ PdfObject pageRoot = catalog .getPageTree () .generateTree ();
663
682
if (catalog .getPdfObject ().isModified () || pageRoot .isModified ()) {
664
683
catalog .getPdfObject ().put (PdfName .Pages , pageRoot );
665
684
catalog .getPdfObject ().flush (false );
@@ -695,7 +714,7 @@ public void close() {
695
714
catalog .put (PdfName .PageLabels , catalog .pageLabels .buildTree ());
696
715
}
697
716
698
- catalog .getPdfObject ().put (PdfName .Pages , catalog .pageTree .generateTree ());
717
+ catalog .getPdfObject ().put (PdfName .Pages , catalog .getPageTree () .generateTree ());
699
718
700
719
for (Map .Entry <PdfName , PdfNameTree > entry : catalog .nameTrees .entrySet ()) {
701
720
PdfNameTree tree = entry .getValue ();
@@ -712,7 +731,7 @@ public void close() {
712
731
flushFonts ();
713
732
writer .flushWaitingObjects ();
714
733
// flush unused objects
715
- if (flushUnusedObjects ) {
734
+ if (isFlushUnusedObjects () ) {
716
735
for (int i = 0 ; i < xref .size (); i ++) {
717
736
PdfIndirectReference indirectReference = xref .get (i );
718
737
if (!indirectReference .isFree () && !indirectReference .checkState (PdfObject .FLUSHED )) {
@@ -762,7 +781,7 @@ public void close() {
762
781
writer .close ();
763
782
}
764
783
}
765
- catalog .pageTree .clearPageRefs ();
784
+ catalog .getPageTree () .clearPageRefs ();
766
785
removeAllHandlers ();
767
786
if (reader != null && isCloseReader ()) {
768
787
reader .close ();
@@ -1393,6 +1412,24 @@ protected void flushFonts() {
1393
1412
}
1394
1413
}
1395
1414
1415
+ protected void checkAndAddPage (int index , PdfPage page ) {
1416
+ if (page .isFlushed ()) {
1417
+ throw new PdfException (PdfException .FlushedPageCannotBeAddedOrInserted , page );
1418
+ }
1419
+ if (page .getDocument () != null && this != page .getDocument ()) {
1420
+ throw new PdfException (PdfException .Page1CannotBeAddedToDocument2BecauseItBelongsToDocument3 ).setMessageParams (page , this , page .getDocument ());
1421
+ }
1422
+ catalog .getPageTree ().addPage (index , page );
1423
+ }
1424
+
1425
+ protected void checkAndAddPage (PdfPage page ) {
1426
+ if (page .isFlushed ())
1427
+ throw new PdfException (PdfException .FlushedPageCannotBeAddedOrInserted , page );
1428
+ if (page .getDocument () != null && this != page .getDocument ())
1429
+ throw new PdfException (PdfException .Page1CannotBeAddedToDocument2BecauseItBelongsToDocument3 ).setMessageParams (page , this , page .getDocument ());
1430
+ catalog .getPageTree ().addPage (page );
1431
+ }
1432
+
1396
1433
/**
1397
1434
* checks whether a method is invoked at the closed document
1398
1435
* @throws PdfException
@@ -1536,7 +1573,7 @@ private void cloneOutlines(Set<PdfOutline> outlinesToCopy, PdfOutline newParent,
1536
1573
array .addAll ((PdfArray ) srcNamedDestinations .get (name ));
1537
1574
PdfObject pageObject = array .get (0 );
1538
1575
if (!pageObject .isNumber ()) {
1539
- PdfPage oldPage = catalog .getPage ((PdfDictionary )pageObject );
1576
+ PdfPage oldPage = catalog .getPageTree (). getPage ((PdfDictionary )pageObject );
1540
1577
PdfPage newPage = page2page .get (oldPage );
1541
1578
if (oldPage == null || newPage == null ) {
1542
1579
dest = null ;
@@ -1553,7 +1590,7 @@ private void cloneOutlines(Set<PdfOutline> outlinesToCopy, PdfOutline newParent,
1553
1590
destArray .addAll ((PdfArray ) dest .getPdfObject ());
1554
1591
PdfObject pageObject = destArray .get (0 );
1555
1592
if (!pageObject .isNumber ()) {
1556
- PdfPage oldPage = catalog .getPage ((PdfDictionary )pageObject );
1593
+ PdfPage oldPage = catalog .getPageTree (). getPage ((PdfDictionary )pageObject );
1557
1594
PdfPage newPage = page2page .get (oldPage );
1558
1595
if (oldPage == null || newPage == null ) {
1559
1596
dest = null ;
@@ -1583,6 +1620,7 @@ private void ensureTreeRootAddedToNames(PdfObject treeRoot, PdfName treeType) {
1583
1620
names .put (treeType , treeRoot );
1584
1621
}
1585
1622
1623
+ @ SuppressWarnings ("unused" )
1586
1624
private byte [] getSerializedBytes () {
1587
1625
ByteArrayOutputStream bos = null ;
1588
1626
ObjectOutputStream oos = null ;
0 commit comments