Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links


Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories


Post Categories

.NET Framework


Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

WPF: Printing FlowDocuments in Visual Basic 2008

In a previous post we saw how easy is printing Windows Presentation Foundations UI controls. But probably the most common scenario is printing documents, another simple task in WPF.


Also in this case we recur to the PrintDialog class but now we'll use a method called PrintDocument. In fact it's possible to call it for printing FlowDocument objects, a particular kind of dynamic WPF documents. 


Let's start from the base, a un FlowDocument that will contain, in this particular example, some text taken from my Italian language article about the ADO.NET Data Services on the VB T&T community. Once you create a new WPF project in Visual Basic 2008, type the following XAML (if you prefer you can grab just a few lines of text):





            <RowDefinition Height="30"/>



        <FlowDocumentReader Grid.Row="0">

            <FlowDocument Name="flowDoc" FontFamily="Verdana" FontSize="12"








                <Paragraph FontWeight="Bold">

                    Introduzione agli ADO.NET Data Services con Visual Basic 2008 - Seconda parte: il client WPF</Paragraph>

                <Paragraph Foreground="Red">

                    a cura di Alessandro Del Sole (requisiti: conoscenze di base su LINQ, WPF e WCF)</Paragraph>

                <Paragraph FontWeight="Bold">





<Hyperlink NavigateUri="">prima parte</Hyperlink> di questa mini serie di articoli introduttivi agli ADO.NET Data Services in Visual Basic 2008, abbiamo visto come creare con Visual Studio un servizio basato su WCF per esporre in rete dei dati attraverso un approccio REST (REpresentational State Transfer) sfruttando il modello a oggetti offerto da ADO.NET Entity Framework.


In questa seconda parte completeremo l'introduzione all'argomento passando alla creazione di un'applicazione client basata su Windows Presentation Foundation e faremo utilizzo di LINQ to Data Services per interrogare i dati.


Vedremo aspetti decisamente interessanti della creazione di applicazioni data-centric con WPF, ma il codice da scrivere stavolta non sarà poco e richiederà una certa dimestichezza con WPF e LINQ, poiché, essendo di media complessità, non potremo soffermarci a spiegare ogni aspetto delle menzionate tecnologie. Ci soffermeremo in particolare su tutto ciò che riguarda gli ADO.NET Data Services, limitandoci ad accenni su tutto il resto.


Anche in questo caso potete utilizzare le edizioni Express di Microsoft Visual Studio 2008.

Con riferimento a quanto visto la volta precedente, è possibile utilizzare Visual Web Developer Express per creare il servizio, mentre, con riferimento al presente articolo, è possibile utilizzare Visual Basic 2008 Express che permette di creare le applicazioni client alle quali è possibile aggiungere riferimenti ai servizi WCF.




        <Button Width="100" Height="25" Grid.Row="1" Content="Print" Name="Button1" />


As you perhaps remember, each  FlowDocument must be stored inside a viewer and the FlowDocumentReader is one of the controls bound to view FlowDocuments. Moreover, each FlowDocument is structured in some Paragraph objects; each of them is customizable assigning its properties. Formatting the document is quite simple, because in this case we're going to focus printing instead of text formatting. I'd just like to underline the usage of an HyperLink inside the document to add an hyperlink according to the typical XAML style. Now the WPF designer should appear as follows:




Now we can switch to the code behind file, Window1.xaml.vb, to define a Click event handler for the printing button:


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

        Dim pDialog As New PrintDialog


        If pDialog.ShowDialog = True Then


            Dim paginator As DocumentPaginator = CType(flowDoc, IDocumentPaginatorSource).DocumentPaginator


            pDialog.PrintDocument(paginator, "Test printing")

        End If

    End Sub


The DocumentPaginator is an object allowing to create multiple pages items starting from a single document and is the type of object supported by the PrintDocument method, so a cast is needed.


If you try to run the sample application, you can click the Print button to print the document. The following picture shows how the document is printed using the Windows Vista's virtual XPS printer driver:



So it's simple, isn't it? J



Print | posted on venerdì 2 gennaio 2009 19:52 | Filed Under [ Visual Basic Windows Presentation Foundation ]


No comments posted yet.

Post Comment

Please add 1 and 1 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET