开发者

Printing an invisible NSView

开发者 https://www.devze.com 2022-12-22 16:32 出处:网络
Initially I created a simple program with a custom NSView.I drew a picture (certificate) and printed it!beautiful!Everything worked great!

Initially I created a simple program with a custom NSView. I drew a picture (certificate) and printed it! beautiful! Everything worked great!

I then moved my custom NSView to an exis开发者_运维技巧ting application. My hope was that when a user hit print it would print this certificate. Simple enough. I figured a could have a NSView pointer in my controller code. Then at initialization I would populate the pointer. Then when someone wanted to print the certificate it would print. The problem is that all of my drawing code is in the "drawRect" method. This method doesn't get called because this view is never displayed in a window.

I have heard that others use non-visible NSView objects just for printing. What do I need to do? I really don't want to show this view to the screen.

Rodger


You don't have to create the view in advance, you can create it when needed.

If you have Document Based application and a view that you want to dump to printer, then in our MyDocument (or whatever you call it) that extends NSDocument you would implement:

- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)ps
                                           error:(NSError **)e

The view then uses standard drawRect: for drawing.

Example, here PeopleView just draws a table of people details, it takes a NSDictonary of people employees here:

- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)ps
                                           error:(NSError **)e
{
    PeopleView * view = [[PeopleView alloc] initWithPeople:employees];
    NSPrintInfo * printInfo = [self printInfo];
    NSPrintOperation * printOp
        = [NSPrintOperation printOperationWithView:view
                                         printInfo:printInfo];
    [view release];
    return printOp;
}

You can look for more details in chapter 27, Printing, in Hillegass' Cocoa Programming For Mac OS X.

0

精彩评论

暂无评论...
验证码 换一张
取 消