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

Comments
Post new comment