Ejemplo n.º 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 = 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;
        }
Ejemplo n.º 2
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;
        }