Skip to content

Commit c21fbdb

Browse files
committed
Fix possible issues with PdfPage's inherited properties
DEVSIX-1704
1 parent 4c06950 commit c21fbdb

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

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

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
33
This file is part of the iText (R) project.
4-
Copyright (c) 1998-2017 iText Group NV
4+
Copyright (c) 1998-2018 iText Group NV
55
Authors: Bruno Lowagie, Paulo Soares, et al.
66
77
This program is free software; you can redistribute it and/or modify
@@ -147,8 +147,7 @@ public int getRotation() {
147147
PdfNumber rotate = getPdfObject().getAsNumber(PdfName.Rotate);
148148
int rotateValue = 0;
149149
if (rotate == null) {
150-
initParentPages();
151-
rotate = (PdfNumber) getParentValue(this.parentPages, PdfName.Rotate);
150+
rotate = (PdfNumber) getInheritedValue(PdfName.Rotate, PdfObject.NUMBER);
152151
}
153152
if (rotate != null) {
154153
rotateValue = rotate.intValue();
@@ -271,8 +270,7 @@ public PdfResources getResources() {
271270
boolean readOnly = false;
272271
PdfDictionary resources = getPdfObject().getAsDictionary(PdfName.Resources);
273272
if (resources == null) {
274-
initParentPages();
275-
resources = (PdfDictionary) getParentValue(this.parentPages, PdfName.Resources);
273+
resources = (PdfDictionary) getInheritedValue(PdfName.Resources, PdfObject.DICTIONARY);
276274
if (resources != null) {
277275
readOnly = true;
278276
}
@@ -509,10 +507,9 @@ public void flush(boolean flushResourcesContentStreams) {
509507
* @throws PdfException in case of any error while reading MediaBox object.
510508
*/
511509
public Rectangle getMediaBox() {
512-
initParentPages();
513510
PdfArray mediaBox = getPdfObject().getAsArray(PdfName.MediaBox);
514511
if (mediaBox == null) {
515-
mediaBox = (PdfArray) getParentValue(parentPages, PdfName.MediaBox);
512+
mediaBox = (PdfArray) getInheritedValue(PdfName.MediaBox, PdfObject.ARRAY);
516513
}
517514
if (mediaBox == null) {
518515
throw new PdfException(PdfException.CannotRetrieveMediaBoxAttribute);
@@ -551,13 +548,12 @@ public PdfPage setMediaBox(Rectangle rectangle) {
551548
* and then shall be imposed on the output medium in some implementation-defined manner.
552549
*
553550
* @return the {@link Rectangle} object specified by pages's CropBox, expressed in default user space units.
554-
* MediaBox by default.
551+
* MediaBox by default.
555552
*/
556553
public Rectangle getCropBox() {
557-
initParentPages();
558554
PdfArray cropBox = getPdfObject().getAsArray(PdfName.CropBox);
559555
if (cropBox == null) {
560-
cropBox = (PdfArray) getParentValue(parentPages, PdfName.CropBox);
556+
cropBox = (PdfArray) getInheritedValue(PdfName.CropBox, PdfObject.ARRAY);
561557
if (cropBox == null) {
562558
return getMediaBox();
563559
}
@@ -1033,14 +1029,22 @@ private PdfArray getAnnots(boolean create) {
10331029
return annots;
10341030
}
10351031

1036-
private PdfObject getParentValue(PdfPages parentPages, PdfName pdfName) {
1032+
private PdfObject getInheritedValue(PdfName pdfName, int type) {
1033+
if (this.parentPages == null) {
1034+
this.parentPages = getDocument().getCatalog().getPageTree().findPageParent(this);
1035+
}
1036+
PdfObject val = getInheritedValue(this.parentPages, pdfName);
1037+
return val != null && val.getType() == type ? val : null;
1038+
}
1039+
1040+
private static PdfObject getInheritedValue(PdfPages parentPages, PdfName pdfName) {
10371041
if (parentPages != null) {
10381042
PdfDictionary parentDictionary = parentPages.getPdfObject();
10391043
PdfObject value = parentDictionary.get(pdfName);
10401044
if (value != null) {
10411045
return value;
10421046
} else {
1043-
return getParentValue(parentPages.getParent(), pdfName);
1047+
return getInheritedValue(parentPages.getParent(), pdfName);
10441048
}
10451049
}
10461050
return null;
@@ -1140,28 +1144,29 @@ private void flushMustBeIndirectObject(PdfObject obj) {
11401144
obj.makeIndirect(getDocument()).flush();
11411145
}
11421146

1143-
/*
1144-
* initialization <code>parentPages</code> if needed
1145-
*/
1146-
private void initParentPages() {
1147-
if (this.parentPages == null) {
1148-
this.parentPages = getDocument().getCatalog().getPageTree().findPageParent(this);
1149-
}
1150-
}
1151-
11521147
private void copyInheritedProperties(PdfPage copyPdfPage, PdfDocument pdfDocument) {
11531148
if (copyPdfPage.getPdfObject().get(PdfName.Resources) == null) {
11541149
PdfObject copyResource = pdfDocument.getWriter().copyObject(getResources().getPdfObject(), pdfDocument, false);
11551150
copyPdfPage.getPdfObject().put(PdfName.Resources, copyResource);
11561151
}
11571152
if (copyPdfPage.getPdfObject().get(PdfName.MediaBox) == null) {
1153+
//media box shall be in any case
11581154
copyPdfPage.setMediaBox(getMediaBox());
11591155
}
11601156
if (copyPdfPage.getPdfObject().get(PdfName.CropBox) == null) {
1161-
initParentPages();
1162-
PdfArray cropBox = (PdfArray) getParentValue(parentPages, PdfName.CropBox);
1157+
//original pdfObject don't have CropBox, otherwise copyPdfPage will contain it
1158+
PdfArray cropBox = (PdfArray) getInheritedValue(PdfName.CropBox, PdfObject.ARRAY);
1159+
//crop box is optional, we shall not set default value.
11631160
if (cropBox != null) {
1164-
copyPdfPage.setCropBox(cropBox.toRectangle());
1161+
copyPdfPage.put(PdfName.CropBox, cropBox);
1162+
}
1163+
}
1164+
if (copyPdfPage.getPdfObject().get(PdfName.Rotate) == null) {
1165+
//original pdfObject don't have Rotate, otherwise copyPdfPage will contain it
1166+
PdfNumber rotate = (PdfNumber) getInheritedValue(PdfName.Rotate, PdfObject.NUMBER);
1167+
//rotate is optional, we shall not set default value.
1168+
if (rotate != null) {
1169+
copyPdfPage.put(PdfName.Rotate, rotate);
11651170
}
11661171
}
11671172
}

0 commit comments

Comments
 (0)