protected internal virtual bool ContainsFlateFilter(PdfStream pdfStream) { PdfObject filter = pdfStream.Get(PdfName.Filter); if (filter != null) { if (filter.GetObjectType() == PdfObject.NAME) { if (PdfName.FlateDecode.Equals(filter)) { return(true); } } else { if (filter.GetObjectType() == PdfObject.ARRAY) { if (((PdfArray)filter).Contains(PdfName.FlateDecode)) { return(true); } } else { throw new PdfException(PdfException.FilterIsNotANameOrArray); } } } return(false); }
protected internal virtual bool CheckEncryption(PdfStream pdfStream) { if (crypto == null || crypto.IsEmbeddedFilesOnly()) { return(false); } else { PdfObject filter = pdfStream.Get(PdfName.Filter, true); if (filter != null) { if (PdfName.Crypt.Equals(filter)) { return(false); } else { if (filter.GetObjectType() == PdfObject.ARRAY) { PdfArray filters = (PdfArray)filter; if (!filters.IsEmpty() && PdfName.Crypt.Equals(filters.Get(0, true))) { return(false); } } } } return(true); } }
protected internal virtual 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 is PdfArray) { filters.AddAll((PdfArray)filter); } else { filters.Add(filter); } PdfObject decodeParms = pdfStream.Get(PdfName.DecodeParms); if (decodeParms != null) { if (decodeParms is PdfDictionary) { PdfArray array = new PdfArray(); array.Add(new PdfNull()); array.Add(decodeParms); pdfStream.Put(PdfName.DecodeParms, array); } else { if (decodeParms is PdfArray) { ((PdfArray)decodeParms).Add(0, new PdfNull()); } else { throw new PdfException(PdfException.DecodeParameterType1IsNotSupported).SetMessageParams(decodeParms.GetType ().ToString()); } } } pdfStream.Put(PdfName.Filter, filters); } }
protected internal virtual byte[] DecodeFlateBytes(PdfStream stream, byte[] bytes) { PdfObject filterObject = stream.Get(PdfName.Filter); if (filterObject == null) { return(bytes); } // check if flateDecode filter is on top PdfName filterName; PdfArray filtersArray = null; if (filterObject is PdfName) { filterName = (PdfName)filterObject; } else { if (filterObject is PdfArray) { filtersArray = (PdfArray)filterObject; filterName = filtersArray.GetAsName(0); } else { throw new PdfException(PdfException.FilterIsNotANameOrArray); } } if (!PdfName.FlateDecode.Equals(filterName)) { return(bytes); } // get decode params if present PdfDictionary decodeParams; PdfArray decodeParamsArray = null; PdfObject decodeParamsObject = stream.Get(PdfName.DecodeParms); if (decodeParamsObject == null) { decodeParams = null; } else { if (decodeParamsObject.GetObjectType() == PdfObject.DICTIONARY) { decodeParams = (PdfDictionary)decodeParamsObject; } else { if (decodeParamsObject.GetObjectType() == PdfObject.ARRAY) { decodeParamsArray = (PdfArray)decodeParamsObject; decodeParams = decodeParamsArray.GetAsDictionary(0); } else { throw new PdfException(PdfException.DecodeParameterType1IsNotSupported).SetMessageParams(decodeParamsObject .GetType().ToString()); } } } // decode byte[] res = FlateDecodeFilter.FlateDecode(bytes, true); if (res == null) { res = FlateDecodeFilter.FlateDecode(bytes, false); } bytes = FlateDecodeFilter.DecodePredictor(res, decodeParams); //remove filter and decode params filterObject = null; if (filtersArray != null) { filtersArray.Remove(0); if (filtersArray.Size() == 1) { filterObject = filtersArray.Get(0); } else { if (!filtersArray.IsEmpty()) { filterObject = filtersArray; } } } decodeParamsObject = null; if (decodeParamsArray != null) { decodeParamsArray.Remove(0); if (decodeParamsArray.Size() == 1 && decodeParamsArray.Get(0).GetObjectType() != PdfObject.NULL) { decodeParamsObject = decodeParamsArray.Get(0); } else { if (!decodeParamsArray.IsEmpty()) { decodeParamsObject = decodeParamsArray; } } } if (filterObject == null) { stream.Remove(PdfName.Filter); } else { stream.Put(PdfName.Filter, filterObject); } if (decodeParamsObject == null) { stream.Remove(PdfName.DecodeParms); } else { stream.Put(PdfName.DecodeParms, decodeParamsObject); } return(bytes); }