How to Print Text or Banner in WPF using F#

Today I am explaining that how you can print a Banner or Text in WPF using F#. Document, Banner or Text Printing Needs a PrintDialogBox and Print Commands. Here in F# you need only write code and add some References to Perform a print operation. Steps are given Below.

If you want to Print the Text or Banner. You will write the below code Block

// For Printing Text or Banner
let stk = new StackPanel()
let wndw = new Window(Title="How to Print Banner In WPF using F#",
                        SizeToContent = SizeToContent.WidthAndHeight,
                        Content=stk)
 
let txtbx = new TextBox(Width=250.0,
                         Margin= new Thickness(12.0))
txtbx |> stk.Children.Add |> ignore
 
let butn = new Button(Content = "Click To Print ",
                     Margin = new Thickness(12.0),
                     HorizontalAlignment = HorizontalAlignment.Center)
butn |> stk.Children.Add |> ignore
 
butn.Click.Add(fun _ ->
   let dlg = new PrintDialog()
   let retval = dlg.ShowDialog().GetValueOrDefault()
   if retval then
      let prntkt = dlg.PrintTicket
      prntkt.PageOrientation <- new Nullable<PageOrientation>(PageOrientation.Portrait)

If you want to Create newBanner Paginator Object. The below Code Block will Work.

// Create new BannerDocumentPaginator object.
      let paginator = new DocPrintBann(Text=txtbx.Text)
      paginator.PageSize <- new Size(dlg.PrintableAreaWidth,
                                     dlg.PrintableAreaHeight)
                                    
      dlg.PrintDocument(paginator, "Banner: " + txtbx.Text))
txtbx.Focus() |>ignore   

Note: The Complete code you can see in Step 4.                    

Step 1: Firstly Open a new project in F# using Visual Studio 2010. Select F# WPF Application template and give name to the project like below image.

New Project Dialog Box

Step 2: Now add below given references to your project by right clicking on your project in solution explorer.

  • PresentationCore

  • PresentationFramework

  • ReachFramework

  • System

  • System.Printing

  • System.Xaml

  • WindowsBase

Step 3: When you add all these References, your Solution Explorer will look like below image.

Solution Explorer

Step 4: Then click on Program.fs file in Solution Explorer and write below given code in Program.fs window, Your window will look like below images.

PrintBanner Example code part 1  

PrintBanner Example code part 1.1

PrintBanner Example code part 1.1.1

#light 

open System
open System.Globalization
open System.Printing
open System.Windows
open System.Windows.Controls
open System.Windows.Documents
open System.Windows.Input
open System.Windows.Media

 
type DocPrintBann() = class
   inherit DocumentPaginator()
  
   let mutable text = ""
   let mutable fce = new Typeface("")
   let mutable mxSze = new Size(0.0,0.0)
   let mutable (sizePage:Size) = new Size(0.0,0.0)
  
   let gtFrmtTxt (ch:char) (face:Typeface) (em:double) =
      new FormattedText(ch.ToString(),
                        CultureInfo.CurrentCulture,
                        FlowDirection.LeftToRight,
                        face,
                        em,
                        Brushes.Black)
 
   member this.Text
      with get() = text
      and set value = text <-value
  
   member this.Typeface
      with get() = fce
      and set value = fce <-value
 
   override this.IsPageCountValid     
      with get() =
         text.ToCharArray() |> Seq.iter (fun ch ->
            let formtxt = gtFrmtTxt ch fce 100.0
            mxSze.Width <- Math.Max(mxSze.Width,formtxt.Width)
            mxSze.Height  <- Math.Max(mxSze.Height ,formtxt.Height )
            )
         true
     
   override this.PageCount    
      with get() = if text = null then 0 else text.Length
 
   override this.PageSize     
      with get() = sizePage
      and set value = sizePage <- value
     
   override this.Source     
      with get() = null
 
   override this.GetPage (numPage) =
      let vsDrw = new DrawingVisual()
      using (vsDrw.RenderOpen()) (fun dc ->
         let factor = Math.Min((this.PageSize.Width - 96.0) / mxSze.Width,
                               (this.PageSize.Height - 96.0) / mxSze.Height)
         let formtxt = gtFrmtTxt (text.Chars numPage) fce (factor*100.0)
         let ptText = new Point((this.PageSize.Width - formtxt.Width) / 2.0,
                                (this.PageSize.Height - formtxt.Height) / 2.0)
         dc.DrawText(formtxt,ptText))
      //
      new DocumentPage(vsDrw)
   
end
 
// For Printing Text or Banner
let stk = new StackPanel()
let wndw = new Window(Title="How to Print Banner In WPF using F#",
                        SizeToContent = SizeToContent.WidthAndHeight,
                        Content=stk)
 
let txtbx = new TextBox(Width=250.0,
                         Margin= new Thickness(12.0))
txtbx |> stk.Children.Add |> ignore
 
let butn = new Button(Content = "Click To Print ",
                     Margin = new Thickness(12.0),
                     HorizontalAlignment = HorizontalAlignment.Center)
butn |> stk.Children.Add |> ignore
 
butn.Click.Add(fun _ ->
   let dlg = new PrintDialog()
   let retval = dlg.ShowDialog().GetValueOrDefault()
   if retval then
      let prntkt = dlg.PrintTicket
      prntkt.PageOrientation <- new Nullable<PageOrientation>(PageOrientation.Portrait)
     
      // Create new BannerDocumentPaginator object.
      let paginator = new DocPrintBann(Text=txtbx.Text)
      paginator.PageSize <- new Size(dlg.PrintableAreaWidth,
                                     dlg.PrintableAreaHeight)
                                    
      dlg.PrintDocument(paginator, "Banner: " + txtbx.Text))
txtbx.Focus() |>ignore                       
 
 
#if COMPILED
[<STAThread()>]
do
    let app =  Application() in
    app.Run(wndw) |> ignore
#endif

Step 5: Now press F5 to execute the code.

Output

PrintBanner Output1

PrintBanner Output2

PrintBanner Output3

PrintBanner Output4

PrintBanner Output5

Summary

In this article I have Discussed that how to Print a Banner or Text in WPF using F#.

Up Next
    Ebook Download
    View all
    Learn
    View all