< Previous PageNext Page > Hide TOC

Printing Documents

An application that uses NSDocument to manage its documents gains additional infrastructure to handle document printing.

Because print settings may be different for different documents, each instance of NSDocument has its own NSPrintInfo object, which is accessed with the printInfo and setPrintInfo: methods.

Contents:

Using a Page Setup Panel with NSDocument
Printing a Document


Using a Page Setup Panel with NSDocument

NSDocument implements the runPageLayout: method to handle the Page Setup menu command instead of letting NSApplication handle it. When it receives this message, it gets the document’s NSPrintInfo object and invokes runModalPageLayoutWithPrintInfo:delegate:didRunSelector:contextInfo: to display the Page Setup panel. To give your NSDocument subclass an opportunity to customize the NSPageLayout object, it passes the object to preparePageLayout: before displaying the panel. Override this method if you want to add an accessory view to the panel.

When the panel is dismissed with the OK button, NSDocument checks the return value of its shouldChangePrintInfo: method. If it returns YES, the default, the document’s NSPrintInfo object is updated to reflect the new print settings. You can override this method to validate the new settings and return NO if the new settings should be discarded.

Printing a Document

In an NSDocument-based application, the Print menu command normally sends a printDocument: message, which only NSDocument implements. The NSDocument object associated with the application’s main window receives the message and invokes printShowingPrintPanel: with YES as its argument. Because NSDocument does not manage the view or views displaying your document data, it cannot provide a default implementation for this printing method. Therefore, your NSDocument subclass must override printShowingPrintPanel: to create and run the print operation for the document.

When printing a document, you can use either a view object already displaying your data in a window or a special view object that you create just for printing. For example, a simple text editor may display text in a window the same way it prints it on the page, so it can use the same view for both cases. Alternatively, a database program’s main window may contain an interface for browsing and editing the database, while the printed data needs to be formatted as a table. In this case, the document needs separate views for drawing in a window and for printing to a printer. If you have a good Model-View-Controller design, you can easily create a custom view that can draw the printer-specific version of your data model and use it when creating the print operation.

After setting up the print operation, invoke NSDocument’s runModalPrintOperation:delegate:didRunSelector:contextInfo: method to run the print operation and display the NSPrintPanel as a sheet on the document’s window.

- (void)printShowingPrintPanel:(BOOL)showPanels {
    // Obtain a custom view that will be printed
    NSView *printView = [self printableView];
 
    // Construct the print operation and setup Print panel
    NSPrintOperation *op = [NSPrintOperation
                printOperationWithView:printView
                printInfo:[self printInfo]];
    [op setShowPanels:showPanels];
    if (showPanels) {
        // Add accessory view, if needed
    }
 
    // Run operation, which shows the Print panel if showPanels was YES
    [self runModalPrintOperation:op
                delegate:nil
                didRunSelector:NULL
                contextInfo:NULL];
}


< Previous PageNext Page > Hide TOC


© 2002, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-06-28)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.