Cocoa osx PDFView NSPrintOperation PrintPanel not showing page preview

后端 未结 2 1900
不知归路
不知归路 2021-01-21 03:13

In my app for Mac I have a webview that shows some html content. I create a PDFDocument from that webview and then I want to print that document. So I create a PDFView from the

2条回答
  •  [愿得一人]
    2021-01-21 03:27

    From this link:

    PDFDocument *doc = ...;
    
    // Invoke private method.
    // NOTE: Use NSInvocation because one argument is a BOOL type. Alternately, you could declare the method in a category and just call it.
    BOOL autoRotate = NO; // Set accordingly.
    NSMethodSignature *signature = [PDFDocument instanceMethodSignatureForSelector:@selector(getPrintOperationForPrintInfo:autoRotate:)];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    [invocation setSelector:@selector(getPrintOperationForPrintInfo:autoRotate:)];
    [invocation setArgument:&printInfo atIndex:2];
    [invocation setArgument:&autoRotate atIndex:3];
    [invocation invokeWithTarget:doc];
    
    // Grab the returned print operation.
    void *result;
    [invocation getReturnValue:&result];
    
    NSPrintOperation *op = (__bridge NSPrintOperation *)result;
    [op setShowsPrintPanel:YES];
    [op setShowsProgressPanel:YES];
    [op runOperation];
    

    This works on OSX from 10.4 to 10.10 (Yosemite).

    EDIT: You can also see this answer which is similar, but with less lines of code.

提交回复
热议问题