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
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).