Search code examples
wpfprintingprintdialog

Printing a Collection in WPF


Is there any way to print in memory collection or variable size in WPF?

I am using the following code in which I print the ListView control. But when the content is larger than the vertical scroll bar takes over and cuts the content.

 PrintDialog printDialog = new PrintDialog();
                printDialog.ShowDialog();

                printDialog.PrintVisual(lvDocumentSummary, "testing printing!");

Solution

  • To print multiple pages you just need to use a class that implements DocumentPaginator FixedDocument is one of the more complex implementations, FlowDocument is a simpler one.

    FlowDocument fd = new FlowDocument();
    
    foreach(object item in items)
    {
        fd.Blocks.Add(new Paragraph(new Run(item.ToString())));
    }
    
    fd.Print();
    

    or

    PrintDialog pd = new PrintDialog();
    pd.PrintDocument(fd);