In this article we are going to see how to use printing options with Windows Store application development. In Traditional app development printing is one find of a feature that is required to print the content directly from the application rather taking the content and printing from other supported applications. In Windows Store app development we have a contract which is used to print in the modern way which is called Print Contract. With this we can see the preview of the content that to be printed as well and have a final print to the printer. Your app need to register with the print contract, so that the printing becomes available from the Device charm by sliding over the right corner of the device.
Before we setup the printing options to the application we need to consider on two different factors, first is you need to understand what is the content that is to be printed and if its dynamic or static content. Second is you need to determine the setting of the print which best suits the application. Since an application hold multiple pages you should have control over which of the content that needs to be printed in each of the section. If your application is a Javascript/HTML based then the flexibility towards printing is pretty straight forward rather than XAML/C# as in XAML you need to decide the layout completely as you do for the pages.
Printing basically in XAML/C# application is derived from the PrintManager object, through which you need to register the object and create a PrintTask object which creates the contract to print from the application. After the contract is registered for the print contract the default UI that has been predefined can be printed from the application directly. By default all the apps that are registered with Print Contract will have the Print preview enabled and has the functions that are associated readily available.
Let us see the steps on how to incorporate printing option to the Windows Store app easily. Open the Windows Store app where we need to add the printing option or create a new project from Visual Studio 2013 and start implementing the below solution. As a first step we need to register the printer and then MyPrint is the new xaml file which has the content that needs to be printed.
[code:c#]using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Printing;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Printing;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace F5debugPrintSample
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public partial class MainPage : Page
{
protected PrintDocument printDocument = null;
protected IPrintDocumentSource printDocumentSource = null;
internal List<UIElement> printPreviewPages = null;
protected FrameworkElement firstPage;
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
RegisterForPrinting();
MyPrint myprnt = new MyPrint();
StackPanel header = (StackPanel)myprnt.FindName("header");
header.Visibility = Windows.UI.Xaml.Visibility.Visible;
PrintingRoot.Children.Add(myprnt);
PrintingRoot.InvalidateMeasure();
PrintingRoot.UpdateLayout();
}
protected virtual Canvas PrintingRoot
{
get
{
return FindName("printingRoot") as Canvas;
}
}
public virtual void RegisterForPrinting()
{
printDocument = new PrintDocument();
printDocumentSource = printDocument.DocumentSource;
}
}
}