Helper Class to Print the TextBox Content in Windows Store App

Introduction

Microsoft's documentation & sample for printing is too complex. The sample app for printing only shows you how to print the RichTextBlock. There's no tutorial available on printing the TextBox content in the Windows Store app. Moreover to print a single line of string, one has to write too many codes rather than a maximum of 10 lines in the WinForm. So this helper class provides the simplest method to print the text box content. It has only one static method. And it prints the text box content along with its formatting. It's majorly based on the MSDN print sample.

I got the idea to write a library from the following Stack Overflow questions.

How to print the contents of a TextBox ?
How do you print contents of a RichTextBox ?
How do I print a TextFile OR contents of a TextBox in Metro apps ?
 
How to use it?

You have to call just one static method ShowPrintUIAsync of the class Printer and it will do the print job on your behalf.

await Printer.ShowPrintUIAsync(PrintPreview, MyTextBox, Guid.NewGuid().ToString());

  • PrintPreview is a canvas, it is mandatory to show the print preview in the device charm. And You will have to declare it in your XAML.
  • MyTextBox is a text box object, whose text content is going to be printed.
  • Guid.NewGuid().ToString() is a file name for printing the document. (It will be applied if you are creating a PDF or an XPS file via printing.)

To install the WinRT.TextboxPrintHelper as NuGet package, run the following command in the Package Manager Console.

Image1.jpg

I would be glad to have issues or suggestions from you, so that I can improve this. If you want to fork the repository or post issues, find this helper class on GitHub.

Ebook Download
View all
Learn
View all