Exemple #1
0
        /**
         * <summary>Serializes the contents into the content stream.</summary>
         */
        public void Flush(
            )
        {
            PdfStream     stream;
            PdfDataObject baseDataObject = BaseDataObject;

            // Are contents just a single stream object?
            if (baseDataObject is PdfStream) // Single stream.
            {
                stream = (PdfStream)baseDataObject;
            }
            else // Array of streams.
            {
                PdfArray streams = (PdfArray)baseDataObject;
                // No stream available?
                if (streams.Count == 0) // No stream.
                {
                    // Add first stream!
                    stream = new PdfStream();
                    streams.Add(              // Inserts the new stream into the content stream.
                        File.Register(stream) // Inserts the new stream into the file.
                        );
                }
                else // Streams exist.
                {
                    // Eliminating exceeding streams...

                    /*
                     * NOTE: Applications that consume or produce PDF files are not required to preserve
                     * the existing structure of the Contents array [PDF:1.6:3.6.2].
                     */
                    while (streams.Count > 1)
                    {
                        File.Unregister((PdfReference)streams[1]); // Removes the exceeding stream from the file.
                        streams.RemoveAt(1);                       // Removes the exceeding stream from the content stream.
                    }
                    stream = (PdfStream)streams.Resolve(0);
                }
            }

            // Get the stream buffer!
            bytes::IBuffer buffer = stream.Body;

            // Delete old contents from the stream buffer!
            buffer.Clear();
            // Serializing the new contents into the stream buffer...
            Document context = Document;

            foreach (ContentObject item in items)
            {
                item.WriteTo(buffer, context);
            }
        }
Exemple #2
0
        /**
         * <summary>Sets the Javascript script into the specified base data object.</summary>
         */
        internal static void SetScript(PdfDictionary baseDataObject, PdfName key, string value)
        {
            PdfDataObject scriptObject = baseDataObject.Resolve(key);

            if (!(scriptObject is PdfStream) && value.Length > 256)
            {
                baseDataObject[key] = baseDataObject.File.Register(scriptObject = new PdfStream());
            }
            // Insert the script!
            if (scriptObject is PdfStream)
            {
                bytes::IBuffer scriptBuffer = ((PdfStream)scriptObject).Body;
                scriptBuffer.Clear();
                scriptBuffer.Append(value);
            }
            else
            {
                baseDataObject[key] = new PdfTextString(value);
            }
        }