tags:

views:

2910

answers:

6

Hi,

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!");
A: 

Interesting, Is the ListView virtualized? If it is, the object are not drawn, that is a possibility. Take a look at the Printing example from Petzold.

Artur Carvalho
I have checked out that example but he is using files to print. I have an in memory collection to print!
azamsharp
The Petzold link is no longer present
Tim Trout
A: 

Here is my solution to this problem. It is kinda shaky but works for my scenario.

I read my collection and transform it into a string. The whole collection now resides in a StringBuilder object. Next, I saw the text/string into a file on the client's machine and then run the notepad process with /p to print the contents of the file.

It works and it prints the contents successfully.

Finally, there is a timer which is called after 5 seconds and which removes the file. Basically within 5 seconds the request is already sent to the printer queue. But a better solution will be to make sure that the print job has been processed this way you will be 100% sure that the job has been performed.

azamsharp
External application invocation to print things, with race conditions? No way! And to think you run a development knowledgebase website! Scary stuff.
OJ
You can easily achieve this by using FlowDocument And FixedDocuments!
azamsharp
That's a really bad way of doing this, you only a FlowDocument with a BlockUIContainer. If it's a particularly long list, use a FixedDocument.
Echilon
A: 

If you want nice printing from WPF you need to build a FixedDocument and print that, unfortunately it can be very complex depending on what you are trying to print.

There's some example code that creates a FixedDocument here: http://www.ericsink.com/wpf3d/B_Printing.html

Nir
+2  A: 

FixedDocument supports DataBinding (other than FlowDocument) like any other xaml document. just host the listview in a fixeddocument and display it in a DocumentViewer (which has built-in print support).

however, if your list is too long for one page, FixedDocument does not automatically generate a new page (like flowdocument does). therefore you have to create a new page maually with code, as this cannot be done in pure xaml.

Joachim Kerschbaumer
+5  A: 

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);
Ifeanyi Echeruo
A: 

PrintDialog dlg = new PrintDialog(); Title = "testing Printing"; if ((bool)dlg.ShowDialog().GetValueOrDefault()) { dlg.PrintVisual(lvDocumentSummary, Title); }

priyanka.sarkar
Did you just copied the content of the question?
Padu Merloti
Yes, for the love of God please unmark this as the correct answer. It's just junk.
Tim Keating