Mac OS X: Print directly from file/URL?

后端 未结 1 1389
南旧
南旧 2021-02-11 00:09

I want to code a litte utility that can print a page from a an URL, the URL would deliver a standard file (like a pdf or a jpg picture) and I just want to print this from within

1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-11 01:09

    You don't need to show the NSView in order to print.
    Just create the NSView programmatically and pass it to the NSPrintOperation.

    Example code:

    // Get Print Info
    NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
    
    // Printing Text
    NSRect textRect = NSMakeRect(0,0,100,50);
    NSTextView *theTextView = [[NSTextView alloc] initWithFrame:textRect];
    [theTextView setString: @"Hello World"];
    NSPrintOperation *textPrint = [NSPrintOperation printOperationWithView:theTextView printInfo:printInfo];
    [textPrint setCanSpawnSeparateThread:YES];
    [textPrint runOperation];
    
    // Printing Picture
    NSImage *pic =  [[NSImage alloc] initWithContentsOfFile: @"/Users/Anne/Desktop/Sample.png"];
    NSRect picRect = NSRectFromCGRect(CGRectMake(0, 0, pic.size.width, pic.size.height));
    NSImageView *imageView = [[NSImageView alloc] initWithFrame:picRect];
    [imageView setImage:pic];
    NSPrintOperation * picPrint = [NSPrintOperation printOperationWithView:imageView printInfo:printInfo];
    [picPrint setCanSpawnSeparateThread:YES];
    [picPrint runOperation];
    

    For PDF documents use PDFView (add the Quartz framework).

    You might also consider using WebView (add the WebKit framework).
    WebView supports many formats and makes formatting a breeze (HTML).

    0 讨论(0)
提交回复
热议问题