PDA

View Full Version : Printing on Mac




renzil
Aug 6, 2009, 02:58 AM
Hi, I need to launch the Mac equivalent of a Windows 'PrintDlg()' when I click on a button. From the Cocoa documentation, I can see that this is easily done given an NSView/NSWindow using the 'print' message.

Suppose I don't have an NSView/NSWindow. Is there a way to bring up a Print Panel without it? Or do I have to create one and then call print?

Edit: Basically, I have an image (or some external resource) that I would like to print, without displaying it in a window/view.



robbieduncan
Aug 6, 2009, 03:58 AM
Hi, I need to launch the Mac equivalent of a Windows 'PrintDlg()' when I click on a button. From the Cocoa documentation, I can see that this is easily done given an NSView/NSWindow using the 'print' message.

Suppose I don't have an NSView/NSWindow. Is there a way to bring up a Print Panel without it? Or do I have to create one and then call print?

Edit: Basically, I have an image (or some external resource) that I would like to print, without displaying it in a window/view.

You don't have to display it: create an NSView in memory that "displays" the image and ask that to print. It should work even though the NSView is not in a window (and is not visible).

renzil
Aug 6, 2009, 07:03 AM
You don't have to display it: create an NSView in memory that "displays" the image and ask that to print. It should work even though the NSView is not in a window (and is not visible).

Did that in the following way:

NSRect frameRect = NSMakeRect(0.0f, 0.0f, 800.0f, 600.0f);
PrintView *view = [[PrintView alloc] initWithFrame:frameRect];
[view setHidden:YES]
[view print:nil];
[view release];

Here, PrintView is an extension of NSView and has a function 'initWithFrame' that invokes the super class' 'initWithFrame'.

Regardless of whether I call setHidden, the view is always shown. How do I hide it?

Also, I have overridden the 'drawRect' function to draw an image. Do I need to explicitly call 'drawRect' before calling 'print'?

Thanks for helping out.

robbieduncan
Aug 6, 2009, 07:59 AM
Where is the view visible? It should only ever be visible if you've added it to a visible NSWindow?

Anyway overriding drawRect: is the correct thing to do: when the print system wants to print it sets up the correct context and asks your view to drawRect:. Whatever you draw is what gets printed.

renzil
Aug 6, 2009, 12:22 PM
OK, I got the view to work fine and I'm able to see my imported image in the panel's preview. However, when I select 'Open PDF in Preview', I get a dialog saying 'Processing page: 1', with a progress indicator. The progress indicator goes to 100%, but the PDF never opens. Even if I click 'Cancel' on this dialog, it still hangs. Any idea what could be causing it?

robbieduncan
Aug 7, 2009, 04:25 AM
I've no idea. How big is the image? Perhaps turning it into a PDF is taking a long time?

The only thing I can suggest is to read the Printing Programming Topics for Cocoa (http://developer.apple.com/documentation/Cocoa/Conceptual/Printing/Printing.html) documentation and see if you can get any tips that way...