Microsoft Lightswitch Client Native Printing

Posted in .net, LightSwitch, Microsoft, Software, Visual Studio by tejana on April 27, 2011

Visual Studio 2010 Custom Silverlight Control as Print Template

Simple printing would be a misnomer, perhaps. Printing is the most obvious shortcoming of Microsoft’s Lightswitch development environment. Before we lament about the lack of printing support, let us review what is available. Silverlight has made advances in printing that will allow limited printing. To see a discussion of shortfalls and alternatives see here:


Following with Beth Massi’s Using Word in Lightswitch project from my last post. Simply adding a Silverlight Class Library with a UserControl allows us to add a reference to our LightSwitch project. I have then added the custom control to the bottom of the Customer Detail screen. This is done by adding another Customer Row Layout and setting the control to our custom control. The control will act as a printing template for simple direct printing of the Customer details. I have also added a screen property just to demonstrate that any screen data is available to print via the custom control binding.


By adding  to the CustomerDetail screen code CustomerDetail.cs:

        partial void CustomerDetail_Activated()
                    += new EventHandler<ControlAvailableEventArgs>(CustomerDetail_ControlAvailable);

        void CustomerDetail_ControlAvailable(object sender, ControlAvailableEventArgs e)
            SilverlightClassLibrary1.SilverlightControl1 ctrl
                      = e.Control as SilverlightClassLibrary1.SilverlightControl1;
            ctrl.Visibility = System.Windows.Visibility.Collapsed;

The custom control will not initially be visible on the screen. We have added a Print Direct button the the screen with the execution code:

        System.Windows.Printing.PrintDocument pd = new System.Windows.Printing.PrintDocument();
        System.Windows.UIElement ele;

        partial void PrintDirect_Execute()
                         += webControlAvailable;
                         += new EventHandler<System.Windows.Printing.PrintPageEventArgs>(pd_PrintPage);


        private void webControlAvailable(object sender, ControlAvailableEventArgs e)

            SilverlightClassLibrary1.SilverlightControl1 ctrl
                          = e.Control as SilverlightClassLibrary1.SilverlightControl1;
            ctrl.Visibility = System.Windows.Visibility.Visible;
            ele = ctrl;

            ctrl.Visibility = System.Windows.Visibility.Collapsed;

        private void pd_PrintPage(object sender, System.Windows.Printing.PrintPageEventArgs ev)
            ev.PageVisual = ele;


When the Print Direct button is executed the custom control becomes visible and it is placed as the PageVisual of a silverlight PrintDocument. The document is printed and then our custom control is again collapsed.




This method will allow printing of un-saved screen data on special forms like Invoices, Purchase Orders or Checks. The custom control is set up like this:

<UserControl x:Class="SilverlightClassLibrary1.SilverlightControl1"
   d:DesignHeight="200" d:DesignWidth="300">

    <Border BorderThickness="3" BorderBrush="Black" CornerRadius="10" Padding="2">
        <StackPanel x:Name="LayoutRoot" Background="White" Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <TextBlock Height="23" HorizontalAlignment="Left" Name="textblock1" VerticalAlignment="Top" Width="Auto" Text="{Binding  Mode=TwoWay, Path=Screen.Customer.FirstName}" FontSize="14" />
                <TextBlock Height="23" HorizontalAlignment="Left" Name="textblock2" VerticalAlignment="Top" Width="Auto" Text=" " FontSize="14" />
                <TextBlock Height="23" HorizontalAlignment="Right" Name="textblock3" VerticalAlignment="Top" Width="Auto" Text="{Binding  Mode=TwoWay, Path=Screen.Customer.LastName}" FontSize="14" />
            <TextBlock Height="23" HorizontalAlignment="Left"  Name="textblock4" VerticalAlignment="Top" Width="120" Text="{Binding  Mode=TwoWay, Path=Screen.Customer.Address1}" FontSize="14"  />
            <TextBlock Height="23" HorizontalAlignment="Left"  Name="textblock5" VerticalAlignment="Top" Width="120" Text="{Binding  Mode=TwoWay, Path=Screen.Customer.Address2}" FontSize="14"  />
            <StackPanel Orientation="Horizontal">
                <TextBlock Height="23" HorizontalAlignment="Left"  Name="textlbock6" VerticalAlignment="Top"  Text="{Binding  Mode=TwoWay, Path=Screen.Customer.City}" FontSize="14"  />
                <TextBlock Height="23" HorizontalAlignment="Left" Name="textblock7" VerticalAlignment="Top" Width="Auto" Text=", " FontSize="14" />
                <TextBlock Height="23" HorizontalAlignment="Left"  Name="textblock8" VerticalAlignment="Top" Text="{Binding  Mode=TwoWay, Path=Screen.Customer.State}" FontSize="14"  />
                <TextBlock Height="23" HorizontalAlignment="Left" Name="textblock9" VerticalAlignment="Top"  Text=" " FontSize="14" />
                <TextBlock Height="23" HorizontalAlignment="Left"  Name="textbock10" VerticalAlignment="Top" Width="Auto" Text="{Binding  Mode=TwoWay, Path=Screen.Customer.PostalCode}" FontSize="14"  />
            <StackPanel Orientation="Horizontal">
                <TextBlock Height="23" HorizontalAlignment="Left" Name="textblock11" VerticalAlignment="Top" Width="Auto" Text="Phone: "></TextBlock>
                <TextBlock Height="23" HorizontalAlignment="Left"  Name="textblock12" VerticalAlignment="Top" Width="Auto" Text="{Binding Mode=TwoWay, Path=Screen.Customer.Phone}" FontSize="14" />
            <StackPanel Orientation="Horizontal">
                <TextBlock Height="23" HorizontalAlignment="Left" Name="textblock13" VerticalAlignment="Top" Width="Auto" Text="Gender: "></TextBlock>
                <TextBlock Height="23" HorizontalAlignment="Left"  Name="textblock14" VerticalAlignment="Top" Width="Auto" Text="{Binding Mode=TwoWay, Path=Screen.Customer.Gender}" FontSize="14" />
            <TextBlock Height="23" HorizontalAlignment="Left"  Name="textblock15" VerticalAlignment="Top" Width="Auto" Text="{Binding Mode=TwoWay, Path=Screen.Property1}" FontSize="14" />


Notice that we can print any screen data using binding such as Path=Screen.Property1.

Source code including the last post, printing using Word in C#, is available here.


4 Responses

Subscribe to comments with RSS.

  1. edison said, on May 4, 2011 at 11:54 pm

    podria hacerlo en vb y como se imprimeria despeus de grabar la factura

  2. Tejana said, on May 16, 2011 at 1:11 am

    La impresión de Silverlight está prevista para el próximo año una actualización. Hasta entonces SSRS o HTML. Escucha aquí para HTML

  3. edison said, on May 16, 2011 at 3:52 pm

    Hola buen dia amigo gracias por tu respuesta tengo una duda y no se ingles y tmapoco se como explicarlo veras a ver si tu me entiendes
    quiero hacer una factura pero con un grid que vaya almacenando los items que busco desde un textbox

    es decir asi como esta imagen donde busco los productos y ponga la cantidad y se agregan al grid


  4. Tejana said, on May 17, 2011 at 12:15 pm

    Spanish to English of edison on May 16
    Hello good day friend thanks for your reply I have a doubt and not tmapoco English and how to explain it really to see if you understand me
    I want to do a bill but with a grid to be storing the items I’m looking from a textbox ie as well as this picture where I find the products and enter the amount and added to the grid

    Hola Edison,
    La manera más sencilla, con baja calidad de impresión, se utiliza el método de esta entrada de blog. El control personalizado es la imagen de la factura.
    Voy a utilizar de la manera difícil, el método de https://tejana.wordpress.com/2011/04/29/microsoft-lightswitch-printing-html
    Esto es mucho trabajo durante muchos días. Me puede discutir, pero no dan.

    Hi Edison,
    The easiest way, with low print quality, using the method of this blog. The custom control is the image of the invoice.
    I will use the hard way, the method of https://tejana.wordpress.com/2011/04/29/microsoft-lightswitch-printing-html
    This is a lot of work for many days. I can discuss, but do not give.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: