@@ -101,8 +101,8 @@ protected PdfPage(PdfDictionary pdfObject) {
101
101
}
102
102
103
103
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 );
106
106
getPdfObject ().put (PdfName .Contents , contentStream );
107
107
getPdfObject ().put (PdfName .Type , PdfName .Page );
108
108
getPdfObject ().put (PdfName .MediaBox , new PdfArray (pageSize ));
@@ -150,8 +150,7 @@ public int getRotation() {
150
150
PdfNumber rotate = getPdfObject ().getAsNumber (PdfName .Rotate );
151
151
int rotateValue = 0 ;
152
152
if (rotate == null ) {
153
- initParentPages ();
154
- rotate = (PdfNumber ) getParentValue (this .parentPages , PdfName .Rotate );
153
+ rotate = (PdfNumber ) getInheritedValue (PdfName .Rotate , PdfObject .NUMBER );
155
154
}
156
155
if (rotate != null ) {
157
156
rotateValue = rotate .intValue ();
@@ -274,8 +273,7 @@ public PdfResources getResources() {
274
273
boolean readOnly = false ;
275
274
PdfDictionary resources = getPdfObject ().getAsDictionary (PdfName .Resources );
276
275
if (resources == null ) {
277
- initParentPages ();
278
- resources = (PdfDictionary ) getParentValue (this .parentPages , PdfName .Resources );
276
+ resources = (PdfDictionary ) getInheritedValue (PdfName .Resources , PdfObject .DICTIONARY );
279
277
if (resources != null ) {
280
278
readOnly = true ;
281
279
}
@@ -515,10 +513,9 @@ public void flush(boolean flushResourcesContentStreams) {
515
513
* @throws PdfException in case of any error while reading MediaBox object.
516
514
*/
517
515
public Rectangle getMediaBox () {
518
- initParentPages ();
519
516
PdfArray mediaBox = getPdfObject ().getAsArray (PdfName .MediaBox );
520
517
if (mediaBox == null ) {
521
- mediaBox = (PdfArray ) getParentValue ( parentPages , PdfName .MediaBox );
518
+ mediaBox = (PdfArray ) getInheritedValue ( PdfName .MediaBox , PdfObject . ARRAY );
522
519
}
523
520
if (mediaBox == null ) {
524
521
throw new PdfException (PdfException .CannotRetrieveMediaBoxAttribute );
@@ -560,10 +557,9 @@ public PdfPage setMediaBox(Rectangle rectangle) {
560
557
* MediaBox by default.
561
558
*/
562
559
public Rectangle getCropBox () {
563
- initParentPages ();
564
560
PdfArray cropBox = getPdfObject ().getAsArray (PdfName .CropBox );
565
561
if (cropBox == null ) {
566
- cropBox = (PdfArray ) getParentValue ( parentPages , PdfName .CropBox );
562
+ cropBox = (PdfArray ) getInheritedValue ( PdfName .CropBox , PdfObject . ARRAY );
567
563
if (cropBox == null ) {
568
564
return getMediaBox ();
569
565
}
@@ -882,7 +878,6 @@ public int getAnnotsSize() {
882
878
/**
883
879
* This method gets outlines of a current page
884
880
*
885
- * @param updateOutlines
886
881
* @return return all outlines of a current page
887
882
*/
888
883
public List <PdfOutline > getOutlines (boolean updateOutlines ) {
@@ -1155,14 +1150,22 @@ private PdfArray getAnnots(boolean create) {
1155
1150
return annots ;
1156
1151
}
1157
1152
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 ) {
1159
1162
if (parentPages != null ) {
1160
1163
PdfDictionary parentDictionary = parentPages .getPdfObject ();
1161
1164
PdfObject value = parentDictionary .get (pdfName );
1162
1165
if (value != null ) {
1163
1166
return value ;
1164
1167
} else {
1165
- return getParentValue (parentPages .getParent (), pdfName );
1168
+ return getInheritedValue (parentPages .getParent (), pdfName );
1166
1169
}
1167
1170
}
1168
1171
return null ;
@@ -1262,28 +1265,29 @@ private void flushMustBeIndirectObject(PdfObject obj) {
1262
1265
obj .makeIndirect (getDocument ()).flush ();
1263
1266
}
1264
1267
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
-
1274
1268
private void copyInheritedProperties (PdfPage copyPdfPage , PdfDocument pdfDocument ) {
1275
1269
if (copyPdfPage .getPdfObject ().get (PdfName .Resources ) == null ) {
1276
1270
PdfObject copyResource = pdfDocument .getWriter ().copyObject (getResources ().getPdfObject (), pdfDocument , false );
1277
1271
copyPdfPage .getPdfObject ().put (PdfName .Resources , copyResource );
1278
1272
}
1279
1273
if (copyPdfPage .getPdfObject ().get (PdfName .MediaBox ) == null ) {
1274
+ //media box shall be in any case
1280
1275
copyPdfPage .setMediaBox (getMediaBox ());
1281
1276
}
1282
1277
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.
1285
1281
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 );
1287
1291
}
1288
1292
}
1289
1293
}
0 commit comments