.NET Zone is brought to you in partnership with:

Farhan is Windows 8 & Windows Phone 8 Apps developer having 1.5+ year of experience of developing the productivity and line-of-business (LOB) apps for Microsoft's new break-through OS Windows 8 as well as Windows Phone 8. He is one of the pioneer developer who develops apps for Windows 8 devices. He has keen interest in Microsoft Technologies. Earlier he has attended Microsoft Tech-Ed 2011 event at Ahmedabad. That event inspired him a lot and it created great impact on his career. Now-a-days he writes articles related to Windows 8 & Windows Phone 8 apps development on his blog, C-Sharp Corner & CodeProject. His most favorite activity is to solve developer's question on Stack Overflow. In free time he loves to solve developer's questions & confusions, so he like to spend my free time on Stack Overflow (Bronze medal owner for Windows-8 & Windows-store-apps tagged questions), CodeProject, GeekChamp, Twitter (#WinRT, #Win8Dev, #Win8, #WPDev, #WP8, #XAML), Paper.li etc. Farhan has experience of developing Windows 8 apps, Windows Phone 8 apps, Silverlight apps and ASP.NET websites. He has superior expertise in the domains of .NET, WinRT, Silverlight, C#, XAML, XML, SQLite, Azure, Expression Blend, Telerik, SyncFusion, WCF, REST, JSON and RSS. He is also best in Object Oriented Analysis (OOA) and he is perfect in Software Development Life Cycle. He is also familiar with utilizing 3rd party libraries, 3rd party UI controls, data exchange, API integration, DLL & WinMD development, API development, etc. He can also migrate Android, iOS and Mac app to Windows 8 and Windows Phone 8. He is more positive, sensible to urgency and passionate about his career as a Microsoft technology developer and looking forward to more and more challenging opportunities. If you have unique app idea or any app requirement for your business & enterprise, you will definitely find him a suitable & workaholic developer. You will have long term professional relationship with better ROI, for sure :) Farhan has posted 3 posts at DZone. You can read more from them at their website. View Full User Profile

A Helper Class to Print Text Box Content in a Windows Store App

01.31.2014
| 4889 views |
  • submit to reddit

Introduction

Microsoft's documentationsample for printing is too complex. The sample app for printing shows only how to print RichTextBlock. There's no tutorial to print the TextBox content in a Windows Store app. Moreover, to print a single line string, one has to write too much code rather than a maximum of 10 lines in WinForm. So, this helper class provides the simplest method to print the text box content. It has only one static method. It prints the text box content with its formatting. It's majorly based on 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 class Printer, it will do print job on your behalf.

await Printer.ShowPrintUIAsync(PrintPreview, MyTextBox, Guid.NewGuid().ToString());
  • PrintPreview is a canvas, which is mandatory to show print preview in device charm. You have to declare 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 PDF or XPS file via printing.)
To install WinRT.TextboxPrintHelper as NuGet package, run the following command in the Package Manager Console.


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

Published at DZone with permission of its author, Farhan Ghumra. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)