コード例 #1
0
ファイル: PdfStream.cs プロジェクト: aakkssqq/CreatePDFCSharp
        // methods

        /**
         * Compresses the stream.
         *
         * @throws PdfException if a filter is allready defined
         */

        public void flateCompress()
        {
            if (!Document.compress)
            {
                return;
            }
            // check if the flateCompress-method has allready been
            if (compressed)
            {
                return;
            }
            // check if a filter allready exists
            PdfObject filter = get(PdfName.FILTER);

            if (filter != null)
            {
                if (filter.isName() && ((PdfName)filter).CompareTo(PdfName.FLATEDECODE) == 0)
                {
                    return;
                }
                else if (filter.isArray() && ((PdfArray)filter).contains(PdfName.FLATEDECODE))
                {
                    return;
                }
                else
                {
                    throw new PdfException("Stream could not be compressed: filter is not a name or array.");
                }
            }
            try {
                // compress
                MemoryStream         stream = new MemoryStream();
                DeflaterOutputStream zip    = new DeflaterOutputStream(stream);
                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;
            }
            catch (IOException ioe) {
                throw ioe;
            }
        }