Results 1 to 4 of 4

Thread: How to do Printing in Silverlight Beta 4

  1. #1
    Join Date
    Feb 2008
    Posts
    121

    How to do Printing in Silverlight Beta 4

    One of the most anticipated features and requested by the community of Silverlight is the media within applications, ie the ability to send print the content you are looking on the application either a list of data in a DataGrid or ListBox or simply take a snapshot of the screen and send it to paper or a virtual printers installed on our computers (for example, OneNote, XPS, PDF, etc.).. Silverlight Beta 4 includes this feature through a simple class model. In fact this model reminds me of the print model we have in Windows Forms applications for quite some time.

    Class System.Windows.Printing.PrintDocument

    Classes for the media in Silverlight have been incorporated into System.Windows.Printing namespace, which contains, among other things, the PrintDocument class. This class PrintDocument class is responsible for sending to print the content you want. The PrintDocument class does not expose a property so we can determine the content you wish to print. However, the method Print () when run opens the print dialog box of our operating system. After selecting the destination printer (physical or virtual), trigger the following sequence of events:
    1. StartPrint
    2. PrintPage
    3. EndPrint


    Event StartPrint

    The event fires once StartPrint closed the dialog box for selecting printer. Immediately after the next event is fired: PrintPage.

    Event PrintPage

    Following the event was triggered StartPrint PrintPage. This is the most important event in the print model as it is in the grounds where we actually establish the content we want to print. This is defined in the property which is of type PageVisual UIElement. Since it is of type UIElement content can actually set as anything: whether it is present in the tree of XAML or is content created dynamically (through code or using XamlReader.Load (), etc.). For example, if we take a snapshot to the current application and print, we can establish as PageVisual the root container object (eg LayoutRoot). Or else, if we create a custom printing can generate wrappers and elements dynamically. Besides PageVisual property we have a couple of properties: HasMorePages and PrintableArea.

    HasMorePages is a property that indicates whether there are still more pages to print, for example in the case of long documents, lists, catalogs etc.. If we set this to true, then the PrintPage event will be fired.

    PrintableArea indicates the size that has the print area as the selected printer. For example it is not the same size if we send a print to OneNote that a PDF or a physical printer. This property is of type Size and we will also be used to calculate margins , positions of elements, etc. It should be noted that if the selected printer still requires user interaction, this will continue (to indicate the name of a file .Xps or .Pdf for example).

  2. #2
    Join Date
    Feb 2008
    Posts
    121

    Re: How to do Printing in Silverlight Beta 4

    Example

    Let's start with creating a new Silverlight project in April through Visual Studio 2010. In this project we will Demo.SL4.Impresion name. Visual Studio will create the solution together with the right project. Add to our MainPage.xaml the following code:

    Code:
    <Grid x: Name = "LayoutRoot" Background = "White"> 
    <Button Content = "A page 
    Height = "47" 
    HorizontalAlignment = "Left" 
    Margin = "12,12,0,0" 
    Name = "button1" 
    VerticalAlignment = "Top" 
    Width = "115" 
    Click = "button1_Click" /> 
    <Button Content = "Many pages" 
    Height = "47" 
    HorizontalAlignment = "Left" 
    Margin = "133,12,0,0" 
    Name = "button2" 
    VerticalAlignment = "Top" 
    Width = "115" 
    Click = "button2_Click" /> 
    <TextBlock Height = "30" 
    HorizontalAlignment = "Left" 
    Margin = "14,70,0,0" 
    Name = "textBlock1" 
    VerticalAlignment = "Top" 
    Width = "372" 
    FontSize = "14" /> 
    </ Grid>
    The buttons will help us to send to print using the Print method execution () PrintDocument class and the TextBlock will serve to identify the status of printing.

    In the first button click will create a new instance of PrintDocument and set up your property DocumentName properly. The value of this property will be what appears in the status window of the printer.

    Code:
    PrintDocument pd = new PrintDocument () 
    (DocumentName = "Print Example");
    However, we will handle the event PrintPage. In this case we use a lambda expression, but could also be a method:
    Code:
    pd.PrintPage + = (s, a) => 
    ( 
    // Canvas is a class level variable 
    // Set the width and height of canvas to using the property PrintableArea 
    canvas = new Canvas () (Width = a.PrintableArea.Width, Height = a.PrintableArea.Height); 
    
    
    // Add the desired title for the page 
    CrearTitulo ("This is a test print"); 
    
    // Set the content property PageVisual 
    to. PageVisual = canvas; 
    );
    We create an object of type Canvas, which will serve as a canvas to draw on the visual elements we want to print (in this example the content will be created in a TextBlock EditTitle method ()). The width and height of the Canvas object establish from PrintableArea property, which tells us the size of printing on the selected printer (for example, the print area is different in OneNote that one .xps).

    Finally, we set this dynamically created content PageVisual property. This property indicates the visual content are going to print. The following code fragment contains all the complete code for this first example:

    Code:
    Canvas canvas = null; 
    int page = 0; 
    
    
    private void button1_Click (object sender, RoutedEventArgs e) 
    ( 
    PrintDocument pd = new PrintDocument () (DocumentName = "Print Example"); 
    pd.PrintPage + = (s, a) => 
    ( 
    // Canvas is a class level variable 
    // Set the width and height of canvas to using the property PrintableArea 
    canvas = new Canvas () (Width = a.PrintableArea.Width, Height = a.PrintableArea.Height); 
    
    // Add the desired title for the page 
    EditTitle ("This is a test print"); 
    
    // Set the content property PageVisual 
    to.PageVisual = canvas; 
    ); 
    pd.StartPrint + = (s, a) => textBlock1.Text = "Starting printing ..." 
    pd.EndPrint + = (s, a) => textBlock1.Text = "Print Complete"; 
    pd.Print (); 
    ) 
    
    EditTitle void (string title) 
    ( 
    // Create a TextBlock that will serve as head 
    Content TextBlock = new TextBlock () (Text = title, FontSize = 20); 
    content.DropShadowEffect Effect = new (); 
    canvas.Children. Add (content); 
    Canvas.SetLeft (content canvas. Width / 2 - (content.ActualWidth / 2)); 
    )

  3. #3
    Join Date
    Feb 2008
    Posts
    121

    Re: How to do Printing in Silverlight Beta 4

    In the second button will have a slight variation: send to print multiple pages. This is accomplished after you set the HasMorePages = true. In the case of this example we have set up a class level variable called page that will be our flag. This flag is we'll be reviewing until the completion of 4 pages in total.

    Code:
    pd. PrintPage + = (s, a) => 
    ( 
    canvas = new Canvas () (Width = a. PrintableArea.Width, Height = a.PrintableArea.Height); 
    EditTitle ("This is a test print"); 
    EditSubtitle (string. Format ("Page (0)", page + 1)); 
    to.PageVisual = canvas; 
    
    
    to.HasMorePages =! (page == 3); 
    page + +; 
    
    );
    It is recalled that in establishing HasMorePages property to true, the event becomes PrintPage shooting, so we can have control of content to print on each page. The complete code is shown below:

    Code:
    EditSubtitle void (string title) 
    ( 
    // Create a TextBlock to serve as a subtitle 
    Content TextBlock = new TextBlock () (Text = title, FontSize = 14); 
    canvas.Children. Add (content); 
    Canvas.SetLeft (content canvas. Width / 2 - (content.ActualWidth / 2)); 
    Canvas.SetTop (content, 40); 
    ) 
    
    
    private void button2_Click (object sender, RoutedEventArgs e) 
    ( 
    PrintDocument pd = new PrintDocument () (DocumentName = "Print Example"); 
    pd.PrintPage + = (s, a) => 
    ( 
    canvas = new Canvas () (Width = a.PrintableArea. Width, Height = a.PrintableArea.Height); 
    EditTitle ("This is a test print"); 
    CrearSubtitulo (string.Format ("Page (0)", page + 1)); 
    to.PageVisual = canvas; 
    
    to.HasMorePages =! (page == 3); 
    page + +; 
    
    ); 
    pd.StartPrint + = (s, a) => textBlock1. Text = "Starting printing ..." 
    pd.EndPrint + = (s, a) => textBlock1. Text = "Print Complete"; 
    pd.Print (); 
    )
    Summary

    System.Windows.Printing.PrintDocument class. This class includes the method Print (), which when executed triggers a series of events including the event where we can establish PrintPage the desired content for print, knowing the size of the print area and determine if there are more pages to print (for scenes of documents over a page).

  4. #4
    Join Date
    Sep 2010
    Posts
    79

    Re: How to do Printing in Silverlight Beta 4

    Your explanation is very clear, excellent! What would be an example without displaying the print dialog, or print directly to a printer (pre-selected by the application by the name of the printer).

    Again, many thanks for your work.

Similar Threads

  1. Replies: 3
    Last Post: 17-04-2011, 04:55 PM
  2. Silverlight 4 RC with Blend 4 Beta
    By Obama P in forum Windows Software
    Replies: 6
    Last Post: 06-08-2010, 10:21 AM
  3. Printing and Parameters issues in Silverlight Beta 4
    By Mulan in forum Windows Software
    Replies: 5
    Last Post: 05-08-2010, 11:14 PM
  4. Uninstalling Silverlight 4 Beta on OSX
    By Fausty in forum Windows Software
    Replies: 3
    Last Post: 05-08-2010, 05:11 PM
  5. Silverlight 4 DataGrid is not printing properly.
    By Adrut in forum Windows Software
    Replies: 3
    Last Post: 03-08-2010, 05:15 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,520,720.01156 seconds with 17 queries