Friday, February 11, 2011

Print Mulitple pages using Silverlight

When you have a single page to print in silverlight, it is straight forward code. However if you want to print multiple pages, developer need to add some logic to the code. We should let printer know how each page is spiltted.


Single Page Print: Let us write a sample code to print single page. For this purpose, create a simple silverlight page.
  • Place a button on silverlight page.
  • Set content (label of button) to Print.
  • Trap click event of button.
Button Width="65" Content="Print" Height="25" Click="PrintButton_Click" HorizontalAlignment="Right" Margin="3"/

Handle click event of Print Button in code behind.


private void PrintButton_Click(object sender, RoutedEventArgs e)
{
 PrintDocument docToPrint = new PrintDocument();
 docToPrint.PrintPage += new EventHandler(docToPrint_PrintPage);
 docToPrint.EndPrint += new EventHandler(docToPrint_EndPrint);
 docToPrint.Print("Remittance Notice");
}


  • Create an object of PrintDocument class.
  • Trap PrintPage and EndPrint events of PrintDocument object.
    • PrintPage will trigger when user invokes print functionality.
    • EndPrint will be triggered after printing completes.
  • Call Print() method of PrintDocument object. This will send print request to printer. Print method takes document name as parameter.
int indexValue = 0; // declare a global variable for print line count.

void docToPrint_PrintPage(object sender, PrintPageEventArgs e)
{
 StackPanel itemHost = new StackPanel();
 while(indexValue < 40) // Loop throught for Fourty Lines.
 {
  TextBlock tb = new TextBlock();
  tb.Text = "This is my line number " + indexValue.ToString();
  itemHost.Children.Add(tb); // Add TextBlock to Stack Panel.
  indexValue++;
 }
  e.PageVisual = itemHost; // Set Stackpanel to PageVisual of    PrintPageEventArgs.
}


Above method is called then PrintPage event of PrintDocument class invoked. In this method,
  • Create an object of StackPanel. This will be the print document.
  • Add all the content to StackPanel which is required for printing.
  • In the above case we are printing Fourty lines
void docToPrint_EndPrint(object sender, EndPrintEventArgs e)
{
 // todo: if required write needed after printing document.
}
Above method is called after printing is completed.


Multiple Page Print: The above code works if it is printing only one page. In the above case we are printint fourty lines of document. In case if user wants to print 500 lines of document, above code will fail. The above code will print first page only and ignores remaning pages. To achived that we need to write below code.

int indexValue = 0;
void docToPrint_PrintPage(object sender, PrintPageEventArgs e)
{
 StackPanel itemHost = new StackPanel();
 while(indexValue < 500) // Loop throught for Five Hundered Lines.
 {
  TextBlock tb = new TextBlock();
  tb.Text = "This is my line number " + indexValue.ToString();
  itemHost.Children.Add(tb);
  itemHost.Measure(new Size(e.PrintableArea.Width,   double.PositiveInfinity)); // To update designed size of StackPanel
  if (itemHost.DesiredSize.Height > e.PrintableArea.Height) // Check if Stack   Panel Height is more than printable area height
 {
  itemHost.Children.Remove(tb);
  e.HasMorePages = true; // Set HasMorePages to true. This will split(page break) the page.
  break; // Come out of while loop.
 }
   indexValue++;
}
 e.PageVisual = itemHost;
}


So the trick to print multiple pages is: Spilt print document after it reaches maximum height of a given page height. Then set HasMorePages of PrintPageEventArgs object to true.







4 comments:

  1. Visual Studio 2010 has now an option to copy code as HTML. It integrates with Windows Live Writer nicely... That should help you with formatting your code. :)

    ReplyDelete
  2. Vyas,
    Thanks for the input. I will try that.

    ReplyDelete
  3. hey thnx man...it helped me solving my problem...a day saved!! :)

    ReplyDelete
  4. But this is not possible to print the document

    ReplyDelete