How to print a List<string> in Silverlight 4 Beta over multiple pages?!

On several people are asking how to print the values of a DataGrid in Silverlight. You cannot just assign the DataGrid to the PageVisual-Property of the PrintPageEventArgs. This would just print the DataGrid as it is on one page. The data wouldn’t be splitted on several pages, cause there’s no paging logic to use. You’ve to write this logic.

You’re responsible for the paging! But how to do it? In this post I’ll give you a little idea how it could work by simple printing out a List of string-values. I won’t talk a lot about the details. Just look at the code below:

public partial class MainPage : UserControl
  private List _list;
  private const double ROWHEIGHT = 20;
  private const double PAGEMARGIN = 30;
  public MainPage()
    _list = new List();

    for (int i = 1; i < 101; i++)
      _list.Add(i + " thanks to Thomas for this printing sample");

  private void Button_Click(object sender, RoutedEventArgs e)
    _currentIndex = 0;

    var pd = new PrintDocument();
    pd.DocumentName = "AListFromThomas";
    pd.PrintPage += pd_PrintPage;


  private int _currentIndex;
  private double _currentTop;
  private double _availableSpace;
  void pd_PrintPage(object sender, PrintPageEventArgs e)
    var pageRoot = new Canvas();
    e.PageVisual = pageRoot;

    _currentTop = PAGEMARGIN;
    _availableSpace = e.PrintableArea.Height - PAGEMARGIN*2;
    while (_currentIndex < _list.Count)
      var txt = new TextBlock { Text = _list[_currentIndex] };

      if (ROWHEIGHT > _availableSpace)
        e.HasMorePages = true;

      txt.SetValue(Canvas.TopProperty, _currentTop);
      txt.SetValue(Canvas.LeftProperty, PAGEMARGIN);
      _currentTop += ROWHEIGHT;
      _availableSpace -= ROWHEIGHT;

When the Button_Click-Eventhandler is executed, the List gets printed over 5 pages. You can easily print it to PDFCreator or XPS Printer to test it. The output looks like this:


Download the source here and enjoy. Give me feedback by entering a comment to this blogentry or via email on

Share this post

Comments (4)

  • neha Reply

    Nice article, helped me a lot. Thanks

    June 4, 2010 at 4:08 am
  • Rasheed Reply


    We are building a large scale Silverlight 4.0 based LOB application for one of our clients. We have a requirement to design data entry forms in Silverlight. After the user fills and submits the data, the form should be converted to PDF. The PDF output should *exactly* match the Silverlight form in terms of document’s layout, fonts, and other visual styles both on screen and printed media. This is required to meet the PDF specification defined by government agencies.

    We would like to know if Silverlight 4.0 is capable of meeting this requirement as this is one of the key scenarios that the system should address. Can you please provide some direction here?


    July 9, 2010 at 3:31 pm
  • Virendra Kumar Reply

    I am developing silverlight application . my canvas size is like 8000(width) & 2000(height) .i want to print entire canvas in single page but it is not clear even i am generating pdf for printing .
    i would like to know how we can print entire canvas in single page with clearity.


    June 27, 2012 at 1:49 pm
    • Thomas Claudius Huber Reply

      Hi Virendra,

      Normally I scale the Canvas down with a Rectangle and a VisualBrush. Maybe you can create a Bitmap and send it to a server-side reporting engine that creates a pdf with a high dpi-setting

      July 3, 2012 at 7:36 am

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.