示例#1
0
        public override void WriteTo(
            IOutputStream stream,
            File context
            )
        {
            /*
             * NOTE: The header is temporarily tweaked to accommodate serialization settings.
             */
            header.Updateable = false;

            byte[] bodyData;
            {
                bool bodyUnencoded;
                {
                    FileSpecification dataFile = DataFile;

                    /*
                     * NOTE: In case of external file, the body buffer has to be saved back only if the file was
                     * actually resolved (that is brought into the body buffer) and modified.
                     */
                    bool encodeBody = (dataFile == null || (bodyResolved && body.Dirty));
                    if (encodeBody)
                    {
                        PdfDirectObject filterObject = Filter;
                        if (filterObject == null) // Unencoded body.
                        {
                            /*
                             * NOTE: Header entries related to stream body encoding are temporary, instrumental to
                             * the current serialization process only.
                             */
                            bodyUnencoded = true;

                            // Set the filter to apply!
                            filterObject = PdfName.FlateDecode; // zlib/deflate filter.
                            // Get encoded body data applying the filter to the stream!
                            bodyData = body.Encode(bytes.filters.Filter.Get((PdfName)filterObject), null);
                            // Set 'Filter' entry!
                            Filter = filterObject;
                        }
                        else // Encoded body.
                        {
                            bodyUnencoded = false;

                            // Get encoded body data!
                            bodyData = body.ToByteArray();
                        }

                        if (dataFile != null)
                        {
                            /*
                             * NOTE: In case of external file, body data has to be serialized there, leaving empty
                             * its representation within this stream.
                             */
                            try
                            {
                                IOutputStream dataFileOutputStream = dataFile.GetOutputStream();
                                dataFileOutputStream.Write(bodyData);
                                dataFileOutputStream.Dispose();
                            }
                            catch (Exception e)
                            { throw new Exception("Data writing into " + dataFile.Path + " failed.", e); }
                            // Local serialization is empty!
                            bodyData = new byte[] {};
                        }
                    }
                    else
                    {
                        bodyUnencoded = false;
                        bodyData      = new byte[] {};
                    }
                }

                // Set the encoded data length!
                header[PdfName.Length] = PdfInteger.Get(bodyData.Length);

                // 1. Header.
                header.WriteTo(stream, context);

                if (bodyUnencoded)
                {
                    // Restore actual header entries!
                    header[PdfName.Length] = PdfInteger.Get((int)body.Length);
                    Filter = null;
                }
            }

            // 2. Body.
            stream.Write(BeginStreamBodyChunk);
            stream.Write(bodyData);
            stream.Write(EndStreamBodyChunk);

            header.Updateable = true;
        }
        public override void WriteTo(
            IOutputStream stream,
            File context
            )
        {
            /*
             * NOTE: The header is temporarily tweaked to accommodate serialization settings.
             */
            header.Updateable = false;

            byte[] bodyData = null;
            {
                bool filterApplied = false;
                {
                    /*
                     * NOTE: In case of external file, the body buffer has to be saved back only if the file was
                     * actually resolved (that is brought into the body buffer) and modified.
                     */
                    FileSpecification dataFile = DataFile;
                    if (dataFile == null || (bodyResolved && body.Dirty))
                    {
                        /*
                         * NOTE: In order to keep the contents of metadata streams visible as plain text to tools
                         * that are not PDF-aware, no filter is applied to them [PDF:1.7:10.2.2].
                         */
                        if (Filter == null &&
                            !PdfName.Metadata.Equals(header[PdfName.Type])) // Filter needed.
                        {
                            // Apply the filter to the stream!
                            bodyData      = body.Encode(bytes.filters.Filter.Get((PdfName)(Filter = PdfName.FlateDecode)), null);
                            filterApplied = true;
                        }
                        else // No filter needed.
                        {
                            bodyData = body.ToByteArray();
                        }

                        if (dataFile != null)
                        {
                            try
                            {
                                using (var dataFileOutputStream = dataFile.GetOutputStream())
                                { dataFileOutputStream.Write(bodyData); }
                            }
                            catch (Exception e)
                            { throw new Exception("Data writing into " + dataFile.Path + " failed.", e); }
                        }
                    }
                    if (dataFile != null)
                    {
                        bodyData = new byte[] {};
                    }
                }

                // Set the encoded data length!
                header[PdfName.Length] = PdfInteger.Get(bodyData.Length);

                // 1. Header.
                header.WriteTo(stream, context);

                if (filterApplied)
                {
                    // Restore actual header entries!
                    header[PdfName.Length] = PdfInteger.Get((int)body.Length);
                    Filter = null;
                }
            }

            // 2. Body.
            stream.Write(BeginStreamBodyChunk);
            stream.Write(bodyData);
            stream.Write(EndStreamBodyChunk);

            header.Updateable = true;
        }
示例#3
0
        public override void WriteTo(
            IOutputStream stream
            )
        {
            bool unencodedBody;

            byte[] bodyData;
            int    bodyLength;

            // 1. Header.
            // Encoding.

            /*
             * NOTE: As the contract establishes that a stream instance should be kept
             * free from encodings in order to be editable, encoding is NOT applied to
             * the actual online stream, but to its serialized representation only.
             * That is, as encoding is just a serialization practise, it is excluded from
             * alive, instanced streams.
             */
            PdfDirectObject filterObj = header[PdfName.Filter];

            if (filterObj == null) // Unencoded body.
            {
                /*
                 * NOTE: As online representation is unencoded,
                 * header entries related to the encoded stream body are temporary
                 * (instrumental to the current serialization process).
                 */
                unencodedBody = true;

                // Set the filter to apply!
                filterObj = PdfName.FlateDecode; // zlib/deflate filter.
                // Get encoded body data applying the filter to the stream!
                bodyData = body.Encode(Filter.Get((PdfName)filterObj), null);
                // Set encoded length!
                bodyLength = bodyData.Length;
                // Update 'Filter' entry!
                header[PdfName.Filter] = filterObj;
            }
            else // Encoded body.
            {
                unencodedBody = false;

                // Get encoded body data!
                bodyData = body.ToByteArray();
                // Set encoded length!
                bodyLength = (int)body.Length;
            }
            // Set encoded length!
            header[PdfName.Length] = new PdfInteger(bodyLength);

            header.WriteTo(stream);

            // Is the body free from encodings?
            if (unencodedBody)
            {
                // Restore actual header entries!
                ((PdfInteger)header[PdfName.Length]).Value = (int)body.Length;
                header[PdfName.Filter] = null;
            }

            // 2. Body.
            stream.Write(BeginStreamBodyChunk);
            stream.Write(bodyData);
            stream.Write(EndStreamBodyChunk);
        }