private boolean compareStreamsExtended(PdfStream outStream, PdfStream cmpStream, ObjectPath currentPath, CompareResult compareResult) { boolean toDecode = PdfName.FlateDecode.equals(outStream.get(PdfName.Filter)); byte[] outStreamBytes = outStream.getBytes(toDecode); byte[] cmpStreamBytes = cmpStream.getBytes(toDecode); if (Arrays.equals(outStreamBytes, cmpStreamBytes)) { return compareDictionariesExtended(outStream, cmpStream, currentPath, compareResult); } else { StringBuilder errorMessage = new StringBuilder(); if (cmpStreamBytes.length != outStreamBytes.length) { errorMessage.append(MessageFormatUtil.format("PdfStream. Lengths are different. Expected: {0}. Found: {1}\n", cmpStreamBytes.length, outStreamBytes.length)); } else { errorMessage.append("PdfStream. Bytes are different.\n"); } int firstDifferenceOffset = findBytesDifference(outStreamBytes, cmpStreamBytes, errorMessage); if (compareResult != null && currentPath != null) { currentPath.pushOffsetToPath(firstDifferenceOffset); compareResult.addError(currentPath, errorMessage.toString()); currentPath.pop(); } return false; } }
/** * To manually flush a {@code PdfObject} behind this wrapper, you have to ensure * that this object is added to the document, i.e. it has an indirect reference. * Basically this means that before flushing you need to explicitly call {@link #makeIndirect(PdfDocument)}. * For example: wrapperInstance.makeIndirect(document).flush(); * Note that not every wrapper require this, only those that have such warning in documentation. */ @Override public void flush() { resources = null; if (getPdfObject().get(PdfName.BBox) == null) { throw new PdfException(PdfException.FormXObjectMustHaveBbox); } super.flush(); }
@Override protected void checkImage(PdfStream image, PdfDictionary currentColorSpaces) { PdfColorSpace colorSpace = null; if (isAlreadyChecked(image)) { colorSpace = checkedObjectsColorspace.get(image); checkColorSpace(colorSpace, currentColorSpaces, true, null); return; } PdfObject colorSpaceObj = image.get(PdfName.ColorSpace); if (colorSpaceObj != null) { colorSpace = PdfColorSpace.makeColorSpace(colorSpaceObj); checkColorSpace(colorSpace, currentColorSpaces, true, null); checkedObjectsColorspace.put(image, colorSpace); } if (image.containsKey(PdfName.Alternates)) { throw new PdfAConformanceException(PdfAConformanceException.AN_IMAGE_DICTIONARY_SHALL_NOT_CONTAIN_ALTERNATES_KEY); } if (image.containsKey(PdfName.OPI)) { throw new PdfAConformanceException(PdfAConformanceException.AN_IMAGE_DICTIONARY_SHALL_NOT_CONTAIN_OPI_KEY); } if (image.containsKey(PdfName.Interpolate) && (boolean) image.getAsBool(PdfName.Interpolate)) { throw new PdfAConformanceException(PdfAConformanceException.THE_VALUE_OF_INTERPOLATE_KEY_SHALL_BE_FALSE); } checkRenderingIntent(image.getAsName(PdfName.Intent)); if (image.containsKey(PdfName.SMask) && !PdfName.None.equals(image.getAsName(PdfName.SMask))) { throw new PdfAConformanceException(PdfAConformanceException.THE_SMASK_KEY_IS_NOT_ALLOWED_IN_XOBJECTS); } }
PdfObject filter = stream.get(PdfName.Filter); if (filter instanceof PdfName) { if (filter.equals(PdfName.LZWDecode))
boolean isFlushed = xObjStream.isFlushed(); if (!isFlushed) { subtype = xObjStream.get(PdfName.Subtype);
PdfObject filter = stream.get(PdfName.Filter); if (filter instanceof PdfName) { if (filter.equals(PdfName.LZWDecode))
@Override protected void checkPdfStream(PdfStream stream) { if (stream.containsKey(PdfName.F) || stream.containsKey(PdfName.FFilter) || stream.containsKey(PdfName.FDecodeParams)) { throw new PdfAConformanceException(PdfAConformanceException.STREAM_OBJECT_DICTIONARY_SHALL_NOT_CONTAIN_THE_F_FFILTER_OR_FDECODEPARAMS_KEYS); } PdfObject filter = stream.get(PdfName.Filter); if (filter instanceof PdfName) { if (filter.equals(PdfName.LZWDecode)) throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); } else if (filter instanceof PdfArray) { for (PdfObject f : ((PdfArray) filter)) { if (f.equals(PdfName.LZWDecode)) throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); } } }
@Override protected void checkImage(PdfStream image, PdfDictionary currentColorSpaces) { PdfColorSpace colorSpace = null; if (isAlreadyChecked(image)) { colorSpace = checkedObjectsColorspace.get(image); checkColorSpace(colorSpace, currentColorSpaces, true, null); return; } PdfObject colorSpaceObj = image.get(PdfName.ColorSpace); if (colorSpaceObj != null) { colorSpace = PdfColorSpace.makeColorSpace(colorSpaceObj); checkColorSpace(colorSpace, currentColorSpaces, true, null); checkedObjectsColorspace.put(image, colorSpace); } if (image.containsKey(PdfName.Alternates)) { throw new PdfAConformanceException(PdfAConformanceException.AN_IMAGE_DICTIONARY_SHALL_NOT_CONTAIN_ALTERNATES_KEY); } if (image.containsKey(PdfName.OPI)) { throw new PdfAConformanceException(PdfAConformanceException.AN_IMAGE_DICTIONARY_SHALL_NOT_CONTAIN_OPI_KEY); } if (image.containsKey(PdfName.Interpolate) && (boolean) image.getAsBool(PdfName.Interpolate)) { throw new PdfAConformanceException(PdfAConformanceException.THE_VALUE_OF_INTERPOLATE_KEY_SHALL_BE_FALSE); } checkRenderingIntent(image.getAsName(PdfName.Intent)); if (image.containsKey(PdfName.SMask) && !PdfName.None.equals(image.getAsName(PdfName.SMask))) { throw new PdfAConformanceException(PdfAConformanceException.THE_SMASK_KEY_IS_NOT_ALLOWED_IN_XOBJECTS); } }
@Override protected void checkPdfStream(PdfStream stream) { if (stream.containsKey(PdfName.F) || stream.containsKey(PdfName.FFilter) || stream.containsKey(PdfName.FDecodeParams)) { throw new PdfAConformanceException(PdfAConformanceException.STREAM_OBJECT_DICTIONARY_SHALL_NOT_CONTAIN_THE_F_FFILTER_OR_FDECODEPARAMS_KEYS); } PdfObject filter = stream.get(PdfName.Filter); if (filter instanceof PdfName) { if (filter.equals(PdfName.LZWDecode)) throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); } else if (filter instanceof PdfArray) { for (PdfObject f : ((PdfArray) filter)) { if (f.equals(PdfName.LZWDecode)) throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); } } }
@Override public boolean showPopupHook(MouseEvent event) { PdfTree tree = (PdfTree) component; int row = tree.getClosestRowForLocation(event.getX(), event.getY()); tree.setSelectionRow(row); TreeSelectionModel selectionModel = ((PdfTree) component).getSelectionModel(); TreePath[] paths = selectionModel.getSelectionPaths(); if (paths == null || paths.length < 1) { return false; } PdfObjectTreeNode lastPath = ((PdfObjectTreeNode) paths[0].getLastPathComponent()); if (!lastPath.isStream()) { return false; } // Make sure the selected item isn't an image PdfStream stream = (PdfStream) lastPath.getPdfObject(); return !PdfName.Image.equals(stream.get(PdfName.Subtype)); } }
@Override public void checkInlineImage(PdfStream inlineImage, PdfDictionary currentColorSpaces) { PdfObject filter = inlineImage.get(PdfName.Filter); if (filter instanceof PdfName) { if (filter.equals(PdfName.LZWDecode)) { throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); } } else if (filter instanceof PdfArray) { for (int i = 0; i < ((PdfArray) filter).size(); i++) { PdfName f = ((PdfArray) filter).getAsName(i); if (f.equals(PdfName.LZWDecode)) { throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); } } } checkImage(inlineImage, currentColorSpaces); }
@Override public void checkInlineImage(PdfStream inlineImage, PdfDictionary currentColorSpaces) { PdfObject filter = inlineImage.get(PdfName.Filter); if (filter instanceof PdfName) { if (filter.equals(PdfName.LZWDecode)) { throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); } } else if (filter instanceof PdfArray) { for (int i = 0; i < ((PdfArray) filter).size(); i++) { PdfName f = ((PdfArray) filter).getAsName(i); if (f.equals(PdfName.LZWDecode)) { throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); } } } checkImage(inlineImage, currentColorSpaces); }
protected boolean containsFlateFilter(PdfStream pdfStream) { PdfObject filter = pdfStream.get(PdfName.Filter); if (filter != null) { if (filter.getType() == PdfObject.NAME) { if (PdfName.FlateDecode.equals(filter)) { return true; } } else if (filter.getType() == PdfObject.ARRAY) { if (((PdfArray) filter).contains(PdfName.FlateDecode)) return true; } else { throw new PdfException(PdfException.FilterIsNotANameOrArray); } } return false; }
@Override public void checkInlineImage(PdfStream inlineImage, PdfDictionary currentColorSpaces) { PdfObject filter = inlineImage.get(PdfName.Filter); if (filter instanceof PdfName) { if (filter.equals(PdfName.LZWDecode)) throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); if (filter.equals(PdfName.Crypt)) { throw new PdfAConformanceException(PdfAConformanceException.CRYPT_FILTER_IS_NOT_PERMITTED_INLINE_IMAGE); } } else if (filter instanceof PdfArray) { for (int i = 0; i < ((PdfArray) filter).size(); i++) { PdfName f = ((PdfArray) filter).getAsName(i); if (f.equals(PdfName.LZWDecode)) throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); if (f.equals(PdfName.Crypt)) { throw new PdfAConformanceException(PdfAConformanceException.CRYPT_FILTER_IS_NOT_PERMITTED_INLINE_IMAGE); } } } checkImage(inlineImage, currentColorSpaces); }
@Override public void checkInlineImage(PdfStream inlineImage, PdfDictionary currentColorSpaces) { PdfObject filter = inlineImage.get(PdfName.Filter); if (filter instanceof PdfName) { if (filter.equals(PdfName.LZWDecode)) throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); if (filter.equals(PdfName.Crypt)) { throw new PdfAConformanceException(PdfAConformanceException.CRYPT_FILTER_IS_NOT_PERMITTED_INLINE_IMAGE); } } else if (filter instanceof PdfArray) { for (int i = 0; i < ((PdfArray) filter).size(); i++) { PdfName f = ((PdfArray) filter).getAsName(i); if (f.equals(PdfName.LZWDecode)) throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); if (f.equals(PdfName.Crypt)) { throw new PdfAConformanceException(PdfAConformanceException.CRYPT_FILTER_IS_NOT_PERMITTED_INLINE_IMAGE); } } } checkImage(inlineImage, currentColorSpaces); }
protected void updateCompressionFilter(PdfStream pdfStream) { PdfObject filter = pdfStream.get(PdfName.Filter); if (filter == null) { pdfStream.put(PdfName.Filter, PdfName.FlateDecode); } else { PdfArray filters = new PdfArray(); filters.add(PdfName.FlateDecode); if (filter instanceof PdfArray) { filters.addAll((PdfArray) filter); } else { filters.add(filter); } PdfObject decodeParms = pdfStream.get(PdfName.DecodeParms); if (decodeParms != null) { if (decodeParms instanceof PdfDictionary) { PdfArray array = new PdfArray(); array.add(new PdfNull()); array.add(decodeParms); pdfStream.put(PdfName.DecodeParms, array); } else if (decodeParms instanceof PdfArray) { ((PdfArray) decodeParms).add(0, new PdfNull()); } else { throw new PdfException(PdfException.DecodeParameterType1IsNotSupported).setMessageParams(decodeParms.getClass().toString()); } } pdfStream.put(PdfName.Filter, filters); } }
protected boolean checkEncryption(PdfStream pdfStream) { if (crypto == null || crypto.isEmbeddedFilesOnly()) { return false; } else if (isXRefStream(pdfStream)) { // The cross-reference stream shall not be encrypted return false; } else { PdfObject filter = pdfStream.get(PdfName.Filter, true); if (filter != null) { if (PdfName.Crypt.equals(filter)) { return false; } else if (filter.getType() == PdfObject.ARRAY) { PdfArray filters = (PdfArray) filter; if (!filters.isEmpty() && PdfName.Crypt.equals(filters.get(0, true))) { return false; } } } return true; } }
PdfObject params = embeddedFile.get(PdfName.Params); if (!params.isDictionary()) { throw new PdfAConformanceException(PdfAConformanceException.EMBEDDED_FILE_SHALL_CONTAIN_PARAMS_KEY_WITH_DICTIONARY_AS_VALUE);
PdfObject params = embeddedFile.get(PdfName.Params); if (!params.isDictionary()) { throw new PdfAConformanceException(PdfAConformanceException.EMBEDDED_FILE_SHALL_CONTAIN_PARAMS_KEY_WITH_DICTIONARY_AS_VALUE);