Locked Printing with LightSwitch?

  • Saturday, August 21, 2010 2:46 AM
     
     
    I am about to create a control that will layout a report and print it. I just wanted to know if I missed something obvious?

Answers

All Replies

  • Saturday, August 21, 2010 4:27 AM
    Moderator
     
     

    I assume that you understand that LightSwitch doesn't have any native support for printing. I can't think of any reason that you couldn't create a Silverlight control to do it, though - I know I've seen a third-party Silverlight reporting component on Visual Studio Gallery, although I haven't tried it.

    Let us know how it turns out.

    Regards,

     


    Steve Hoag Microsoft aka the V-Bee
  • Saturday, August 21, 2010 5:10 AM
     
     
    There's native support for printing in Silverlight. See here: http://msdn.microsoft.com/en-us/library/system.windows.printing.printdocument(VS.95).aspx

    In our OSS project (MIT/X-11 license) we want to persist images of controls, with the additional option of printing. Here's how we do that bit:

    In Silverlight, WritableBitmap has a constructor overload that takes any UIElement, so you can...

    1) create a WriteableBitmap using the UIElement overload
    2) encode the bitmap to a .jpg (optional)
    3) save the image
        a) if you want a user-specified location to save, you have to use an OpenFileDialog
        b) otherwise, you can save to isolated storage

    Like so:

    public static void SaveUIElementToJpegImage(UIElement element)
            {
                // todo: pass myChart as a command parameter declaratively to be handled in PlotViewModel
                var b = new WriteableBitmap(element, null);
                using (var s = GetLocalFilestreamFromFileDialog(".jpg"))
                {
                    if (s != null)
                    {
                        SaveBitmapToJpeg(b, s);
                    }
                }
            }

    // as found here: http://blog.blueboxes.co.uk/2009/07/21/rendering-xaml-to-a-jpeg-using-silverlight-3/
            // also see attribution to SO post here: http://stackoverflow.com/questions/1139200/using-fjcore-to-encode-silverlight-writeablebitmap 
            private static void SaveBitmapToJpeg(WriteableBitmap bitmap, Stream fs)
            {
                //Convert the Image to pass into FJCore
                int width = bitmap.PixelWidth;
                int height = bitmap.PixelHeight;
                int bands = 3;

                byte[][,] raster = new byte[bands][,];

                for (int i = 0; i < bands; i++)
                {
                    raster[i] = new byte[width, height];
                }

                for (int row = 0; row < height; row++)
                {
                    for (int column = 0; column < width; column++)
                    {
                        int pixel = bitmap.Pixels[width * row + column];
                        raster[0][column, row] = (byte)(pixel >> 16);
                        raster[1][column, row] = (byte)(pixel >> 8);
                        raster[2][column, row] = (byte)pixel;
                    }
                }

                ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };

                FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);

                //Encode the Image as a JPEG
                MemoryStream stream = new MemoryStream();
                FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);

                encoder.Encode();

                //Move back to the start of the stream
                stream.Seek(0, SeekOrigin.Begin);

                //Get the Bytes and write them to the stream
                byte[] binaryData = new Byte[stream.Length];
                long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);

                fs.Write(binaryData, 0, binaryData.Length);
            }

            private static Stream GetLocalFilestreamFromFileDialog(string defaultExtension)
            {
                // save to user-specified location (requires user interaction)
                var dialog = new SaveFileDialog();
                dialog.DefaultExt = defaultExtension;
                dialog.Filter = dialog.DefaultExt + " File|*" + dialog.DefaultExt + "|All Files|*.*";

                if (dialog.ShowDialog() == true)
                {
                    return dialog.OpenFile();
                }
                return null;
            }

            private static Stream GetFileStreamFromIsolatedStorage(string filename, FileMode fileMode)
            {
                // save to IsolatedStorage with no user interaction
                var userstore = IsolatedStorageFile.GetUserStoreForApplication();
                var locations = userstore.GetDirectoryNames();
                return new IsolatedStorageFileStream(filename, fileMode,
                    IsolatedStorageFile.GetUserStoreForApplication());
            }
    • Edited by David Cuccia Saturday, August 21, 2010 5:10 AM edited dangling sentence
    •  
  • Saturday, August 21, 2010 6:58 AM
     
     

    I assume that you understand that LightSwitch doesn't have any native support for printing. I can't think of any reason that you couldn't create a Silverlight control to do it, though - I know I've seen a third-party Silverlight reporting component on Visual Studio Gallery, although I haven't tried it.

    Let us know how it turns out.

    Regards,

     


    Steve Hoag Microsoft aka the V-Bee

     


    MSFT *MUST* get on the printing .....    for example that 3rd party reporting costs to much and is just not that great!

    We must have a SIlverlight Report Viewer and Printing as a native part of SIlverlight by V5

    LightSwitch V1 needs to have that also......

    sure we can export data to Excell but that's just not the way to print stuff.... not for a decent app.

    Look at what folks can do an Access reports, SIlverlight needs to have at least the View / Print UI for SQL RS

     

     

  • Saturday, August 21, 2010 9:49 AM
     
     

    MSFT *MUST* get on the printing .....    for example that 3rd party reporting costs to much and is just not that great!

    We must have a SIlverlight Report Viewer and Printing as a native part of SIlverlight by V5

    LightSwitch V1 needs to have that also......

    sure we can export data to Excell but that's just not the way to print stuff.... not for a decent app.

    Look at what folks can do an Access reports, SIlverlight needs to have at least the View / Print UI for SQL RS

     

    +1 (I agree)



    Mario De Ghetto
    http://community.dotnetwork.it/mario.deghetto/Default.aspx
    http://deghetto.wordpress.com
  • Saturday, August 21, 2010 4:43 PM
     
     

    Me too! :)

    Is this suggestion already in MS Connect?


    André Alves de Lima
    Visite o meu site: http://andrealveslima.spaces.live.com
    Me siga no Twitter: @andrealveslima
  • Sunday, August 22, 2010 10:43 PM
     
     Answered
  • Sunday, August 22, 2010 11:01 PM
     
     
    Nice! :)
  • Sunday, August 22, 2010 11:20 PM
    Moderator
     
     

    Ditto. Thanks Michael - having this and the custom control walkthrough available when LightSwitch goes live to the public tomorrow is going to be a big plus!

    Thanks,

     


    Steve Hoag Microsoft aka the V-Bee
  • Monday, August 23, 2010 8:24 AM
     
     

    I posted a tutorial showing how to print here:

    http://lightswitch.adefwebserver.com/Blog/tabid/61/EntryId/3/Printing-With-LightSwitch.aspx


    That's nice, but what about serious reports like ledgers and others with multiple sub-groups/sub-totals, etc.

    VFP9 has a very powerful report builder, although it cannot be compared to Crystal Reports.

  • Monday, August 23, 2010 1:12 PM
     
     

    That's nice, but what about serious reports like ledgers and others with multiple sub-groups/sub-totals, etc.

    VFP9 has a very powerful report builder, although it cannot be compared to Crystal Reports.


    see:

    http://johnpapa.net/silverlight/advanced-silverlight-printing-strategies/

  • Tuesday, August 24, 2010 9:15 PM
     
     

    One of LightSwitch sample provided was an Invoice application. What's the point of having an invoice application without the ability to print it?

    I strongly suggest to add this feature to v1 final release. This is a major need for business applications.