PrintDocument Class

Provides printing capabilities for a Silverlight application.


Namespace:  System.Windows.Printing
Assembly:  System.Windows (in System.Windows.dll)

Public Class PrintDocument _
	Inherits DependencyObject

The PrintDocument type exposes the following members.

Public methodPrintDocumentInitializes a new instance of the PrintDocument class.

Public propertyDispatcherGets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public propertyPrintedPageCountGets the number of pages that have printed.

Public methodCheckAccessDetermines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public methodClearValueClears the local value of a dependency property. (Inherited from DependencyObject.)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetAnimationBaseValueReturns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetValueReturns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodPrint(String)Starts the printing process for the specified document by opening the print dialog box.
Public methodPrint(String, PrinterFallbackSettings, Boolean)Starts the vector printing process for the specified document by optionally opening the print dialog box or printing directly to the default printer for trusted applications.
Public methodPrintBitmapStarts the bitmap printing process for the specified document by opening the print dialog box.
Public methodReadLocalValueReturns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public methodSetValueSets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Public eventBeginPrintOccurs after the Print method is called and the print dialog box successfully returns, but before the PrintPage event is raised.
Public eventEndPrintOccurs when the printing operation is passed to the print spooler or when the print operation is cancelled by the application author.
Public eventPrintPageOccurs when each page is printing.

Public fieldStatic memberPrintedPageCountPropertyGets the identifier for the PrintedPageCount dependency property.

The PrintDocument class provides printing capabilities from a Silverlight application. To open a print dialog box call the Print method.

In Silverlight 5 the printing defaults to vector printing. In Silverlight 4, the printing defaults to bitmap printing. You can specify bitmap printing in Silverlight 5 by using the PrintBitmap method.

To specify the content to print, handle the PrintPage event. Inside the method that handles this even you specify the content to print. For either vector or bitmap printing you set the PrintPageEventArgs.PageVisual property to the element you want to print. If you are printing items that are not in the visual tree, you should populate the UIElement with all of its content first and then assign it as the PageVisual. This will cause the layout to recalculate for printing purposes.

For bitmap printing, you can print the entire Silverlight control by setting the PrintPageEventArgs.PageVisual property to the layout root of the Silverlight content. Alternatively, you can print a portion of the Silverlight control by setting PrintPageEventArgs.PageVisual that contains the items you want to print to the named UIElement that you want to print.

After the PrintPage event occurs, the specified PrintPageEventArgs.PageVisual will be sent to the printer to be printed. If the content is too large to fit in the PrintableArea, it will be clipped. If the HasMorePages property is true, the PrintPage event occurs multiple times until HasMorePages is false.

Use the BeginPrint event to perform special handling or set up before printing begins. Use the EndPrint event for clean up or other operations after printing is complete or to detect errors that occurred during the printing process with the EndPrintEventArgs.Error property.

The following code example shows a button event handler that calls the Print method and an event handler for the PrintPage event. In this example, an Image control, which contains a map, is printed.

Partial Public Class MainPage
    Inherits UserControl
    Private pd As PrintDocument()

    Public Sub New()
        pd = New PrintDocument()

    End Sub

    Private Sub PrintButton_Click(ByVal sender As Object, _
            ByVal e As RoutedEventArgs)
        pd.Print("M yMap")
    End Sub

    Private Sub pd_PrintPage(ByVal sender As Object, _
            ByVal e As PrintPageEventArgs) Handles pd.PrintPage
        e.PageVisual = mapImage
    End Sub
End Class

<StackPanel x:Name="LayoutRoot">
      <Button Margin="5" Width="200" Content="Click to print" x:Name="PrintButton" 
              Click="PrintButton_Click" />
      <Image Width="600"  Height="600" Source="RedmondMap.jpg" x:Name="mapImage"/>


Supported in: 5, 4

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions