views:

787

answers:

3

One of the Silverlight 4 features listed in a lot of the PDC documents is Print Preview.

I've searched for examples on how to use this and found nothing so far. Has anyone got this working yet? Can you give me some pointers on how to implement a simple web app with print preview in.

+1  A: 

I have not seen print preview as any of them but actual Printing support in which you can control which controls are printed and events based on the printing process.

Hurricanepkt
http://silverlight.net/getstarted/silverlight-4-beta/ talks about a virtual print view which may or may not be print preview but http://blog.stormideas.com/archive/2009/11/20/silverlight-4-beta-released-did-you-get-what-you.aspx and others talk explicitly about print preview functionality.
colethecoder
virtual print view allows you to create controls on the fly without actually having them display on the screen ...
Hurricanepkt
Well I should also mention Tim Heuer shows how to create the virtual print http://www.silverlight.net/learn/videos/silverlight-4-beta-videos/printing-api-basics/
Hurricanepkt
A: 

I think from the lack of responses and the fact that as Hurricanepkt pointed out in his reply Tim Heuer and others talk about a virtual print which if displying the same thing on the screen could be built quite easily into your own bespoke Print Preview functionality that the Print Preview listed in some lists is actually people misinterpretting what the Virtual Print documents actually are.

colethecoder
A: 

After looking for a while I found a way to do this by combining some features I found in other projects, but they used it for image manipulation. I tried with printing and it seems to work fine.

Here how it works: Get the base container for the print contents converted to a bitmap by using WriteableBitmap, here I´ll use a Canvas:

WriteableBitmap wb = new WriteableBitmap(this.canvas1, null);

Use this bitmap as a source for a Image control (can be inside a ScrollViewer, what is even better).

this.imagePreview.Height = wb.PixelHeight;
this.imagePreview.Width = wb.PixelWidth;
this.imagePreview.Source = wb;

Set scaling base units (used 1 percent in this case):

Point scale = new Point();      

scale.X = imagePreview.Width/100d;
scale.Y = imagePreview.Height/100d;

Then adjust the scaling using a Slider (optional)

private void vSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {

                imagePreview.Height = scale.Y * vSlider.Value;
                imagePreview.Width = scale.X * vSlider.Value;           
        }
PetVetBR