Printing multiple images each on a separate page using single NSPrintOperation

Discussion in 'Mac Programming' started by rahulkesharwani, Oct 30, 2011.

  1. rahulkesharwani macrumors newbie

    Oct 30, 2011

    I have a application that intends to print raster image of each page of a document using NSPrintOperation. I am able to create a NSImage of a single page and print it using NSPrintOperation as follows
    -void printPage: (NSImage)nsImage
        	NSImageView *nsImageView = [[NSImageView alloc] init];
    	NSSize imageSize = [nsImage size];
    	[nsImageView setImage: (NSImage *)nsImage];
            [nsImageView setFrame:NSMakeRect(0, 0, imageSize.width, imageSize.height)];
            [nsImageView setImageScaling:NSScaleToFit];
            NSPrintOperation *mNSPrintOperation = [NSPrintOperation printOperationWithView: (NSView *)nsImageView];
            NSPrintInfo *currentNSPrintInfo = [NSPrintInfo sharedPrintInfo];
            [currentNSPrintInfo setHorizontalPagination:NSFitPagination];
    	[currentNSPrintInfo setVerticalPagination:NSFitPagination];
    	[mNSPrintOperation setPrintInfo:currentNSPrintInfo];
    	[mNSPrintOperation setShowsPrintPanel:NO];
    	[mNSPrintOperation setShowsProgressPanel:YES];
            [mNSPrintOperation runOperation];
    Now when I have multiple pages to print, I would like to print all of them using a single NSPrintOperation. So basically, I would like to insert NSImage/NSImageView of each page as a separate page into a single NSView and use this NSView to print finally using NSPrintOperation. The reason I want to print it using single NSPrintOperation is that I want to get the print progress bar that shows the current page being printed. Otherwise, I could have created a separate NSPrintOperation for each NSImageView and print using it.

    Any help would be greatly appreciated

    Thanks & Regards
  2. jiminaus, Oct 30, 2011
    Last edited: Oct 30, 2011

    jiminaus macrumors 65816


    Dec 16, 2010
    You wouldn't want to use multiple NSPrintOperation objects for other reasons too. For example, if I printed to PDF, I would get multiple PDFs each with a single image instead of a single PDF with multiple images.

    What you need to do is code a custom NSView subclass that overrides the printing methods. This subclass would know how to layout the images one per page.

    Start off by reading the Printing Programming Topics for Cocoa guide. In particular the chapter titled Providing a Custom Pagination Scheme.

    Basically the NSView subclass "lays out" the images on a single canvas, and then responds to rectForPage: by returning the rectangle of the canvas that represents a given page. Then the printing subsystem will call drawRect: with the rect you responded with, and your NSView subclass then needs to draw the image for the page.

    I guess you could implement this by adding NSImageView objects as subviews placed appropriately. But I don't know how well that would scale if you wanted to print lots of images.

Share This Page