/** * Compresses the stream. * @param compressionLevel the compression level (0 = best speed, 9 = best compression, -1 is default) * @since 2.1.3 */ public void FlateCompress(int compressionLevel) { if (!Document.Compress) return; // check if the flateCompress-method has allready been if (compressed) { return; } this.compressionLevel = compressionLevel; if (inputStream != null) { compressed = true; return; } // check if a filter allready exists PdfObject filter = PdfReader.GetPdfObject(Get(PdfName.FILTER)); if (filter != null) { if (filter.IsName()) { if (PdfName.FLATEDECODE.Equals(filter)) return; } else if (filter.IsArray()) { if (((PdfArray) filter).Contains(PdfName.FLATEDECODE)) return; } else { throw new PdfException("Stream could not be compressed: filter is not a name or array."); } } // compress MemoryStream stream = new MemoryStream(); var zip = new ZOutputStream(stream, compressionLevel); if (streamBytes != null) streamBytes.WriteTo(zip); else zip.Write(bytes, 0, bytes.Length); //zip.Close(); zip.Finish(); // update the object streamBytes = stream; bytes = null; Put(PdfName.LENGTH, new PdfNumber(streamBytes.Length)); if (filter == null) { Put(PdfName.FILTER, PdfName.FLATEDECODE); } else { PdfArray filters = new PdfArray(filter); filters.Add(PdfName.FLATEDECODE); Put(PdfName.FILTER, filters); } compressed = true; }
private static void EncryptWithCompressionInternal(DocumentHeaders outputDocumentHeaders, Stream inputStream, CryptoStream encryptingStream, ProgressContext progress) { using (ZOutputStream deflatingStream = new ZOutputStream(encryptingStream, -1)) { deflatingStream.FlushMode = JZlib.Z_SYNC_FLUSH; CopyToWithCount(inputStream, deflatingStream, progress); deflatingStream.FlushMode = JZlib.Z_FINISH; deflatingStream.Finish(); outputDocumentHeaders.UncompressedLength = deflatingStream.TotalIn; outputDocumentHeaders.PlaintextLength = deflatingStream.TotalOut; } }