Subscribe to:

Easy landscape printing in Silverlight

I know this isn't normally what I post about here, but I thought someone on the web might find it useful.

I struggled in vain to find a simple solution to printing landscape view in Silverlight, but I managed to come up with one that only takes a few lines of code:

 

 

            var printDocument = new PrintDocument();

            printDocument.PrintPage += (s, r) =>

                                           {

                                               r.PageVisual = this.LayoutRoot;

                                               var transformGroup = new TransformGroup();

                                               transformGroup.Children.Add(new RotateTransform(){Angle = 90});

                                               transformGroup.Children.Add(new TranslateTransform() {X = r.PrintableArea.Width});

                                               LayoutRoot.RenderTransform = transformGroup;

                                           };

This simple Lamda method, which assumes that you want to print the layout root of the page (Although there shouldn't be a reason why it can't print any element on the page), applies two transforms. The first is simply to rotate the whole layout root 90 degrees. The reason why this alone isn't enough, is that by default the layout root will rotate on it's top left corner, so when you rotate it 90 degrees the body of the page moves left (Imagine rotating a page on a cork board with a pin through the top left corner). The second transform is to correct this by moving the page by the width of the printable area to the right.

After you have finished printing, you will want to move everything back so that it still renders to screen correctly.

 

            printDocument.EndPrint += (s, r) =>

                                          {

                                              var transformGroup = new TransformGroup();

                                              transformGroup.Children.Add(new RotateTransform() { Angle = 0 });

                                              transformGroup.Children.Add(new TranslateTransform() { X = 0});

                                              LayoutRoot.RenderTransform = transformGroup;

                                          };

Hope this helps

Tags:

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • HTML tags will be transformed to conform to HTML standards.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.