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); }