Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / WPF

GoalBook - A Hybrid Smart Client

Rate me:
Please Sign up or sign in to vote.
4.86/5 (24 votes)
25 Sep 2009CPOL10 min read 79K   834   69  
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
// <copyright file="PrintService.cs" company="GoalBook"> 
//    Copyright © 2009 Mark Brownsword. All rights reserved.
//    This source code and supporting files are licensed under The Code Project  
//    Open License (CPOL) as detailed at http://www.codeproject.com/info/cpol10.aspx. 
// </copyright>
namespace GoalBook.Shell.Services
{
    #region Using Statements
    using System.Printing;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Documents.Serialization;
    using System.Windows.Media;
    using System.Windows.Xps;
    using GoalBook.Infrastructure;
    using GoalBook.Infrastructure.Constants;
    using GoalBook.Infrastructure.Events;
    using GoalBook.Infrastructure.Interfaces;
    using GoalBook.Infrastructure.Printing;
    using Microsoft.Practices.Composite.Events;
    using Microsoft.Practices.Unity;
    using ShellResources = GoalBook.Shell.Properties.Resources;    
    #endregion

    /// <summary>
    /// Print Service.
    /// </summary>
    public class PrintService : IPrintService
    {              
        #region Constants and Enums
        #endregion

        #region Instance and Shared Fields
        /// <summary>
        /// Declaration for IUnityContainer.
        /// </summary>
        private readonly IUnityContainer unityContainer;

        /// <summary>
        /// Declaration for PrintDialog.
        /// </summary>
        private readonly PrintDialog printDialog;        
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the PrintService class.
        /// </summary> 
        /// <param name="unityContainer">Reference to IUnityContainer</param>
        public PrintService(IUnityContainer unityContainer)
        {
            this.unityContainer = unityContainer;
                                    
            // Initialise PrintDialog.
            this.printDialog = new PrintDialog();
            this.printDialog.PageRangeSelection = PageRangeSelection.AllPages;
            this.printDialog.UserPageRangeEnabled = true;            
        }
        #endregion

        #region Delegates and Events
        #endregion

        #region Properties
        /// <summary>
        /// Gets PrintableAreaWidth.
        /// </summary>
        public double PrintableAreaWidth { get; private set; }

        /// <summary>
        /// Gets PrintableAreaHeight.
        /// </summary>
        public double PrintableAreaHeight { get; private set; }
        #endregion

        #region Public and internal Methods
        /// <summary>
        /// Print the specified FlowDocument.
        /// </summary>
        /// <param name="title">title parameter</param>
        /// <param name="document">FlowDocument to print</param>
        public void Print(string title, FlowDocument document)
        {            
            // Return if user cancels.
            if (this.printDialog.ShowDialog().Equals(false)) 
            { 
                return; 
            }
                                      
            // Print the document.
            if (this.printDialog.PrintableAreaWidth > 0 && this.printDialog.PrintableAreaHeight > 0)
            {
                // Initialise the XpsDocumentWriter.
                XpsDocumentWriter xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(this.printDialog.PrintQueue);
                xpsDocumentWriter.WritingCancelled += new WritingCancelledEventHandler(this.PrintCancelled);
                xpsDocumentWriter.WritingCompleted += new WritingCompletedEventHandler(this.PrintCompleted);

                // Publish event PrintBegin.
                this.OnPrintBegin(ShellResources.PrintBeginMessage);

                // Initialise PageDefinition (Header and Footer).
                PageDefinition pageDefinition = new PageDefinition();
                pageDefinition.HeaderPadding = new Thickness(75, 0, 75, 0);
                pageDefinition.HeaderHeight = 50;
                pageDefinition.HeaderTemplate = title;
                pageDefinition.HeaderFontFamily = new FontFamily(PrintConstants.FONT_FAMILY_ARIAL);
                pageDefinition.HeaderFontSize = 14;
                pageDefinition.FooterPadding = new Thickness(75, 0, 75, 0);
                pageDefinition.FooterHeight = 50;
                pageDefinition.FooterTemplate = AssemblyInfoProperties.ProductName;
                pageDefinition.FooterFontFamily = new FontFamily(PrintConstants.FONT_FAMILY_ARIAL);
                pageDefinition.FooterFontSize = 12;
                pageDefinition.PageSize = new Size(this.printDialog.PrintableAreaWidth, this.printDialog.PrintableAreaHeight);

                // Set document properties to ensure content fits between 
                // header and footer and spans the whole page width.
                document.PagePadding = new Thickness(75, 75, 75, 75);
                document.ColumnWidth = this.printDialog.PrintableAreaWidth;
                
                // Initialise the FlowDocumentPaginator.
                FlowDocumentPaginator paginator = new FlowDocumentPaginator(pageDefinition, document);

                // Print asynchronously.            
                xpsDocumentWriter.WriteAsync(paginator);
            }            
        }               
        #endregion

        #region Base Class Overrides
        #endregion

        #region Private and Protected Methods
        /// <summary>
        /// Raise the PrintBegin event.
        /// </summary>  
        /// <param name="message">Message to publish</param>
        private void OnPrintBegin(string message)
        {
            this.unityContainer.Resolve<IEventAggregator>().GetEvent<PrintBeginEvent>().Publish(message);
        }

        /// <summary>
        /// Raise the PrintEnd event.
        /// </summary>   
        /// <param name="message">Message to publish</param>
        private void OnPrintEnd(string message)
        {
            this.unityContainer.Resolve<IEventAggregator>().GetEvent<PrintEndEvent>().Publish(message);
        }

        /// <summary>
        /// Raise the PrintException event.
        /// </summary>
        /// <param name="ex">Exception to publish</param>
        private void OnPrintException(PrintDialogException ex)
        {
            this.unityContainer.Resolve<IEventAggregator>().GetEvent<PrintExceptionEvent>().Publish(ex);
        }

        /// <summary>
        /// Print Completed.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">WritingCompletedEventArgs parameter</param>
        private void PrintCompleted(object sender, WritingCompletedEventArgs e)
        {
            if (e.Cancelled || e.Error != null)
            {
                this.OnPrintException(new PrintDialogException(
                    string.Format(ShellResources.PrintExceptionMessage, e.Error.Message), e.Error));
            }
            else
            {
                this.OnPrintEnd(ShellResources.PrintEndMessage);
            }
        }

        /// <summary>
        /// Print Cancelled.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">WritingCancelledEventArgs parameter</param>
        private void PrintCancelled(object sender, WritingCancelledEventArgs e)
        {
            if (e.Error != null)
            {
                this.OnPrintException(new PrintDialogException(
                    string.Format(ShellResources.PrintExceptionMessage, e.Error.Message.Replace("\r\n", string.Empty)), e.Error));
            }
            else
            {
                this.OnPrintEnd(ShellResources.PrintEndMessage);
            }
        }
        #endregion

        #region Event Handlers
        #endregion
    }  
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
I've been working as a software developer since 2000 and hold a Bachelor of Business degree from The Open Polytechnic of New Zealand. Computers are for people and I aim to build applications for people that they would want to use.

Comments and Discussions