Romias
Romias

Reputation: 14133

Printing multiple pages of different data

In my app I have several 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.

Now, I need to print all those 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 to not print several documents.

What would be your approach to do this?

Upvotes: 3

Views: 1096

Answers (1)

plinth
plinth

Reputation: 49189

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;
        }
    }
}

Upvotes: 2

Related Questions