/** * <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. } PdfReference streamReference = (PdfReference)streams[0]; File.Update(streamReference); // Updates the existing stream into the file. stream = (PdfStream)streamReference.DataObject; } } // Get the stream buffer! bytes::IBuffer buffer = stream.Body; // Delete old contents from the stream buffer! buffer.SetLength(0); // Serializing the new contents into the stream buffer... foreach (ContentObject item in items) { item.WriteTo(buffer); } // Update the content stream container! Update(); }
/** * <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.SetLength(0); scriptBuffer.Append(value); } else { baseDataObject[key] = new PdfTextString(value); } }