Skip to content

Commit 951d300

Browse files
committed
Remove public page-related methods from PdfCatalog.
DEVSIX-597
1 parent 3f9968c commit 951d300

File tree

10 files changed

+118
-113
lines changed

10 files changed

+118
-113
lines changed

forms/src/main/java/com/itextpdf/forms/PdfAcroForm.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ private void defineWidgetPageAndAddToIt(PdfPage currentPage, PdfDictionary merge
885885
throw new PdfException(PdfException.PageWasAlreadyFlushedUseAddFieldAppearanceToPageMethodBeforePageFlushing);
886886
}
887887
PdfDocument doc = pageDic.getIndirectReference().getDocument();
888-
PdfPage widgetPage = doc.getCatalog().getPage(pageDic);
888+
PdfPage widgetPage = doc.getPage(pageDic);
889889
addWidgetAnnotationToPage(widgetPage, annot);
890890
} else {
891891
addWidgetAnnotationToPage(currentPage, annot);
@@ -1027,7 +1027,7 @@ private static PdfDictionary createAcroFormDictionaryByFields(PdfArray fields) {
10271027
private PdfPage getFieldPage(PdfDictionary annotDic) {
10281028
PdfDictionary pageDic = annotDic.getAsDictionary(PdfName.P);
10291029
if (pageDic != null) {
1030-
return document.getCatalog().getPage(pageDic);
1030+
return document.getPage(pageDic);
10311031
}
10321032
for (int i = 1; i <= document.getNumberOfPages(); i++) {
10331033
PdfPage page = document.getPage(i);

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfCatalog.java

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class PdfCatalog extends PdfObjectWrapper<PdfDictionary> {
6161

6262
private static final long serialVersionUID = -1354567597112193418L;
6363

64-
final PdfPagesTree pageTree;
64+
final private PdfPagesTree pageTree;
6565
protected Map<PdfName, PdfNameTree> nameTrees = new HashMap<>();
6666
protected PdfNumTree pageLabels;
6767
protected PdfOCProperties ocProperties;
@@ -88,42 +88,6 @@ protected PdfCatalog(PdfDocument pdfDocument) {
8888
this(new PdfDictionary().makeIndirect(pdfDocument));
8989
}
9090

91-
public void addPage(PdfPage page) {
92-
if (page.isFlushed())
93-
throw new PdfException(PdfException.FlushedPageCannotBeAddedOrInserted, page);
94-
if (page.getDocument() != null && page.getDocument() != getDocument())
95-
throw new PdfException(PdfException.Page1CannotBeAddedToDocument2BecauseItBelongsToDocument3).setMessageParams(page, getDocument(), page.getDocument());
96-
pageTree.addPage(page);
97-
}
98-
99-
public void addPage(int index, PdfPage page) {
100-
if (page.isFlushed())
101-
throw new PdfException(PdfException.FlushedPageCannotBeAddedOrInserted, page);
102-
if (page.getDocument() != null && page.getDocument() != getDocument())
103-
throw new PdfException(PdfException.Page1CannotBeAddedToDocument2BecauseItBelongsToDocument3).setMessageParams(page, getDocument(), page.getDocument());
104-
pageTree.addPage(index, page);
105-
}
106-
107-
public PdfPage getPage(int pageNum) {
108-
return pageTree.getPage(pageNum);
109-
}
110-
111-
public PdfPage getPage(PdfDictionary pageDictionary) {
112-
return pageTree.getPage(pageDictionary);
113-
}
114-
115-
public int getNumberOfPages() {
116-
return pageTree.getNumberOfPages();
117-
}
118-
119-
public int getPageNumber(PdfPage page) {
120-
return pageTree.getPageNumber(page);
121-
}
122-
123-
public int getPageNumber(PdfDictionary pageDictionary) {
124-
return pageTree.getPageNumber(pageDictionary);
125-
}
126-
12791
/**
12892
* Use this method to get the <B>Optional Content Properties Dictionary</B>.
12993
* Note that if you call this method, then the PdfDictionary with OCProperties will be
@@ -188,25 +152,28 @@ public PdfCatalog setAdditionalAction(PdfName key, PdfAction action) {
188152
/**
189153
* This flag determines if Outline tree of the document has been built via calling getOutlines method. If this flag is false all outline operations will be ignored
190154
*
191-
* @return
155+
* @return state of outline mode.
192156
*/
193157
public boolean isOutlineMode() {
194158
return outlineMode;
195159
}
196160

197161
/**
198-
* This method sets a page mode of the document
162+
* This method sets a page mode of the document.
163+
* </p>
164+
* Valid values are: {@code PdfName.UseNone}, {@code PdfName.UseOutlines}, {@code PdfName.UseThumbs},
165+
* {@code PdfName.FullScreen}, {@code PdfName.UseOC}, {@code PdfName.UseAttachments}.
199166
*
200-
* @param pageMode
201-
* @return
167+
* @param pageMode page mode.
168+
* @return current instance of PdfCatalog
202169
*/
203170
public PdfCatalog setPageMode(PdfName pageMode) {
204-
if (!pageMode.equals(PdfName.UseNone) && !pageMode.equals(PdfName.UseOutlines) &&
205-
!pageMode.equals(PdfName.UseThumbs) && !pageMode.equals(PdfName.FullScreen) &&
206-
!pageMode.equals(PdfName.UseOC) && !pageMode.equals(PdfName.UseAttachments)) {
207-
return this;
171+
if (pageMode.equals(PdfName.UseNone) || pageMode.equals(PdfName.UseOutlines) ||
172+
pageMode.equals(PdfName.UseThumbs) || pageMode.equals(PdfName.FullScreen) ||
173+
pageMode.equals(PdfName.UseOC) || pageMode.equals(PdfName.UseAttachments)) {
174+
return put(PdfName.PageMode, pageMode);
208175
}
209-
return put(PdfName.PageMode, pageMode);
176+
return this;
210177
}
211178

212179
public PdfName getPageMode() {
@@ -219,12 +186,12 @@ public PdfName getPageMode() {
219186
* @return
220187
*/
221188
public PdfCatalog setPageLayout (PdfName pageLayout) {
222-
if (!pageLayout.equals(PdfName.SinglePage) && !pageLayout.equals(PdfName.OneColumn) &&
223-
!pageLayout.equals(PdfName.TwoColumnLeft) && !pageLayout.equals(PdfName.TwoColumnRight) &&
224-
!pageLayout.equals(PdfName.TwoPageLeft) && !pageLayout.equals(PdfName.TwoPageRight)) {
225-
return this;
189+
if (pageLayout.equals(PdfName.SinglePage) || pageLayout.equals(PdfName.OneColumn) ||
190+
pageLayout.equals(PdfName.TwoColumnLeft) || pageLayout.equals(PdfName.TwoColumnRight) ||
191+
pageLayout.equals(PdfName.TwoPageLeft) || pageLayout.equals(PdfName.TwoPageRight)) {
192+
return put(PdfName.PageLayout, pageLayout);
226193
}
227-
return put(PdfName.PageLayout, pageLayout);
194+
return this;
228195
}
229196

230197
public PdfName getPageLayout(){
@@ -253,7 +220,7 @@ public PdfViewerPreferences getViewerPreferences() {
253220
/**
254221
* This method gets Names tree from the catalog.
255222
* @param treeType type of the tree (Dests, AP, EmbeddedFiles etc).
256-
* @return
223+
* @return returns {@link PdfNameTree}
257224
*/
258225
public PdfNameTree getNameTree(PdfName treeType) {
259226
PdfNameTree tree = nameTrees.get(treeType);
@@ -267,7 +234,7 @@ public PdfNameTree getNameTree(PdfName treeType) {
267234

268235
/**
269236
* This method returns the NumberTree of Page Labels
270-
* @return
237+
* @return returns {@link PdfNumTree}
271238
*/
272239
public PdfNumTree getPageLabelsTree(boolean createIfNotExists) {
273240
if (pageLabels == null && (getPdfObject().containsKey(PdfName.PageLabels) || createIfNotExists)) {
@@ -343,9 +310,10 @@ protected boolean isOCPropertiesMayHaveChanged() {
343310
return ocProperties != null;
344311
}
345312

346-
PdfPage removePage(int pageNum) {
347-
return pageTree.removePage(pageNum);
313+
PdfPagesTree getPageTree() {
314+
return pageTree;
348315
}
316+
349317
/**
350318
* this method return map containing all pages of the document with associated outlines.
351319
*

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfDocument.java

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,18 @@ public int getNumberOfPdfObjects() {
333333
*/
334334
public PdfPage getPage(int pageNum) {
335335
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);
337348
}
338349

339350
/**
@@ -373,7 +384,7 @@ public PdfPage addNewPage() {
373384
public PdfPage addNewPage(PageSize pageSize) {
374385
checkClosingStatus();
375386
PdfPage page = new PdfPage(this, pageSize);
376-
catalog.addPage(page);
387+
checkAndAddPage(page);
377388
dispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.START_PAGE, page));
378389
dispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.INSERT_PAGE, page));
379390
return page;
@@ -401,7 +412,7 @@ public PdfPage addNewPage(int index) {
401412
public PdfPage addNewPage(int index, PageSize pageSize) {
402413
checkClosingStatus();
403414
PdfPage page = new PdfPage(this, pageSize);
404-
catalog.addPage(index, page);
415+
checkAndAddPage(index, page);
405416
currentPage = page;
406417
dispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.START_PAGE, page));
407418
dispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.INSERT_PAGE, page));
@@ -417,7 +428,7 @@ public PdfPage addNewPage(int index, PageSize pageSize) {
417428
*/
418429
public PdfPage addPage(PdfPage page) {
419430
checkClosingStatus();
420-
catalog.addPage(page);
431+
checkAndAddPage(page);
421432
dispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.INSERT_PAGE, page));
422433
return page;
423434
}
@@ -432,7 +443,7 @@ public PdfPage addPage(PdfPage page) {
432443
*/
433444
public PdfPage addPage(int index, PdfPage page) {
434445
checkClosingStatus();
435-
catalog.addPage(index, page);
446+
checkAndAddPage(index, page);
436447
currentPage = page;
437448
dispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.INSERT_PAGE, page));
438449
return currentPage;
@@ -445,7 +456,7 @@ public PdfPage addPage(int index, PdfPage page) {
445456
*/
446457
public int getNumberOfPages() {
447458
checkClosingStatus();
448-
return catalog.getNumberOfPages();
459+
return catalog.getPageTree().getNumberOfPages();
449460
}
450461

451462
/**
@@ -456,7 +467,17 @@ public int getNumberOfPages() {
456467
*/
457468
public int getPageNumber(PdfPage page) {
458469
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);
460481
}
461482

462483
/**
@@ -471,9 +492,7 @@ public int getPageNumber(PdfPage page) {
471492
public boolean removePage(PdfPage page) {
472493
checkClosingStatus();
473494
int pageNum = getPageNumber(page);
474-
if (pageNum < 1)
475-
return false;
476-
return removePage(pageNum) != null;
495+
return pageNum >= 1 && removePage(pageNum) != null;
477496
}
478497

479498
/**
@@ -484,7 +503,7 @@ public boolean removePage(PdfPage page) {
484503
*/
485504
public PdfPage removePage(int pageNum) {
486505
checkClosingStatus();
487-
PdfPage removedPage = catalog.removePage(pageNum);
506+
PdfPage removedPage = catalog.getPageTree().removePage(pageNum);
488507

489508
if (removedPage != null) {
490509
catalog.removeOutlines(removedPage);
@@ -659,7 +678,7 @@ public void close() {
659678
catalog.put(PdfName.PageLabels, catalog.pageLabels.buildTree());
660679
}
661680

662-
PdfObject pageRoot = catalog.pageTree.generateTree();
681+
PdfObject pageRoot = catalog.getPageTree().generateTree();
663682
if (catalog.getPdfObject().isModified() || pageRoot.isModified()) {
664683
catalog.getPdfObject().put(PdfName.Pages, pageRoot);
665684
catalog.getPdfObject().flush(false);
@@ -695,7 +714,7 @@ public void close() {
695714
catalog.put(PdfName.PageLabels, catalog.pageLabels.buildTree());
696715
}
697716

698-
catalog.getPdfObject().put(PdfName.Pages, catalog.pageTree.generateTree());
717+
catalog.getPdfObject().put(PdfName.Pages, catalog.getPageTree().generateTree());
699718

700719
for (Map.Entry<PdfName, PdfNameTree> entry : catalog.nameTrees.entrySet()) {
701720
PdfNameTree tree = entry.getValue();
@@ -712,7 +731,7 @@ public void close() {
712731
flushFonts();
713732
writer.flushWaitingObjects();
714733
// flush unused objects
715-
if (flushUnusedObjects) {
734+
if (isFlushUnusedObjects()) {
716735
for (int i = 0; i < xref.size(); i++) {
717736
PdfIndirectReference indirectReference = xref.get(i);
718737
if (!indirectReference.isFree() && !indirectReference.checkState(PdfObject.FLUSHED)) {
@@ -762,7 +781,7 @@ public void close() {
762781
writer.close();
763782
}
764783
}
765-
catalog.pageTree.clearPageRefs();
784+
catalog.getPageTree().clearPageRefs();
766785
removeAllHandlers();
767786
if (reader != null && isCloseReader()) {
768787
reader.close();
@@ -1393,6 +1412,24 @@ protected void flushFonts() {
13931412
}
13941413
}
13951414

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+
13961433
/**
13971434
* checks whether a method is invoked at the closed document
13981435
* @throws PdfException
@@ -1536,7 +1573,7 @@ private void cloneOutlines(Set<PdfOutline> outlinesToCopy, PdfOutline newParent,
15361573
array.addAll((PdfArray) srcNamedDestinations.get(name));
15371574
PdfObject pageObject = array.get(0);
15381575
if (!pageObject.isNumber()) {
1539-
PdfPage oldPage = catalog.getPage((PdfDictionary)pageObject);
1576+
PdfPage oldPage = catalog.getPageTree().getPage((PdfDictionary)pageObject);
15401577
PdfPage newPage = page2page.get(oldPage);
15411578
if (oldPage == null || newPage == null) {
15421579
dest = null;
@@ -1553,7 +1590,7 @@ private void cloneOutlines(Set<PdfOutline> outlinesToCopy, PdfOutline newParent,
15531590
destArray.addAll((PdfArray) dest.getPdfObject());
15541591
PdfObject pageObject = destArray.get(0);
15551592
if (!pageObject.isNumber()) {
1556-
PdfPage oldPage = catalog.getPage((PdfDictionary)pageObject);
1593+
PdfPage oldPage = catalog.getPageTree().getPage((PdfDictionary)pageObject);
15571594
PdfPage newPage = page2page.get(oldPage);
15581595
if (oldPage == null || newPage == null) {
15591596
dest = null;
@@ -1583,6 +1620,7 @@ private void ensureTreeRootAddedToNames(PdfObject treeRoot, PdfName treeType) {
15831620
names.put(treeType, treeRoot);
15841621
}
15851622

1623+
@SuppressWarnings("unused")
15861624
private byte[] getSerializedBytes() {
15871625
ByteArrayOutputStream bos = null;
15881626
ObjectOutputStream oos = null;

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfPage.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,7 @@ private void flushXObjects(Collection<PdfObject> xObjects) {
786786
*/
787787
private void initParentPages() {
788788
if (this.parentPages == null) {
789-
PdfPagesTree pageTree = getDocument().getCatalog().pageTree;
790-
this.parentPages = pageTree.findPageParent(this);
789+
this.parentPages = getDocument().getCatalog().getPageTree().findPageParent(this);
791790
}
792791
}
793792

kernel/src/main/java/com/itextpdf/kernel/utils/CompareTool.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,9 @@ private String compareByContent(String outPath, String differenceImagePrefix, Ma
641641
}
642642

643643
private void loadPagesFromReader(PdfDocument doc, List<PdfDictionary> pages, List<PdfIndirectReference> pagesRef) {
644-
int numOfPages = doc.getCatalog().getNumberOfPages();
644+
int numOfPages = doc.getNumberOfPages();
645645
for (int i = 0; i < numOfPages; ++i) {
646-
pages.add(doc.getCatalog().getPage(i + 1).getPdfObject());
646+
pages.add(doc.getPage(i + 1).getPdfObject());
647647
pagesRef.add(pages.get(i).getIndirectReference());
648648
}
649649
}
@@ -1004,7 +1004,7 @@ private boolean compareXmls(InputStream xml1, InputStream xml2) throws ParserCon
10041004

10051005
private List<PdfLinkAnnotation> getLinkAnnotations(int pageNum, PdfDocument document) {
10061006
List<PdfLinkAnnotation> linkAnnotations = new ArrayList<>();
1007-
List<PdfAnnotation> annotations = document.getCatalog().getPage(pageNum).getAnnotations();
1007+
List<PdfAnnotation> annotations = document.getPage(pageNum).getAnnotations();
10081008
for (PdfAnnotation annotation : annotations) {
10091009
if(PdfName.Link.equals(annotation.getSubtype())) {
10101010
linkAnnotations.add((PdfLinkAnnotation)annotation);

kernel/src/main/java/com/itextpdf/kernel/utils/TaggedPdfReaderTool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ protected void parseTag(PdfMcr kid) {
192192
MarkedContentEventListener listener = new MarkedContentEventListener();
193193

194194
PdfCanvasProcessor processor = new PdfCanvasProcessor(listener);
195-
PdfPage page = document.getCatalog().getPage(pageDic);
195+
PdfPage page = document.getPage(pageDic);
196196
processor.processContent(page.getContentBytes(), page.getResources());
197197

198198
parsedTags.put(pageDic, listener.getMcidContent());

0 commit comments

Comments
 (0)