views:

218

answers:

2

Hi there,

I want to print a specific NSView. When I do this, I wish to add content to the header of the print page.

e.g. If the NSView contains a picture of a cat, when I press print, print preview shows up with the picture of the cat. I want the print out to be a picture of a cat, with the caption: "Cat" in the header, which I do not want visible on the original NSView.

Also, if this is possible, is it also possible to add images too?

Thanks!

+2  A: 

If you want to draw things differently on the screen than the printer, you can use the isDrawingToScreen method in your drawRect: method.

Eg:

if (![[NSGraphicsContext currentContext] isDrawingToScreen]) {
//draw printer headers and images
}
Ellie P.
Thanks, I just required the header at the moment, but I might need to use this in the future!
Michael
+3  A: 

You can overwrite the method - (NSAttributedString *)pageHeader in your NSView subclass. See Apple's documentation here.

Note that headers are generated only if the user defaults contain the key NSPrintHeaderAndFooter with the value YES.

As for images, those can be added to a NSAttributedString using a NSTextAttachment.

Johan Kool
Thank you! Very useful
Michael
I overwrote the method pageHeader, with a custom subclass called PrintView. This works, but there's a really annoying warning:"Incompatible Objective-C types 'struct PrintView *', expected 'struct NSView *' when passing argument 1 of 'printOperationWithView:printInfo:' from distinct Objective-C typeThe printout is fine, and the program works, but is there a way to get rid of the message?
Michael
Sounds like the compiler doesn't know that your PrintView is a subclass of NSView. Did you import PrintView.h where you called the print method?
Johan Kool
Oh my gosh, I can't believe I didn't do that!Thanks again!
Michael