|
1 | 1 | /*
|
2 | 2 |
|
3 | 3 | 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 |
5 | 5 | Authors: Bruno Lowagie, Paulo Soares, et al.
|
6 | 6 |
|
7 | 7 | This program is free software; you can redistribute it and/or modify
|
@@ -147,8 +147,7 @@ public int getRotation() {
|
147 | 147 | PdfNumber rotate = getPdfObject().getAsNumber(PdfName.Rotate);
|
148 | 148 | int rotateValue = 0;
|
149 | 149 | if (rotate == null) {
|
150 |
| - initParentPages(); |
151 |
| - rotate = (PdfNumber) getParentValue(this.parentPages, PdfName.Rotate); |
| 150 | + rotate = (PdfNumber) getInheritedValue(PdfName.Rotate, PdfObject.NUMBER); |
152 | 151 | }
|
153 | 152 | if (rotate != null) {
|
154 | 153 | rotateValue = rotate.intValue();
|
@@ -271,8 +270,7 @@ public PdfResources getResources() {
|
271 | 270 | boolean readOnly = false;
|
272 | 271 | PdfDictionary resources = getPdfObject().getAsDictionary(PdfName.Resources);
|
273 | 272 | if (resources == null) {
|
274 |
| - initParentPages(); |
275 |
| - resources = (PdfDictionary) getParentValue(this.parentPages, PdfName.Resources); |
| 273 | + resources = (PdfDictionary) getInheritedValue(PdfName.Resources, PdfObject.DICTIONARY); |
276 | 274 | if (resources != null) {
|
277 | 275 | readOnly = true;
|
278 | 276 | }
|
@@ -509,10 +507,9 @@ public void flush(boolean flushResourcesContentStreams) {
|
509 | 507 | * @throws PdfException in case of any error while reading MediaBox object.
|
510 | 508 | */
|
511 | 509 | public Rectangle getMediaBox() {
|
512 |
| - initParentPages(); |
513 | 510 | PdfArray mediaBox = getPdfObject().getAsArray(PdfName.MediaBox);
|
514 | 511 | if (mediaBox == null) {
|
515 |
| - mediaBox = (PdfArray) getParentValue(parentPages, PdfName.MediaBox); |
| 512 | + mediaBox = (PdfArray) getInheritedValue(PdfName.MediaBox, PdfObject.ARRAY); |
516 | 513 | }
|
517 | 514 | if (mediaBox == null) {
|
518 | 515 | throw new PdfException(PdfException.CannotRetrieveMediaBoxAttribute);
|
@@ -551,13 +548,12 @@ public PdfPage setMediaBox(Rectangle rectangle) {
|
551 | 548 | * and then shall be imposed on the output medium in some implementation-defined manner.
|
552 | 549 | *
|
553 | 550 | * @return the {@link Rectangle} object specified by pages's CropBox, expressed in default user space units.
|
554 |
| - * MediaBox by default. |
| 551 | + * MediaBox by default. |
555 | 552 | */
|
556 | 553 | public Rectangle getCropBox() {
|
557 |
| - initParentPages(); |
558 | 554 | PdfArray cropBox = getPdfObject().getAsArray(PdfName.CropBox);
|
559 | 555 | if (cropBox == null) {
|
560 |
| - cropBox = (PdfArray) getParentValue(parentPages, PdfName.CropBox); |
| 556 | + cropBox = (PdfArray) getInheritedValue(PdfName.CropBox, PdfObject.ARRAY); |
561 | 557 | if (cropBox == null) {
|
562 | 558 | return getMediaBox();
|
563 | 559 | }
|
@@ -1033,14 +1029,22 @@ private PdfArray getAnnots(boolean create) {
|
1033 | 1029 | return annots;
|
1034 | 1030 | }
|
1035 | 1031 |
|
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) { |
1037 | 1041 | if (parentPages != null) {
|
1038 | 1042 | PdfDictionary parentDictionary = parentPages.getPdfObject();
|
1039 | 1043 | PdfObject value = parentDictionary.get(pdfName);
|
1040 | 1044 | if (value != null) {
|
1041 | 1045 | return value;
|
1042 | 1046 | } else {
|
1043 |
| - return getParentValue(parentPages.getParent(), pdfName); |
| 1047 | + return getInheritedValue(parentPages.getParent(), pdfName); |
1044 | 1048 | }
|
1045 | 1049 | }
|
1046 | 1050 | return null;
|
@@ -1140,28 +1144,29 @@ private void flushMustBeIndirectObject(PdfObject obj) {
|
1140 | 1144 | obj.makeIndirect(getDocument()).flush();
|
1141 | 1145 | }
|
1142 | 1146 |
|
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 |
| - |
1152 | 1147 | private void copyInheritedProperties(PdfPage copyPdfPage, PdfDocument pdfDocument) {
|
1153 | 1148 | if (copyPdfPage.getPdfObject().get(PdfName.Resources) == null) {
|
1154 | 1149 | PdfObject copyResource = pdfDocument.getWriter().copyObject(getResources().getPdfObject(), pdfDocument, false);
|
1155 | 1150 | copyPdfPage.getPdfObject().put(PdfName.Resources, copyResource);
|
1156 | 1151 | }
|
1157 | 1152 | if (copyPdfPage.getPdfObject().get(PdfName.MediaBox) == null) {
|
| 1153 | + //media box shall be in any case |
1158 | 1154 | copyPdfPage.setMediaBox(getMediaBox());
|
1159 | 1155 | }
|
1160 | 1156 | 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. |
1163 | 1160 | 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); |
1165 | 1170 | }
|
1166 | 1171 | }
|
1167 | 1172 | }
|
|
0 commit comments