private static PdfObject getInheritedValue(PdfPages parentPages, PdfName pdfName) { if (parentPages != null) { PdfDictionary parentDictionary = parentPages.getPdfObject(); PdfObject value = parentDictionary.get(pdfName); if (value != null) { return value; } else { return getInheritedValue(parentPages.getParent(), pdfName); } } return null; }
/** * Gets the number of degrees by which the page shall be rotated clockwise when displayed or printed. * Shall be a multiple of 90. * * @return {@code int} number of degrees. Default value: 0 */ public int getRotation() { PdfNumber rotate = getPdfObject().getAsNumber(PdfName.Rotate); int rotateValue = 0; if (rotate == null) { rotate = (PdfNumber) getInheritedValue(PdfName.Rotate, PdfObject.NUMBER); } if (rotate != null) { rotateValue = rotate.intValue(); } rotateValue %= 360; return rotateValue < 0 ? rotateValue + 360 : rotateValue; }
/** * Gets the {@link Rectangle} specified by page's CropBox, that defines the visible region of default user space. * When the page is displayed or printed, its contents shall be clipped (cropped) to this rectangle * and then shall be imposed on the output medium in some implementation-defined manner. * * @return the {@link Rectangle} object specified by pages's CropBox, expressed in default user space units. * MediaBox by default. */ public Rectangle getCropBox() { PdfArray cropBox = getPdfObject().getAsArray(PdfName.CropBox); if (cropBox == null) { cropBox = (PdfArray) getInheritedValue(PdfName.CropBox, PdfObject.ARRAY); if (cropBox == null) { return getMediaBox(); } } return cropBox.toRectangle(); }
private PdfObject getInheritedValue(PdfName pdfName, int type) { if (this.parentPages == null) { this.parentPages = getDocument().getCatalog().getPageTree().findPageParent(this); } PdfObject val = getInheritedValue(this.parentPages, pdfName); return val != null && val.getType() == type ? val : null; }
PdfDictionary resources = getPdfObject().getAsDictionary(PdfName.Resources); if (resources == null) { resources = (PdfDictionary) getInheritedValue(PdfName.Resources, PdfObject.DICTIONARY); if (resources != null) { readOnly = true;
/** * Gets {@link Rectangle} object specified by page's Media Box, that defines the boundaries of the physical medium * on which the page shall be displayed or printed * * @return {@link Rectangle} object specified by page Media Box, expressed in default user space units. * @throws PdfException in case of any error while reading MediaBox object. */ public Rectangle getMediaBox() { PdfArray mediaBox = getPdfObject().getAsArray(PdfName.MediaBox); if (mediaBox == null) { mediaBox = (PdfArray) getInheritedValue(PdfName.MediaBox, PdfObject.ARRAY); } if (mediaBox == null) { throw new PdfException(PdfException.CannotRetrieveMediaBoxAttribute); } if (mediaBox.size() != 4) { throw new PdfException(PdfException.WrongMediaBoxSize1).setMessageParams(mediaBox.size()); } PdfNumber llx = mediaBox.getAsNumber(0); PdfNumber lly = mediaBox.getAsNumber(1); PdfNumber urx = mediaBox.getAsNumber(2); PdfNumber ury = mediaBox.getAsNumber(3); if (llx == null || lly == null || urx == null || ury == null) { throw new PdfException(PdfException.InvalidMediaBoxValue); } return new Rectangle(Math.min(llx.floatValue(), urx.floatValue()), Math.min(lly.floatValue(), ury.floatValue()), Math.abs(urx.floatValue() - llx.floatValue()), Math.abs(ury.floatValue() - lly.floatValue())); }
private void copyInheritedProperties(PdfPage copyPdfPage, PdfDocument pdfDocument) { if (copyPdfPage.getPdfObject().get(PdfName.Resources) == null) { PdfObject copyResource = pdfDocument.getWriter().copyObject(getResources().getPdfObject(), pdfDocument, false); copyPdfPage.getPdfObject().put(PdfName.Resources, copyResource); } if (copyPdfPage.getPdfObject().get(PdfName.MediaBox) == null) { //media box shall be in any case copyPdfPage.setMediaBox(getMediaBox()); } if (copyPdfPage.getPdfObject().get(PdfName.CropBox) == null) { //original pdfObject don't have CropBox, otherwise copyPdfPage will contain it PdfArray cropBox = (PdfArray) getInheritedValue(PdfName.CropBox, PdfObject.ARRAY); //crop box is optional, we shall not set default value. if (cropBox != null) { copyPdfPage.put(PdfName.CropBox, cropBox.copyTo(pdfDocument)); } } if (copyPdfPage.getPdfObject().get(PdfName.Rotate) == null) { //original pdfObject don't have Rotate, otherwise copyPdfPage will contain it PdfNumber rotate = (PdfNumber) getInheritedValue(PdfName.Rotate, PdfObject.NUMBER); //rotate is optional, we shall not set default value. if (rotate != null) { copyPdfPage.put(PdfName.Rotate, rotate.copyTo(pdfDocument)); } } }