PrintDocument Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Provides printing capabilities for a Silverlight application.

Inheritance Hierarchy


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


Public Class PrintDocument _
    Inherits DependencyObject
public class PrintDocument : DependencyObject

The PrintDocument type exposes the following members.


  Name Description
Public method PrintDocument Initializes a new instance of the PrintDocument class.



  Name Description
Public property Dispatcher Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public property PrintedPageCount Gets the number of pages that have printed.



  Name Description
Public method CheckAccess Determines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public method ClearValue Clears the local value of a dependency property. (Inherited from DependencyObject.)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetAnimationBaseValue Returns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetValue Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Print(String) Starts the printing process for the specified document by opening the print dialog box.
Public method Print(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 method PrintBitmap Starts the bitmap printing process for the specified document by opening the print dialog box.
Public method ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public method SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)



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



  Name Description
Public fieldStatic member PrintedPageCountProperty Gets 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
public partial class MainPage : UserControl
    PrintDocument pd; 

    public MainPage()
        pd = new PrintDocument();
        pd.PrintPage += new EventHandler<PrintPageEventArgs>(pd_PrintPage);


    private void PrintButton_Click(object sender, RoutedEventArgs e)
      pd.Print("My Map");

    void pd_PrintPage(object sender, PrintPageEventArgs e)
        e.PageVisual = mapImage;
<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"/>

Version Information


Supported in: 5, 4


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

Thread Safety

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

See Also


Other Resources