XpsSerializerWriter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Print / Reach / SerializerFactory / XpsSerializerWriter.cs / 1 / XpsSerializerWriter.cs

                            //---------------------------------------------------------------------------- 
//
// 
//    Copyright (C) Microsoft Corporation.  All rights reserved.
//  
//
// 
// Description: XpsSerializerWriter is a concrete implementation for a plug-in SerializerWriter. It punts everything to XpsDocumentWriter 
//
//              See spec at  
//
// History:
//  07/16/2005 : [....] - Created
// 
//---------------------------------------------------------------------------
namespace System.Windows.Xps.Serialization 
{ 
    using System;
    using System.IO; 
    using System.IO.Packaging;
    using System.Printing;
    using System.Windows.Xps;
    using System.Windows.Documents; 
    using System.Windows.Documents.Serialization;
    using System.Windows.Media; 
    using System.Windows.Xps.Packaging; 

    ///  
    /// XpsSerializerWriter is a concrete implementation for a plug-in SerializerWriter. It punts everything to XpsDocumentWriter
    /// 
    internal class XpsSerializerWriter : SerializerWriter
    { 
        #region Constructors
 
        private XpsSerializerWriter() 
        {
        } 

        /// 
        /// creates a XpsSerializerWriter
        ///  
        public XpsSerializerWriter(Stream stream)
            : base() 
        { 
            _package = Package.Open(stream,FileMode.Create,FileAccess.ReadWrite);
 
            _xpsDocument = new XpsDocument(_package);
            _xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(_xpsDocument);
            _xpsDocumentWriter.WritingPrintTicketRequired += new WritingPrintTicketRequiredEventHandler(xsw_WritingPrintTicketRequired);
            _xpsDocumentWriter.WritingProgressChanged += new WritingProgressChangedEventHandler(xsw_WritingProgressChanged); 
            _xpsDocumentWriter.WritingCompleted += new WritingCompletedEventHandler(xsw_WritingCompleted);
            _xpsDocumentWriter.WritingCancelled += new WritingCancelledEventHandler(xsw_WritingCancelled); 
        } 

        #endregion 

        #region Public Methods

        ///  
        /// Write a single Visual and close stream
        ///  
        public override void Write(Visual visual) 
        {
            CheckDisposed(); 

            _xpsDocumentWriter.Write(visual);

            FinalizeWriter(); 
        }
 
        ///  
        /// Write a single Visual and close stream
        ///  
        public override void Write(Visual visual, PrintTicket printTicket)
        {
            CheckDisposed();
 
            _xpsDocumentWriter.Write(visual, printTicket);
 
            FinalizeWriter(); 
        }
 
        /// 
        /// Async Write a single Visual and close stream
        /// 
        public override void WriteAsync(Visual visual) 
        {
            CheckDisposed(); 
 
            _xpsDocumentWriter.WriteAsync(visual);
        } 

        /// 
        /// Async Write a single Visual and close stream
        ///  
        public override void WriteAsync(Visual visual, PrintTicket printTicket)
        { 
            CheckDisposed(); 

            _xpsDocumentWriter.WriteAsync(visual, printTicket); 
        }

        /// 
        /// Async Write a single Visual and close stream 
        /// 
        public override void WriteAsync(Visual visual, object userState) 
        { 
            CheckDisposed();
 
            _xpsDocumentWriter.WriteAsync(visual, userState);
        }

        ///  
        /// Async Write a single Visual and close stream
        ///  
        public override void WriteAsync(Visual visual, PrintTicket printTicket, object userState) 
        {
            CheckDisposed(); 

            _xpsDocumentWriter.WriteAsync(visual, printTicket, userState);
        }
 
        /// 
        /// Write a single DocumentPaginator and close stream 
        ///  
        public override void Write(DocumentPaginator paginator)
        { 
            CheckDisposed();

            _xpsDocumentWriter.Write(paginator);
 
            FinalizeWriter();
        } 
 
        /// 
        /// Write a single DocumentPaginator and close stream 
        /// 
        public override void Write(DocumentPaginator paginator, PrintTicket printTicket)
        {
            CheckDisposed(); 

            _xpsDocumentWriter.Write(paginator, printTicket); 
 
            FinalizeWriter();
        } 

        /// 
        /// Async Write a single DocumentPaginator and close stream
        ///  
        public override void WriteAsync(DocumentPaginator paginator)
        { 
            CheckDisposed(); 

            _xpsDocumentWriter.WriteAsync(paginator); 
        }

        /// 
        /// Async Write a single DocumentPaginator and close stream 
        /// 
        public override void WriteAsync(DocumentPaginator paginator, PrintTicket printTicket) 
        { 
            CheckDisposed();
 
            _xpsDocumentWriter.WriteAsync(paginator, printTicket);
        }

        ///  
        /// Async Write a single DocumentPaginator and close stream
        ///  
        public override void WriteAsync(DocumentPaginator paginator, object userState) 
        {
            CheckDisposed(); 

            _xpsDocumentWriter.WriteAsync(paginator, userState);
        }
 
        /// 
        /// Async Write a single DocumentPaginator and close stream 
        ///  
        public override void WriteAsync(DocumentPaginator paginator, PrintTicket printTicket, object userState)
        { 
            CheckDisposed();

            _xpsDocumentWriter.WriteAsync(paginator, printTicket, userState);
        } 

        ///  
        /// Write a single FixedPage and close stream 
        /// 
        public override void Write(FixedPage fixedPage) 
        {
            CheckDisposed();

            _xpsDocumentWriter.Write(fixedPage); 

            FinalizeWriter(); 
        } 

        ///  
        /// Write a single FixedPage and close stream
        /// 
        public override void Write(FixedPage fixedPage, PrintTicket printTicket)
        { 
            CheckDisposed();
 
            _xpsDocumentWriter.Write(fixedPage, printTicket); 

            FinalizeWriter(); 
        }

        /// 
        /// Async Write a single FixedPage and close stream 
        /// 
        public override void WriteAsync(FixedPage fixedPage) 
        { 
            CheckDisposed();
 
            _xpsDocumentWriter.WriteAsync(fixedPage);
        }

        ///  
        /// Async Write a single FixedPage and close stream
        ///  
        public override void WriteAsync(FixedPage fixedPage, PrintTicket printTicket) 
        {
            CheckDisposed(); 

            _xpsDocumentWriter.WriteAsync(fixedPage, printTicket);
        }
 
        /// 
        /// Async Write a single FixedPage and close stream 
        ///  
        public override void WriteAsync(FixedPage fixedPage, object Async)
        { 
            CheckDisposed();

            _xpsDocumentWriter.WriteAsync(fixedPage, Async);
        } 

        ///  
        /// Async Write a single FixedPage and close stream 
        /// 
        public override void WriteAsync(FixedPage fixedPage, PrintTicket printTicket, object Async) 
        {
            CheckDisposed();

            _xpsDocumentWriter.WriteAsync(fixedPage, printTicket, Async); 
        }
 
        ///  
        /// Write a single FixedDocument and close stream
        ///  
        public override void Write(FixedDocument fixedDocument)
        {
            CheckDisposed();
 
            _xpsDocumentWriter.Write(fixedDocument);
 
            FinalizeWriter(); 
        }
 
        /// 
        /// Write a single FixedDocument and close stream
        /// 
        public override void Write(FixedDocument fixedDocument, PrintTicket printTicket) 
        {
            CheckDisposed(); 
 
            _xpsDocumentWriter.Write(fixedDocument, printTicket);
 
            FinalizeWriter();
        }

        ///  
        /// Async Write a single FixedDocument and close stream
        ///  
        public override void WriteAsync(FixedDocument fixedDocument) 
        {
            CheckDisposed(); 

            _xpsDocumentWriter.WriteAsync(fixedDocument);
        }
 
        /// 
        /// Async Write a single FixedDocument and close stream 
        ///  
        public override void WriteAsync(FixedDocument fixedDocument, PrintTicket printTicket)
        { 
            CheckDisposed();

            _xpsDocumentWriter.WriteAsync(fixedDocument, printTicket);
        } 

        ///  
        /// Async Write a single FixedDocument and close stream 
        /// 
        public override void WriteAsync(FixedDocument fixedDocument, object userState) 
        {
            CheckDisposed();

            _xpsDocumentWriter.WriteAsync(fixedDocument, userState); 
        }
 
        ///  
        /// Async Write a single FixedDocument and close stream
        ///  
        public override void WriteAsync(FixedDocument fixedDocument, PrintTicket printTicket, object userState)
        {
            CheckDisposed();
 
            _xpsDocumentWriter.WriteAsync(fixedDocument, printTicket, userState);
        } 
 
        /// 
        /// Write a single FixedDocumentSequence and close stream 
        /// 
        public override void Write(FixedDocumentSequence fixedDocumentSequence)
        {
            CheckDisposed(); 

            _xpsDocumentWriter.Write(fixedDocumentSequence); 
 
            FinalizeWriter();
        } 

        /// 
        /// Write a single FixedDocumentSequence and close stream
        ///  
        public override void Write(FixedDocumentSequence fixedDocumentSequence, PrintTicket printTicket)
        { 
            CheckDisposed(); 

            _xpsDocumentWriter.Write(fixedDocumentSequence, printTicket); 

            FinalizeWriter();
        }
 
        /// 
        /// Async Write a single FixedDocumentSequence and close stream 
        ///  
        public override void WriteAsync(FixedDocumentSequence fixedDocumentSequence)
        { 
            CheckDisposed();

            _xpsDocumentWriter.WriteAsync(fixedDocumentSequence);
        } 

        ///  
        /// Async Write a single FixedDocumentSequence and close stream 
        /// 
        public override void WriteAsync(FixedDocumentSequence fixedDocumentSequence, PrintTicket printTicket) 
        {
            CheckDisposed();

            _xpsDocumentWriter.WriteAsync(fixedDocumentSequence, printTicket); 
        }
 
        ///  
        /// Async Write a single FixedDocumentSequence and close stream
        ///  
        public override void WriteAsync(FixedDocumentSequence fixedDocumentSequence, object userState)
        {
            CheckDisposed();
 
            _xpsDocumentWriter.WriteAsync(fixedDocumentSequence, userState);
        } 
 
        /// 
        /// Async Write a single FixedDocumentSequence and close stream 
        /// 
        public override void WriteAsync(FixedDocumentSequence fixedDocumentSequence, PrintTicket printTicket, object userState)
        {
            CheckDisposed(); 

            _xpsDocumentWriter.WriteAsync(fixedDocumentSequence, printTicket, userState); 
        } 

        ///  
        /// Cancel Asynchronous Write
        /// 
        public override void CancelAsync()
        { 
            CheckDisposed();
 
            _xpsDocumentWriter.CancelAsync(); 
        }
 
        /// 
        /// Create a SerializerWriterCollator to gobble up multiple Visuals
        /// 
        public override SerializerWriterCollator CreateVisualsCollator() 
        {
            CheckDisposed(); 
 
            SerializerWriterCollator collator = _xpsDocumentWriter.CreateVisualsCollator();
 
            // swc will close these
            _xpsDocument = null;
            _xpsDocumentWriter = null;
            _package = null; 

            return collator; 
        } 

        ///  
        /// Create a SerializerWriterCollator to gobble up multiple Visuals
        /// 
        public override SerializerWriterCollator CreateVisualsCollator(PrintTicket documentSequencePT, PrintTicket documentPT)
        { 
            CheckDisposed();
 
            SerializerWriterCollator collator = _xpsDocumentWriter.CreateVisualsCollator(documentSequencePT, documentPT); 

            // swc will close these 
            _xpsDocument = null;
            _xpsDocumentWriter = null;
            _package = null;
 
            return collator;
        } 
 
        /// 
        /// This event will be invoked if the writer wants a PrintTicker 
        /// 
        public override event WritingPrintTicketRequiredEventHandler WritingPrintTicketRequired;

        ///  
        /// This event will be invoked if the writer progress changes
        ///  
        public override event WritingProgressChangedEventHandler WritingProgressChanged; 

        ///  
        /// This event will be invoked if the writer is done
        /// 
        public override event WritingCompletedEventHandler WritingCompleted;
 
        /// 
        /// This event will be invoked if the writer is done 
        ///  
        public override event WritingCancelledEventHandler WritingCancelled;
 
        #endregion

        #region Private Methods
 
        private void xsw_WritingPrintTicketRequired(object sender, WritingPrintTicketRequiredEventArgs e)
        { 
            if (WritingPrintTicketRequired != null) 
            {
                WritingPrintTicketRequired.Invoke(sender, e); 
            }
        }

        private void xsw_WritingProgressChanged(object sender, WritingProgressChangedEventArgs e) 
        {
            if ( WritingProgressChanged != null) 
            { 
                WritingProgressChanged.Invoke(sender, e);
            } 
        }

        private void xsw_WritingCompleted(object sender, WritingCompletedEventArgs e)
        { 
            if ( WritingCompleted != null)
            { 
                FinalizeWriter(); 
                WritingCompleted.Invoke(sender, e);
            } 
        }

        private void xsw_WritingCancelled(object sender, WritingCancelledEventArgs e)
        { 
            if ( WritingCancelled != null)
            { 
                FinalizeWriter(); 
                WritingCancelled.Invoke(sender, e);
            } 
        }

        private void CheckDisposed()
        { 
            if (_xpsDocumentWriter == null)
            { 
                throw new XpsSerializationException(ReachSR.Get(ReachSRID.XpsSerializerFactory_WriterIsClosed)); 
            }
        } 

        private void FinalizeWriter()
        {
            _xpsDocument.Close(); 
            _package.Close();
            _xpsDocument = null; 
            _xpsDocumentWriter = null; 
            _package = null;
        } 

        #endregion

        #region Data 

        private Package             _package; 
        private XpsDocument         _xpsDocument; 
        private XpsDocumentWriter   _xpsDocumentWriter;
 
        #endregion
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK