问题描述
我想编写一个litte实用程序,可以从一个URL打印一个页面,该URL将提供一个标准文件(如pdf或JPG图片),我只是打印这个从我的可可应用程序,而不显示任何对话框,是可能吗?我在文档中找不到任何东西,除了一个事情告诉我建立一个视图的文件,然后打印这个视图,但这是真的有必要吗?
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 my cocoa app without showing any dialog, is that possible? I can't find anything about this in the docs except for a thing telling me to build a view with the file and then print this view but is this really necessary?
任何帮助赞赏。
感谢,
Philip
Thanks,Philip
推荐答案
您不需要显示 NSView
即可打印。
只需创建 NSView
以编程方式将其传递给 NSPrintOperation
。
You don't need to show the NSView
in order to print.
Just create the NSView
programmatically and pass it to the NSPrintOperation
.
示例代码:
// 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];
对于PDF文档,使用 PDFView
Quarts框架)。
For PDF documents use PDFView
(add the Quarts framework).
您还可以考虑使用 WebView
(添加WebKit框架)。
WebView支持许多格式,并使格式化一个微风(HTML)。
You might also consider using WebView
(add the WebKit framework).
WebView supports many formats and makes formatting a breeze (HTML).
这篇关于Mac OS X:直接从文件/ URL打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!