Printing Invisible NSView

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

Then I moved my custom NSView to an existing application. I hope that when the user hits the seal, he will print this certificate. Simple enough. I realized that I might have an NSView pointer in my controller code. Then at initialization I would fill the pointer. Then, when someone wants to print the certificate, it will print. The problem is that all my drawing code is in the "drawRect" method. This method is not called because this view is never displayed in a window.

I heard that others use invisible NSView objects for printing only. What do I need to do? I really do not want to show this view on the screen.

Rodger

+3
source share
2 answers

You do not need to create a view in advance, you can create it when necessary.

If you have a document-based application and a presentation that you want to reset to a printer, then in our MyDocument(or another, what you call it), which expands NSDocument, you should implement:

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

The view then uses the standard drawRect:for drawing.

, PeopleView table , NSDictonary employees:

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

27 "" Hillegass Cocoa Mac OS X.

+2

...

- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)ps error:(NSError **)e
{
    NSLog(@"MyDoc printOp");
    StretchView * view = [[StretchView alloc] init];
    NSPrintInfo * printInfo = [self printInfo];
    NSPrintOperation * printOp
    = [NSPrintOperation printOperationWithView:view
                                     printInfo:printInfo];
    [view release];
    return printOp;
}

...

unning…
2010-03-15 10:10:42.251 spelling_tutor[6772:a0b] MyDoc printOp

nslog . , . .

0

Source: https://habr.com/ru/post/1736778/


All Articles