• Printing multiple pages in Objective-C

    16 June 2015

    This prints a representation of a page preview called sheet, which is an NSBox

    - (void)sendToPrinter:(int)code {
        NSPrintInfo *printInfo;
        NSPrintInfo *sharedInfo;
        NSPrintOperation *printOp;
        NSMutableDictionary *printInfoDict;
        NSMutableDictionary *sharedDict;
    
         sharedInfo = [NSPrintInfo sharedPrintInfo];
         sharedDict = [sharedInfo dictionary];
         printInfoDict = [NSMutableDictionary dictionaryWithDictionary:
                         sharedDict];
        [printInfoDict setObject:NSPrintSpoolJob 
                          forKey:NSPrintJobDisposition];
         printInfo = [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
        [printInfo setHorizontalPagination: NSAutoPagination];
        [printInfo setVerticalPagination: NSAutoPagination];
        [printInfo setVerticallyCentered:NO];
        [printInfo setLeftMargin:10];
        [printInfo setRightMargin:10];
        [printInfo setTopMargin:10];
        [printInfo setBottomMargin:10];
        [printInfo setScalingFactor:1.1];
         printOp = [NSPrintOperation printOperationWithView:sheet 
                                                 printInfo:printInfo];
        [printOp setShowsPrintPanel:YES];
        [printOp runOperation];
    }
    
    - (BOOL)knowsPageRange:(NSRangePointer)aRange;
    { 
        *aRange = NSMakeRange(1, 10);
        return YES;
    }
    
    - (void)beginPageInRect:(NSRect)aRect atPlacement:(NSPoint)location;
    {
        [self setTitle:[NSString stringWithFormat:@"Page %d", [[NSPrintOperation currentOperation] currentPage]]];
        [super beginPageInRect:aRect atPlacement:location];
    }
    
    - (NSRect)rectForPage:(NSInteger)page;
    {
        return [self bounds];
    }
    
    @end
    

Comments

Comments closed on this post.