Floating FB popout byF5debug

Handling Print Options in Windows Store App - Day 18

 

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;           

        }
    }
}

[/code]

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter