Search code examples
xcodecocoaprintingnsviewnsprintoperation

Issues with printing a custom view to printer


Working on printing a custom view and I am having difficulties - hope someone can point me in the right direction. I have read just above every Apple document but it's just not working. I created a simple program to test the printing, I subclassed NSView (MainView) and added the following to the drawRect method.

@implementation MainView


- (void)drawRect:(NSRect)dirtyRect{
  [super drawRect:dirtyRect];

  NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Hi"];

  [myString drawInRect:dirtyRect];

}

I created a custom view on the window and set it's class to MainView.

I run the program and the text appears - good so far.

When I click the print from the pull down, dialog opens and the preview shows the entire window - it includes the upper bar with min, max buttons. So, this is question #1, why does it have the entire window vs. just the view?

Second, I created a print routine in the MainView and linked a pulldown menu item to it.

-(IBAction) printTheView{

NSRect r = [self bounds];
[[NSPrintOperation printOperationWithView:self] runOperation];
[self dataWithPDFInsideRect:r];

}

This yields nothing in the print preview. I have tried various ways and nothing has worked (ex: NSSavePanel and beginSheetForDirectory... method). The code above was my last attempt.

In summary, I have two questions: 1) Why is the first one printing the entire window, vs just the view? 2) Why won't my custom print method work?

I have successfully printed a NSTextView, but can't seam to get this one. Any suggestions/pointers/kicks would be appreciated.

Thank You.

[EDIT]------

I have it partially working. I was binding the Custom View through an NSObject to the printTheView method. In IB, I bound the pull down menu item to the Custom View object on the window. This works.

However, I cannot bind a button on the window to the print method the same way - i.e. CNTL CLICK and drag from button to the object.

Thoughts?


Solution

  • I resolved this by creating a separate controller object. So I have the following:

    @interface ViewController : NSObject
    
    
    @interface MainView  :  NSView
    

    Made the ViewController a delegate for MainView and created an IBOutlet for the custom view on the window.

    IBOutlet MainView *view;
    

    Added this print method to the ViewController:

    -(IBAction)printToPDF:(id)sender{
    [view printPDF];
    }
    

    Added a button to the window and linked it to the printToPDF method in ViewController.

    In MainView, added the following print method:

    -(void)printPDF{
    NSRect r = [self bounds];
    [[NSPrintOperation printOperationWithView:self] runOperation];
    [self dataWithPDFInsideRect:r];
    }
    

    This all works. However the remaining question is why the print menu items prints the entire window vs the view. I have put that into a separate question.