New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Creating print output

From: Silverlight 5 Essential Training

Video: Creating print output

It's hard to imagine creating a business application that doesn't work with printers. You can now capture any part of your Silverlight XAML tree and send it to the printer. Of course, you need to show the Print dialog to your users for permission before you can complete the print job. There are three major ways to print from a Silverlight application. The first is to use the browser host window. That is very easy, and I'll show you how to do that in one minute. The second is a little more complicated. If you're running an elevated trust application, you can use COM Interop to talk to Microsoft Word.

Creating print output

It's hard to imagine creating a business application that doesn't work with printers. You can now capture any part of your Silverlight XAML tree and send it to the printer. Of course, you need to show the Print dialog to your users for permission before you can complete the print job. There are three major ways to print from a Silverlight application. The first is to use the browser host window. That is very easy, and I'll show you how to do that in one minute. The second is a little more complicated. If you're running an elevated trust application, you can use COM Interop to talk to Microsoft Word.

Of course Microsoft Word is a very full- featured document editor, so you can use its API to create table of contents, headers and footers, any other sophisticated content you want, and then have COM send that to the printer. And lastly, you can use the PrintDocument class inside Silverlight. Let me show you how to use the browser. I'm going to switch over to Visual Studio, and I'm in the Printing project, and I've opened the MainPage.xaml.

I'm in a regular Silverlight application ,so I'm going to press F5, which will launch my default browser; on this machine, that's Firefox. I'll then go up and choose File > Print, and then I have to choose where to output this for demonstration purposes. I could save it to Adobe PDF, which would create a file on my computer, and then launch my PDF reader. I could use Microsoft's XPS Document Writer, which will also create a file and save it on my hard drive. Or one that I like to use during demonstrations is Send To OneNote.

Let me show you how that works. I'll click OK. I'll then be asked where to place it inside OneNote. I'm going to choose General and then click OK, and there's my output results. Notice it looks like a standard browser print job. I've got things like the file name across the top of the page and the date/time and Page 1 of 1 at the bottom. So how do you take control of the printing process? On your own. In Silverlight, you use something called the PrintDocument. And at some point after you instantiate a PrintDocument, you're going to call its Print method.

That gives you access to three events: BeginPrint, PrintPage, and EndPrint. BeginPrint is where you write your code to initialize your print job. Once you're done there, Silverlight is going to call PrintPage. It's called for each page that you decide to print. It starts out by assuming you have a single page to print. Within this event you are at some point going to take the visuals that you want to show on the printed page and you're going to assign them to PrintDocument.PageVisual. Then you have to decide if you have any more pages or not.

If you do, then you set the HasMorePages = true, and then Silverlight calls PrintPage a second time. And you continue this until you set HasMorePages to false. And at that point then it fires the EndPrint event. This is where you do any cleanup, and this is also typically the place you tell your user, I've printed your document, how many pages you printed, and details like that. I have returned back to Visual Studio, and I'm going to start by looking at the code behind this Print this screen button. Double-click on the button, and here is the code.

It's pretty simple code. I'm going to instantiate a PrintDocument, I'm going to do some work--we'll talk about that in a second--and then I'm going to call the Print method on it here. Notice that you can read details off the PrintDocument, like it has a property called PrintedPageCount, which I can use to determine how many pages were printed. I have to set up an event procedure in order to actually print the data to the printer. In this case, I've decided to do it using an inline lambda instead of creating a separate method out here.

But the alternative would have been to come here and do a private void, create a method here with the correct signature, and then set up an event procedure saying call this print method that I defined down here. But I'm doing it inline here. This is the lambda expression for setting that up, and basically these are the two lines of code that are going to run during the print job. I say there's no more pages, and then I take the LayoutRoot, which is, if you look over here in MainPage, that is up here; it's this Grid. I'm going to take the entire contents of this grid and I'm going to send that XAML tree to PageVisual.

Before I show you the demo, let's go ahead and look at the other bits of code that are in here. If I want to do a multipage document, I'm going to start off the same. I create an instance of the page document, I set up my PrintPage function here inline, and then my Print title, and then let's look at what's different in here. I have a counter that I set up here and I have a constant called pageSize. This is the number of pages that I'm going to print. Now, in reality, you wouldn't do it this way. In reality what you need to do is you need to determine how big your print page is, what size paper you're using.

You need to determine how big a printing area you're going to print, like margin sizes. You need to look at your content in your Silverlight application. Let's say you've got a listbox that's got 400 rows. You need to decide how to divide that listbox up into what you consider pages. How much will fit on a page? You might have to calculate the font sizes and the length of the strings. So you're going to write some code to determine how to divide your content up into the pages. And then of course you have to divide and then send those chunks to the printer.

I have a really simplistic version here. I'm going to look at the counter, which should be 0, see that it's less than pageSize, which is 4, and then I'm going to say HasMorePages = true. Then I increment the counter, and then down here I generate a TextBlock programmatically, assign some text to it, and then assign that TextBlock to the PageVisual. Then it loops around again and I increment the counter, and I'll do this four times. Enough talk. Let's take a look at the printout. I'll press F5.

I'll click this first button and send that to OneNote. And then I'll go ahead and click OK to accept the general area, and there's my output. Notice it doesn't have the browser headers on it anymore, just my grid. And we'll do one more print. I'll come back here and click the Print pages, which is my multipage, and choose OK. And here you can see that I have Page 1. There's my text blocks, then I scroll down. There's Page 2, and Page 3 and Page 4 and Page 5.

So by adding printing support, Microsoft has made Silverlight even more appropriate for creating line of business applications.

Show transcript

This video is part of

Image for Silverlight 5 Essential Training
Silverlight 5 Essential Training

106 video lessons · 5311 viewers

Walt Ritscher
Author

 
Expand all | Collapse all
  1. 1m 41s
    1. Welcome
      53s
    2. Using the exercise files
      48s
  2. 35m 0s
    1. Overview of Silverlight
      3m 29s
    2. Setting up a developer computer
      2m 46s
    3. Installing the Silverlight Toolkit
      2m 21s
    4. Exploring the toolkit samples
      2m 35s
    5. Using Visual Studio 2010 to create a Silverlight project
      5m 10s
    6. Using Expression Blend to create a Silverlight project
      3m 13s
    7. Getting to know the Visual Studio interface
      8m 15s
    8. Working in the Expression Blend interface
      7m 11s
  3. 29m 46s
    1. Understanding the Visual Studio project structure
      3m 38s
    2. Creating a Silverlight page
      3m 17s
    3. Compiling your first application
      5m 0s
    4. Using other assemblies in an application
      5m 45s
    5. Deploying a Silverlight application
      3m 43s
    6. Understanding the startup process for an application
      3m 13s
    7. Understanding how users get Silverlight on their computers
      5m 10s
  4. 16m 59s
    1. Exploring the relationship between XAML and .NET
      3m 57s
    2. Using C#
      7m 16s
    3. Exploring the code-behind file
      5m 46s
  5. 43m 22s
    1. Working with XAML elements and Property attributes
      4m 56s
    2. Investigating XAML namespaces
      7m 31s
    3. Converting XAML properties with TypeConverters
      5m 1s
    4. Working with Property elements
      4m 24s
    5. Assigning runtime data with XAML markup extensions
      4m 21s
    6. Digging into the dependency property system
      6m 12s
    7. Creating a custom dependency property
      4m 42s
    8. Understanding attached properties
      6m 15s
  6. 9m 35s
    1. Debugging your code
      5m 17s
    2. Special Silverlight debugging techniques
      4m 18s
  7. 36m 0s
    1. Understanding layout
      4m 55s
    2. Using DockPanel and WrapPanel
      4m 2s
    3. Exploring sophisticated layouts with the Grid
      6m 40s
    4. Absolute positioning with the Canvas panel
      5m 20s
    5. Scrolling content with the ScrollViewer
      3m 28s
    6. Adjusting content alignment, margins, and sizing
      5m 6s
    7. Using the TabControl
      2m 17s
    8. Manipulating elements with transforms
      4m 12s
  8. 20m 32s
    1. Painting the user interface (UI) with SolidColorBrush
      6m 7s
    2. Getting colorful with gradient brushes
      3m 15s
    3. Decorating elements with ImageBrush
      4m 56s
    4. Creating effects with VideoBrush
      6m 14s
  9. 25m 42s
    1. Understanding routed events
      3m 12s
    2. Wiring up event handlers in Silverlight
      6m 4s
    3. Understanding event bubbling
      4m 39s
    4. Exploring mouse events
      7m 43s
    5. Exploring keyboard events
      4m 4s
  10. 32m 31s
    1. Displaying text on the screen
      3m 24s
    2. Gathering text input from the user
      5m 30s
    3. Showing complex text with RichTextBox
      6m 7s
    4. Understanding text overflow and text linking
      3m 14s
    5. Searching content with the AutoComplete type-ahead control
      6m 45s
    6. Using and embedding Silverlight fonts
      7m 31s
  11. 26m 47s
    1. Understanding content controls
      5m 7s
    2. Understanding button controls
      6m 16s
    3. Using the BusyIndicator
      4m 40s
    4. Showing tooltips
      4m 34s
    5. Changing content size with ViewBox
      1m 39s
    6. Exploring more controls
      4m 31s
  12. 55m 36s
    1. Connecting elements with binding
      9m 54s
    2. Using business data in a binding
      9m 21s
    3. Listing data with ItemsControls
      6m 39s
    4. Digging into the DataGrid
      8m 28s
    5. Using the DataForm
      3m 49s
    6. Storing data on the client
      7m 10s
    7. Debugging XAML bindings
      5m 41s
    8. Charting data
      4m 34s
  13. 24m 36s
    1. Creating an Out-of-Browser (OOB) application
      6m 16s
    2. Controlling aspects of an OOB application
      2m 49s
    3. Alerting the user with notification windows
      7m 13s
    4. Hosting HTML content in Silverlight applications
      8m 18s
  14. 30m 2s
    1. Exploring Silverlight trust levels
      3m 51s
    2. Creating an elevated trust application
      5m 57s
    3. Reaping the benefits of XAP signing
      6m 5s
    4. Making cross-domain calls
      5m 1s
    5. Getting started with COM Interop using the Speech API
      4m 21s
    6. Using COM Interop to interact with Microsoft Office
      4m 47s
  15. 16m 33s
    1. Showing child windows
      3m 48s
    2. Using a child window as a dialog window
      5m 7s
    3. Creating native windows
      2m 53s
    4. Maximizing a window with full screen mode
      4m 45s
  16. 13m 20s
    1. Dynamically loading UI content with user controls
      4m 46s
    2. Using the Navigation Framework
      8m 34s
  17. 9m 28s
    1. Using resource files
      3m 53s
    2. Using images in an application
      5m 35s
  18. 16m 32s
    1. Playing audio
      4m 27s
    2. Playing sound effects
      5m 4s
    3. Viewing video content with MediaElement
      4m 58s
    4. Changing video playback speed with TrickPlay
      2m 3s
  19. 14m 40s
    1. Capturing video with a web camera
      4m 16s
    2. Working with a microphone
      3m 47s
    3. Creating print output
      6m 37s
  20. 22m 35s
    1. Understanding Silverlight animations
      4m 35s
    2. Creating animations
      7m 52s
    3. Controlling animations
      4m 18s
    4. Simulating physics with animation easings
      5m 50s
  21. 27m 47s
    1. Placing XAML resources within a FrameworkElement
      6m 47s
    2. Centralizing settings in styles
      4m 8s
    3. Creating an alternate control UI with ControlTemplates
      6m 29s
    4. Dressing up your data with DataTemplates
      4m 33s
    5. Explaining the VisualStateManager: a simple way to manage control states
      5m 50s
  22. 10m 55s
    1. Handling global errors in a Silverlight application
      5m 46s
    2. Showing an application loading screen
      5m 9s
  23. 10m 48s
    1. Integrating with XNA
      7m 40s
    2. Exploring data analysis with PivotViewer
      3m 8s
  24. 1m 6s
    1. Goodbye
      1m 6s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.