• Blog
  • Info Support
  • Career
  • Training
  • International Group
  • Info Support
  • Blog
  • Career
  • Training
  • International Group
  • Search
logo InfoSupport
  • Latest blogs
  • Popular blogs
  • Experts
      • All
      • Bloggers
      • Speakers
  • Meet us
  • About us
    • nl
    • en
    • .NET
    • 3D printing
    • Advanced Analytics
    • Agile
    • Akka
    • Alexa
    • Algorithms
    • Api's
    • Architectuur
    • Artificial Intelligence
    • ATDD
    • Augmented Reality
    • AWS
    • Azure
    • Big Data
    • Blockchain
    • Business Intelligence
    • Chatbots
    • Cloud
    • Code Combat
    • Cognitive Services
    • Communicatie
    • Containers
    • Continuous Delivery
    • CQRS
    • Cyber Security
    • Dapr
    • Data
    • Data & Analystics
    • Data Science
    • Data Warehousing
    • Databricks
    • DataOps
    • Developers life
    • DevOps
    • Digital Days
    • Digital Twin
    • Docker
    • eHealth
    • Enterprise Architecture
    • Event Sourcing
    • Hacking
    • Infrastructure & Hosting
    • Innovatie
    • Integration
    • Internet of Things
    • Java
    • Machine Learning
    • Microservices
    • Microsoft
    • Microsoft Bot Framework
    • Microsoft Data Platform
    • Mobile Development
    • Mutation Testing
    • Open source
    • Pepper
    • Power BI
    • Privacy & Ethiek
    • Python
    • Quality Assistance & Test
    • Quality Assurance & Test
    • Requirements Management
    • Scala
    • Scratch
    • Security
    • SharePoint
    • Software Architecture
    • Software development
    • Software Factory
    • SQL Server
    • SSL
    • Start-up
    • Startup thinking
    • Stryker
    • Test Quality
    • Testing
    • TLS
    • TypeScript
    • Various
    • Web Development
    • Web-scale IT
    • Xamarin
    • All
    • Bloggers
    • Speakers
Home » Silverlight 4 vs Flex 4: Printing
  • Silverlight 4 vs Flex 4: Printing

    • By Alex van Beek
    • Java 13 years ago
    • Java 0 comments
    • Java Java
    Silverlight 4 vs Flex 4: Printing

    I’ve wanted to do a “Silverlight vs Flex” series for quite a while now. Both Silverlight and Flex will reach version 4 during the first quarter of this year, at least, that’s what the roadmaps say. With both reaching version 4, this seems like the perfect time to do a comparison about how to implement certain features. Silverlight has been catching up with Flex, but in Silverlight 4, there are features in Silverlight that are just not possible in Flex (COM interop / Drag and Drop from the desktop), while other features that previously were only possible in Flex, are now also available in Silverlight 4. Whether the implementation of these features in Silverlight is as mature as in Flex is what we are going to find out in this series. We’re going to start with how printing is implemented on both platforms.

     

    Printing in Silverlight 4

    Central to printing in Silverlight 4 is the PrintDocument class. Printing in Silverlight always has the same steps:

    1. Create a PrintDocument object.
    2. Set the document name on the PrintDocument object.
    3. Attach eventhandlers for the PrintPage event and optionally for the StartPrint and EndPrint events.
    4. In the eventhandler for the PrintPage event, create the visual you want to print and assign it to the RootVisual property of the incoming PrintPageEventArgs object.
    5. Specify whether another page will be printed by assigning the HasMorePages property of the incoming PrintPageEventArgs object true or false.
    6. Repeat step 4 and 5 as long as there are pages to print.

    All of these steps are shown in the code behind of a Silverlight application that prints “Hello World” when a button is pressed:

       1: using System;

       2: using System.Windows;

       3: using System.Windows.Controls;

       4: using System.Windows.Printing;

       5:  

       6: namespace HelloPrinter

       7: {

       8:     public partial class MainPage : UserControl

       9:     {

      10:         public MainPage()

      11:         {

      12:             InitializeComponent();

      13:         }

      14:  

      15:         private void button1_Click(object sender, RoutedEventArgs e)

      16:         {

      17:             PrintDocument printDoc = new PrintDocument();

      18:             printDoc.DocumentName = "Hello World from Silverlight";

      19:             printDoc.PrintPage += new EventHandler<PrintPageEventArgs>(printDoc_PrintPage);

      20:             printDoc.Print();

      21:         }

      22:  

      23:         void printDoc_PrintPage(object sender, PrintPageEventArgs e)

      24:         {

      25:             StackPanel panel = new StackPanel() { Orientation = Orientation.Horizontal };

      26:             panel.Children.Add(new TextBlock() { Text = "Hello ", FontFamily = new System.Windows.Media.FontFamily("Arial"), FontSize = 12 });

      27:             panel.Children.Add(new TextBlock() { Text = "World", FontFamily = new System.Windows.Media.FontFamily("Arial"), FontSize = 12 });

      28:             e.PageVisual = panel;

      29:             e.HasMorePages = false;

      30:         }

      31:     }

      32: }

    Line 15 is the eventhandler that gets called when the button is pressed. What stands out is the fact that the visual you want to print, doesn’t have to be part of the visual tree that Silverlight renders to the screen. Another thing to consider is that Silverlight automatically stretches the RootVisual to the height and width of the page. You can check this size by reading the PrintableArea property of the incoming PrintPageEventArgs object. You can override this behavior by setting the height and width properties of the RootVisual explicitly.

    Printing in Flex 4

    Printing has been in Flex since the first version. Just like in Silverlight it has a couple of steps to follow, but the programming model is quite different. Printing in Flex is not event driven, this are the steps to take in Flex:

    1. Create a FlexPrintJob object, you can compare this more or less with Silverlight’s PrintDocument class.
    2. Specify with a boolean whether you want to print in vector graphics or in bitmap graphics. Bitmap supports transparency, vector not but prints in higher quality.
    3. Call the FlexPrintJob’s start() method. This will show the print dialog and returns true if the user clicked ok. If the start() method returned true, the printer is now waiting for us to send it some pages.
    4. Create a visual object you like to print.
    5. Add the visual object to the display list.
    6. Add the object to the job using FlexPrintJob’s addObject() method. Every visual object is another page, except when a visual object is larger than one page, Flex automatically detects this and prints the object on multiple pages.
    7. When you’re done adding pages / visual objects, call the send() method of the FlexPrintJob class.
    8. Remove the visual objects you added to the display list just for printing.

    All of these steps are shown in the code behind of a Flex application that prints “Hello World” when a button is pressed:

       1: protected function button1_clickHandler(event:MouseEvent):void

       2:             {

       3:                 var job : FlexPrintJob = new FlexPrintJob();

       4:                 job.printAsBitmap = false;

       5:                 if(job.start()) {

       6:                     var group : HGroup = new HGroup();

       7:                     group.height = job.pageHeight;

       8:                     group.width = job.pageWidth;

       9:                     var text : SimpleText = new SimpleText();

      10:                     text.text = "Hello ";

      11:                     text.setStyle("fontFamily", "Arial");

      12:                     text.setStyle("fontSize",12);

      13:                     group.addElement(text);

      14:                     text = new SimpleText();

      15:                     text.setStyle("fontFamily", "Arial");

      16:                     text.setStyle("fontSize",12);

      17:                     text.text = "World";

      18:                     group.addElement(text);

      19:                     

      20:                     addElement(group);

      21:                 

      22:                     job.addObject(group, FlexPrintJobScaleType.NONE);

      23:                     

      24:                     job.send();

      25:                     removeElement(group);

      26:                 }

    If you look at the code and my explanation above it you can see that Flex printing requires more steps. This is because of the following differences:

    1. In Flex , you can specify how to print, in bitmap mode or in vector mode.
    2. Flex doesn’t automatically scale the visual object to the page size. You can supply a constant defined in the FlexPrintJobScaleType class to specify how Flex should scale when you call the addObject() method, but “NONE” was the only option that printed anything. To scale the object to the page size I set the width and height of the HGroup to print to the “pageHeight” and “pageWidth” properties of the FlexPrintJob class.
    3. Components in Flex are only printed if they are part of the display list. Don’t worry, in the example above you don’t see the HGroup popping up and disappearing during the print. If you remove the visual object from the display list after printing, nobody will notice.

    Conclusion

    I think the printing in Flex is somewhat more mature than the printing in Silverlight, I like the option of the higher quality vector printing in Flex and that Flex automatically spreads large visual objects across multiple pages, instead of just cutting them off, like Silverlight does. Also, in Flex there is more support for printing than shown above, for example a lot of components have a prepareToPrint() method, which they can override to make themselves look better when printed, for example, by removing scrollbars (they have no use on paper :)), you can also customize how components print themselves by subclassing the component and overriding this method. Finally, there are components especially designed for printing, a PrintDataGrid for example, which makes sure that only whole rows are printed on each page. What I do like in Silverlight is the whole programming model, it feels more intuitive to me. Just for comparison, below are images showing the quality of the prints when printed to a .XPS file. The Flex printing with Vector graphics clearly has the best quality.

     

    Flex printing:image Silverlight printing:image
    Flex Vector printing:image  

    Share this

Alex van Beek

View profile

Related IT training

Go to training website

Related Consultancy solutions

Go to infosupport.com

Related blogs

  • It’s Java 20 Release Day! Here’s What’s New

    It’s Java 20 Release Day! Here’s What’s New Hanno Embregts - 1 week ago

  • Exploring sustainability in tech (without the guilt-tri…

    Exploring sustainability in tech (without the guilt-tri… Hanno Embregts - 4 months ago

  • Eleven crazy learnings from the Java 11 certification: …

    Eleven crazy learnings from the Java 11 certification: … Hanno Embregts - 6 months ago

Data Discovery Channel

  • Explainable AI - Break open the blackbox

  • Toekomstvaste microservice data architecturen

  • Modern Data Platform

Nieuwsbrief

* verplichte velden

Contact

  • Head office NL
  • Kruisboog 42
  • 3905 TG Veenendaal
  • T +31 318 552020
  • Call
  • Mail
  • Directions
  • Head office BE
  • Generaal De Wittelaan 17
  • bus 30 2800 Mechelen
  • T +32 15 286370
  • Call
  • Mail
  • Directions

Follow us

  • Twitter
  • Facebook
  • Linkedin
  • Youtube

Newsletter

Sign in

Extra

  • Media Library
  • Disclaimer
  • Algemene voorwaarden
  • ISHBS Webmail
  • Extranet
Beheer cookie toestemming
Deze website maakt gebruik van Functionele en Analytische cookies voor website optimalisatie en statistieken.
Functioneel Always active
De technische opslag of toegang is strikt noodzakelijk voor het legitieme doel het gebruik mogelijk te maken van een specifieke dienst waarom de abonnee of gebruiker uitdrukkelijk heeft gevraagd, of met als enig doel de uitvoering van de transmissie van een communicatie over een elektronisch communicatienetwerk.
Voorkeuren
De technische opslag of toegang is noodzakelijk voor het legitieme doel voorkeuren op te slaan die niet door de abonnee of gebruiker zijn aangevraagd.
Statistieken
De technische opslag of toegang die uitsluitend voor statistische doeleinden wordt gebruikt. De technische opslag of toegang die uitsluitend wordt gebruikt voor anonieme statistische doeleinden. Zonder dagvaarding, vrijwillige naleving door uw Internet Service Provider, of aanvullende gegevens van een derde partij, kan informatie die alleen voor dit doel wordt opgeslagen of opgehaald gewoonlijk niet worden gebruikt om je te identificeren.
Marketing
De technische opslag of toegang is nodig om gebruikersprofielen op te stellen voor het verzenden van reclame, of om de gebruiker op een website of over verschillende websites te volgen voor soortgelijke marketingdoeleinden.
Manage options Manage services Manage vendors Read more about these purposes
Voorkeuren
{title} {title} {title}