Printing

  • I want to print programatically. So I do [NSPrintOperation
    setShowsPrintPanel: NO].
    But how do I tell the NSPrintOperation about the destination (fax,
    printer, file, etc.)? It seems always to print to the default
    printer, which is not what I want.

    Or: where is [NSView dataWithPDFInsideRect: (NSRect)aRect
    usingPrintInfo: (NSPrintInfo *)aPrintInfo] ?
    The existing method NSView dataWithPDFInsideRect:] prints all of the
    view into one huge page, which is not what I want.

    Gerriet.
  • On 12 Oct 2006, at 20:58, Gerriet M. Denkmann wrote:

    > I want to print programatically. So I do [NSPrintOperation
    > setShowsPrintPanel: NO].
    > But how do I tell the NSPrintOperation about the destination (fax,
    > printer, file, etc.)? It seems always to print to the default
    > printer, which is not what I want.

    This is what I do do print something without displaying the Print panel:

    - (void)markPageViewsDirtyForPrinting
    {
        NSEnumerator *viewEnum;
        NSView *subView;

        // Force the subviews to redisplay for printing
        viewEnum = [[[scrollView documentView] subviews] objectEnumerator];
        while ((subView = [viewEnum nextObject]))
            [subView setNeedsDisplay: YES];
        [[scrollView documentView] setNeedsDisplay: YES];
    }

    - (BOOL)writeToFile:(NSString *)aPath
    {
        BOOL success = YES;
        NSPrintInfo *tempPrintInfo;
        NSPrintOperation *op;

        tempPrintInfo = [[[self printInfo] copyWithZone: [self zone]]
    autorelease];
        [tempPrintInfo setJobDisposition: NSPrintSaveJob];
        [[tempPrintInfo dictionary] setObject: aPath
                                        forKey: NSPrintSavePath];
        [[tempPrintInfo dictionary] setObject: [NSNumber numberWithBool:
    YES]
                                        forKey: NSPrintAllPages];
        op = [NSPrintOperation printOperationWithView: [scrollView
    documentView]
                                            printInfo: tempPrintInfo];
        [op setShowPanels: NO];
        [self markPageViewsDirtyForPrinting];
        success = [op runOperation];

        [self setDocumentEdited: NO];

        return success;
    }

    Annard
  • On 12 Oct 2006, at 23:12, <ab_lists...> wrote:
    >
    > On 12 Oct 2006, at 20:58, Gerriet M. Denkmann wrote:
    >
    >> I want to print programatically. So I do [NSPrintOperation
    >> setShowsPrintPanel: NO].
    >> But how do I tell the NSPrintOperation about the destination (fax,
    >> printer, file, etc.)? It seems always to print to the default
    >> printer, which is not what I want.
    >
    > This is what I do do print something without displaying the Print
    > panel:
    >
    > - (void)markPageViewsDirtyForPrinting
    > {
    > NSEnumerator *viewEnum;
    > NSView *subView;
    >
    > // Force the subviews to redisplay for printing
    > viewEnum = [[[scrollView documentView] subviews]
    > objectEnumerator];
    > while ((subView = [viewEnum nextObject]))
    > [subView setNeedsDisplay: YES];
    > [[scrollView documentView] setNeedsDisplay: YES];
    > }
    >
    > - (BOOL)writeToFile:(NSString *)aPath
    > {
    > BOOL success = YES;
    > NSPrintInfo *tempPrintInfo;
    > NSPrintOperation *op;
    >
    > tempPrintInfo = [[[self printInfo] copyWithZone: [self zone]]
    > autorelease];
    > [tempPrintInfo setJobDisposition: NSPrintSaveJob];
    > [[tempPrintInfo dictionary] setObject: aPath
    > forKey: NSPrintSavePath];
    > [[tempPrintInfo dictionary] setObject: [NSNumber
    > numberWithBool: YES]
    > forKey: NSPrintAllPages];
    > op = [NSPrintOperation printOperationWithView: [scrollView
    > documentView]
    > printInfo: tempPrintInfo];
    > [op setShowPanels: NO];
    > [self markPageViewsDirtyForPrinting];
    > success = [op runOperation];
    >
    > [self setDocumentEdited: NO];
    >
    > return success;
    > }
    >
    > Annard

    Many thanks for this! I did spend hours looking in the wrong direction.

    Two follow-up questions:

    1. is there a way to print to an NSData? (Yes, one can print to some
    temp file, then read this into an NSData - but this looks rather
    inefficient).

    2. The documentation for setJobDisposition: says: "NSPrintFaxJob is
    deprecated". So: what to use instead? And - if I decide to use a
    deprecated function - how to set the fax number?

    Kind regards

    Gerriet.
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today