goalbook.zip
GoalBook
Dependencies
CAG WPF 2
Microsoft.Practices.Composite.dll
Microsoft.Practices.Composite.Presentation.dll
Microsoft.Practices.Composite.UnityExtensions.dll
Microsoft.Practices.ObjectBuilder2.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
vssver2.scc
CSLA
Csla.dll
Csla.XmlSerializers.dll
vssver2.scc
Infragistics
Infragistics3.Wpf.DataPresenter.v9.1.Express.dll
Infragistics3.Wpf.Editors.v9.1.Express.dll
Infragistics3.Wpf.v9.1.Express.dll
vssver2.scc
WPFToolkit
vssver2.scc
WPFToolkit.dll
GoalBook.Controls
GoalBook.Goals
GoalBook.Infrastructure
GoalBook.Notes
GoalBook.Public
GoalBook.Shell
App.ico
App.xaml
App.xaml.cs
Bootstrapper.cs
Commands
GoalBook.Shell.csproj
GoalBook.Shell.csproj.user
Journal.ico
Journal48.ico
Misc
Modules
Properties
Resources
arrow_refresh.png
arrow_undo.png
checked.gif
cross.png
disk.png
email.png
erase.png
error.png
exclamation.png
flag_red.png
group.png
help.png
information.png
magnifier.png
page_red.png
pencil.png
printer.png
user.png
vssver2.scc
Wave.jpg
world.png
world_link.png
Services
Splash.png
Views
vssver2.scc
Windows
GoalBook.sln
GoalBook.snk
GoalBook.Synchronisation
GoalBook.Tasks
License.txt
ReadMe.txt
SolutionInfo.cs
vssver2.scc
|
// <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.
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.