Printing on Mac

Discussion in 'Mac Programming' started by renzil, Aug 5, 2009.

  1. renzil macrumors newbie

    Joined:
    Aug 5, 2009
    #1
    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.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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).
     
  3. renzil thread starter macrumors newbie

    Joined:
    Aug 5, 2009
    #3
    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.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     
  5. renzil thread starter macrumors newbie

    Joined:
    Aug 5, 2009
    #5
    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?
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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 documentation and see if you can get any tips that way...
     

Share This Page