tags:

views:

383

answers:

2

In my app I have serveral forms that print specific data. For accomplish that I use the PrintDocument PrintPage Event.

If one report has more than 1 page... I set the hasMorePages flag to true... and the event is fired again and it is my responsability to continue printing from where I was at the end of the last page.

That is correct.

Now, I need to print all that reports in ONE PrintDocument, and I want to reuse the code of each one, so that in one print button the user will get all the reports printed. The idea is not print several documents.

What would be your approach to do this?

+1  A: 

Although I don't like the feel of it too much, the obvious solution is to make a print event that is an aggregator of other print events. You hook into the document print events and for each item that needs to print, you manually fire its print events.

I think you will want to make an interface like IPrintableForm which has a method DoPrintEvent(object sender, PrintPageEventArgs args);

then your aggregator gets a stack of forms that need to print and stores it in an instance variable and does something like:

private multiDocPageEventHandler(object sender, PrintPageEventArgs args)
{
    if (printStack == null) { // all done
        throw new Exception("This should never happen.");
    }
    else { // send to top of stack
        printStack.Peek().DoPrintEvent(sender, args);
        if (!args.HasMorePages) {
             printStack.Pop();
        }
        args.HasMorePages = printStack.Count > 0;
        if (!args.HasMorePages) {
            printStack = null;
        }
    }
}
plinth
I like your approach... this night I'll give it a try... and let you know. As I have a quite big product such changes take time. THANKS
Romias
It worked very well... the inner forms that has more that one page are printed along the ones that have just one... so exactly what I needed. Now I just need to do some modifications for some Forms that print more than on report but that is twekening your code. Thanks again
Romias
A: 

The solution suggested by plinth works out pretty well if the print job is sent to a physical printer. But if the user is redirecting it to a virtual printer such as PDF printers, then the users will still end up having multiple files!

Raghu
I suppose that depends of the virtual printer... I use PDFFactory and works fine.
Romias