Skip to content

Commit 0854f99

Browse files
committed
Fix possible issues with PdfPage's inherited properties
DEVSIX-1704
1 parent 54de9ee commit 0854f99

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

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

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ protected PdfPage(PdfDictionary pdfObject) {
101101
}
102102

103103
protected PdfPage(PdfDocument pdfDocument, PageSize pageSize) {
104-
this((PdfDictionary)new PdfDictionary().makeIndirect(pdfDocument));
105-
PdfStream contentStream = (PdfStream)new PdfStream().makeIndirect(pdfDocument);
104+
this((PdfDictionary) new PdfDictionary().makeIndirect(pdfDocument));
105+
PdfStream contentStream = (PdfStream) new PdfStream().makeIndirect(pdfDocument);
106106
getPdfObject().put(PdfName.Contents, contentStream);
107107
getPdfObject().put(PdfName.Type, PdfName.Page);
108108
getPdfObject().put(PdfName.MediaBox, new PdfArray(pageSize));
@@ -150,8 +150,7 @@ public int getRotation() {
150150
PdfNumber rotate = getPdfObject().getAsNumber(PdfName.Rotate);
151151
int rotateValue = 0;
152152
if (rotate == null) {
153-
initParentPages();
154-
rotate = (PdfNumber) getParentValue(this.parentPages, PdfName.Rotate);
153+
rotate = (PdfNumber) getInheritedValue(PdfName.Rotate, PdfObject.NUMBER);
155154
}
156155
if (rotate != null) {
157156
rotateValue = rotate.intValue();
@@ -274,8 +273,7 @@ public PdfResources getResources() {
274273
boolean readOnly = false;
275274
PdfDictionary resources = getPdfObject().getAsDictionary(PdfName.Resources);
276275
if (resources == null) {
277-
initParentPages();
278-
resources = (PdfDictionary) getParentValue(this.parentPages, PdfName.Resources);
276+
resources = (PdfDictionary) getInheritedValue(PdfName.Resources, PdfObject.DICTIONARY);
279277
if (resources != null) {
280278
readOnly = true;
281279
}
@@ -515,10 +513,9 @@ public void flush(boolean flushResourcesContentStreams) {
515513
* @throws PdfException in case of any error while reading MediaBox object.
516514
*/
517515
public Rectangle getMediaBox() {
518-
initParentPages();
519516
PdfArray mediaBox = getPdfObject().getAsArray(PdfName.MediaBox);
520517
if (mediaBox == null) {
521-
mediaBox = (PdfArray) getParentValue(parentPages, PdfName.MediaBox);
518+
mediaBox = (PdfArray) getInheritedValue(PdfName.MediaBox, PdfObject.ARRAY);
522519
}
523520
if (mediaBox == null) {
524521
throw new PdfException(PdfException.CannotRetrieveMediaBoxAttribute);
@@ -560,10 +557,9 @@ public PdfPage setMediaBox(Rectangle rectangle) {
560557
* MediaBox by default.
561558
*/
562559
public Rectangle getCropBox() {
563-
initParentPages();
564560
PdfArray cropBox = getPdfObject().getAsArray(PdfName.CropBox);
565561
if (cropBox == null) {
566-
cropBox = (PdfArray) getParentValue(parentPages, PdfName.CropBox);
562+
cropBox = (PdfArray) getInheritedValue(PdfName.CropBox, PdfObject.ARRAY);
567563
if (cropBox == null) {
568564
return getMediaBox();
569565
}
@@ -882,7 +878,6 @@ public int getAnnotsSize() {
882878
/**
883879
* This method gets outlines of a current page
884880
*
885-
* @param updateOutlines
886881
* @return return all outlines of a current page
887882
*/
888883
public List<PdfOutline> getOutlines(boolean updateOutlines) {
@@ -1155,14 +1150,22 @@ private PdfArray getAnnots(boolean create) {
11551150
return annots;
11561151
}
11571152

1158-
private PdfObject getParentValue(PdfPages parentPages, PdfName pdfName) {
1153+
private PdfObject getInheritedValue(PdfName pdfName, int type) {
1154+
if (this.parentPages == null) {
1155+
this.parentPages = getDocument().getCatalog().getPageTree().findPageParent(this);
1156+
}
1157+
PdfObject val = getInheritedValue(this.parentPages, pdfName);
1158+
return val != null && val.getType() == type ? val : null;
1159+
}
1160+
1161+
private static PdfObject getInheritedValue(PdfPages parentPages, PdfName pdfName) {
11591162
if (parentPages != null) {
11601163
PdfDictionary parentDictionary = parentPages.getPdfObject();
11611164
PdfObject value = parentDictionary.get(pdfName);
11621165
if (value != null) {
11631166
return value;
11641167
} else {
1165-
return getParentValue(parentPages.getParent(), pdfName);
1168+
return getInheritedValue(parentPages.getParent(), pdfName);
11661169
}
11671170
}
11681171
return null;
@@ -1262,28 +1265,29 @@ private void flushMustBeIndirectObject(PdfObject obj) {
12621265
obj.makeIndirect(getDocument()).flush();
12631266
}
12641267

1265-
/*
1266-
* initialization <code>parentPages</code> if needed
1267-
*/
1268-
private void initParentPages() {
1269-
if (this.parentPages == null) {
1270-
this.parentPages = getDocument().getCatalog().getPageTree().findPageParent(this);
1271-
}
1272-
}
1273-
12741268
private void copyInheritedProperties(PdfPage copyPdfPage, PdfDocument pdfDocument) {
12751269
if (copyPdfPage.getPdfObject().get(PdfName.Resources) == null) {
12761270
PdfObject copyResource = pdfDocument.getWriter().copyObject(getResources().getPdfObject(), pdfDocument, false);
12771271
copyPdfPage.getPdfObject().put(PdfName.Resources, copyResource);
12781272
}
12791273
if (copyPdfPage.getPdfObject().get(PdfName.MediaBox) == null) {
1274+
//media box shall be in any case
12801275
copyPdfPage.setMediaBox(getMediaBox());
12811276
}
12821277
if (copyPdfPage.getPdfObject().get(PdfName.CropBox) == null) {
1283-
initParentPages();
1284-
PdfArray cropBox = (PdfArray) getParentValue(parentPages, PdfName.CropBox);
1278+
//original pdfObject don't have CropBox, otherwise copyPdfPage will contain it
1279+
PdfArray cropBox = (PdfArray) getInheritedValue(PdfName.CropBox, PdfObject.ARRAY);
1280+
//crop box is optional, we shall not set default value.
12851281
if (cropBox != null) {
1286-
copyPdfPage.setCropBox(cropBox.toRectangle());
1282+
copyPdfPage.put(PdfName.CropBox, cropBox);
1283+
}
1284+
}
1285+
if (copyPdfPage.getPdfObject().get(PdfName.Rotate) == null) {
1286+
//original pdfObject don't have Rotate, otherwise copyPdfPage will contain it
1287+
PdfNumber rotate = (PdfNumber) getInheritedValue(PdfName.Rotate, PdfObject.NUMBER);
1288+
//rotate is optional, we shall not set default value.
1289+
if (rotate != null) {
1290+
copyPdfPage.put(PdfName.Rotate, rotate);
12871291
}
12881292
}
12891293
}

0 commit comments

Comments
 (0)