Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Silverlight
PrintDocument Class
Collapse All/Expand All Collapse All
.NET Framework Class Library for Silverlight
PrintDocument Class

Provides printing capabilities for a Silverlight application.

System..::.Object
  System.Windows..::.DependencyObject
    System.Windows.Printing..::.PrintDocument

Namespace:  System.Windows.Printing
Assembly:  System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
Public Class PrintDocument _
    Inherits DependencyObject
C#
public class PrintDocument : DependencyObject

The PrintDocument type exposes the following members.

  NameDescription
Public methodPrintDocumentInitializes a new instance of the PrintDocument class.
Top
  NameDescription
Public propertyDispatcherGets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public propertyPrintedPageCountGets the number of pages that have printed.
Top
  NameDescription
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.)
Top
  NameDescription
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.
Top
  NameDescription
Public fieldStatic memberPrintedPageCountPropertyGets the identifier for the PrintedPageCount dependency property.
Top

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.

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

    Public Sub New()
        InitializeComponent()
        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
C#
public partial class MainPage : UserControl
{
    PrintDocument pd; 

    public MainPage()
    {
        InitializeComponent();
        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;
    }
}
XAML
<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"/>
  </StackPanel>

Silverlight

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.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
PrintDocument fails silently if page size too large      breed052   |   Edit   |   Show History
After calling Print() on a PrintDocument object, if the paper size selected by the user is too large (A2 or larger 16.5 In.x23.4 In.), PrintDocument never fires BeginPrint, PrintPage, or EndPrint events, and no exception is thrown from the Print call. It just fails silently
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker