cocoansdocumentpdfview

How to find out if print panel was closed successfully when printing from a PDFView


I need to know if the PDFDocument of a PDFView has been printed successfully in order to do some housekeeping afterwards. When printing a regular NSView from NSDocument, I can do

    NSPrintOperation *op = [NSPrintOperation
                            printOperationWithView:myRegularPrintView
                            printInfo:self.printInfo];
    [op setCanSpawnSeparateThread:NO]; // Because we want to clean up afterwards
    [op setShowsPrintPanel:YES];
    
    [self runModalPrintOperation:op
                        delegate:self
                  didRunSelector:@selector(documentDidRunModalPrintOperation:success:contextInfo:)
                     contextInfo:NULL];

In the documentDidRunModalPrintOperation callback I can do the housekeeping. But printing the content of a PDFView only works correctly if I call

[myPDFView printWithInfo:[NSPrintInfo sharedPrintInfo] autoRotate:YES];

So I see no way to run the print operation with a callback function that is going to be called when the print panel is closed.


Solution

  • Since macOS 10.7 there is a function in PDFDocument which returns a NSPrintOperation, so one can simply do

        NSPrintOperation *op = [myPDFView.document printOperationForPrintInfo:self.printInfo scalingMode:kPDFPrintPageScaleToFit autoRotate:YES];
    

    and then continue just like with a normal NSView and add a callback when calling runModalPrintOperation.