我有一个可可应用程序,我在其中创建了一个我想发送给打印机的customView。在子类NSView中,我也设置了一些框架选项,代码如下。我有2个全局变量来保存在main()函数外部声明的打印信息。可可(Mac OS X)打印多页,为什么预览窗口显示2页而不是1?
- (id)initWithFrame:(NSRect)frame
{
extern NSPrintInfo *globalPrintInfo;
extern NSPrintOperation *globalPrintOperation;
//Modify the frame before it's sent to it's super method. Also set the global variables to there default values.
globalPrintOperation = [NSPrintOperation printOperationWithView:self];
globalPrintInfo = [globalPrintOperation printInfo];//Get the print information from it.
[globalPrintInfo setBottomMargin:0.0];
[globalPrintInfo setLeftMargin:0.0];
[globalPrintInfo setTopMargin:0.0];
[globalPrintInfo setRightMargin:0.0];
[globalPrintOperation setPrintInfo:globalPrintInfo];//save the printInfo changes.
//modify the frame to reflect the correct height & width of the paper.
frame.size.height = globalPrintInfo.paperSize.height-globalPrintInfo.topMargin-globalPrintInfo.bottomMargin;
frame.size.width = globalPrintInfo.paperSize.width-globalPrintInfo.leftMargin-globalPrintInfo.rightMargin;
frame.origin.x=0;
frame.origin.y=0;
NSLog(@"Printer Name=%@, Printer Type=%@",globalPrintInfo.printer.name,globalPrintInfo.printer.type);
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
对于子类的NSView,这样我可以看到它的边界,我添加下面的下面的代码它的drawRect方法。
- (void)drawRect:(NSRect)dirtyRect
{
if ([NSGraphicsContext currentContextDrawingToScreen]) {
NSLog(@"Drawing To Screen");
} else {
NSLog(@"Drawing To Printer");
}
// Draw common elements here
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
//Set color of drawing to green, and fill the rectangle green, so we can see it's boundaries.
[[NSColor greenColor] setFill];
NSRectFill(dirtyRect);
CGContextSelectFont(myContext, "Helvetica-Bold", 18, kCGEncodingMacRoman);
CGContextSetCharacterSpacing(myContext, 10);
CGContextSetTextDrawingMode(myContext, kCGTextFillStroke);
CGContextSetRGBFillColor(myContext, 0, 0, 0, 1);//black
CGContextSetRGBStrokeColor (myContext, 0, 0, 1, 1);//blue stroke
CGContextShowTextAtPoint(myContext, 40, 0, "Here is some text!", 18);
}
当我去使用全局变量来运行打印操作,像这样......
- (IBAction)print:(id)sender {
NSLog(@"Testing Print");
extern NSPrintOperation *globalPrintOperation;
[globalPrintOperation runOperation];
}
我得到的打印窗口出现,我看到我的“绿色背景”对我的看法,但由于某种原因,它被分成2页。我不确定究竟发生了什么,因为我将框架的宽度和高度设置为高度,因此我们不胜感激任何帮助。下面是我看到的一些图像。
我的猜测是pagesize的宽度和高度与用于定义视图框架的像素单位类型的单位不同。
我的最终目标是制作一个程序,让用户选择他们想要的内容,并根据他们选择的选项打印特定的页面,但首先我要弄清楚如何甚至可以获得我期望的'内容' 1'页面而不是'2'。我可以通过实验手动确定宽度和高度,但对于我所假设的不同纸张尺寸,这不会很动态。
在此先感谢。
编辑***
我刚编辑我的代码下面的下面的子类的NSView
//METHOD OVERIDES
- (id)initWithFrame:(NSRect)frame
{
extern NSPrintInfo *globalPrintInfo;
extern NSPrintOperation *globalPrintOperation;
//Modify the frame before it's sent to it's super method. Also set the global variables to there default values.
globalPrintOperation = [NSPrintOperation printOperationWithView:self];//use whatever is currently there as the default print operation.
globalPrintInfo = [globalPrintOperation printInfo];//Get the print information from it.
[globalPrintInfo setBottomMargin:0.0];
[globalPrintInfo setLeftMargin:0.0];
[globalPrintInfo setTopMargin:0.0];
[globalPrintInfo setRightMargin:0.0];
[globalPrintOperation setPrintInfo:globalPrintInfo];//save the printInfo changes.
//modify the frame to reflect the correct height & width of the paper.
frame.size.height = (globalPrintInfo.paperSize.height-globalPrintInfo.topMargin-globalPrintInfo.bottomMargin);
frame.size.width = globalPrintInfo.paperSize.width-globalPrintInfo.leftMargin-globalPrintInfo.rightMargin;
frame.origin.x=0;
frame.origin.y=0;
NSLog(@"Printer Name=%@, Printer Type=%@",globalPrintInfo.printer.name,globalPrintInfo.printer.type);
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
if ([NSGraphicsContext currentContextDrawingToScreen]) {
NSLog(@"Drawing To Screen");
} else {
NSLog(@"Drawing To Printer");
}
// Draw common elements here
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
//Set color of drawing to green, and fill the rectangle green, so we can see it's boundaries.
[[NSColor greenColor] setFill];
NSRectFill(dirtyRect);
CGContextSelectFont(myContext, "Helvetica-Bold", 18, kCGEncodingMacRoman);
CGContextSetCharacterSpacing(myContext, 10);
CGContextSetTextDrawingMode(myContext, kCGTextFillStroke);
CGContextSetRGBFillColor(myContext, 0, 0, 0, 1);//black
CGContextSetRGBStrokeColor (myContext, 0, 0, 1, 1);//blue stroke
CGContextShowTextAtPoint(myContext, 40, 0, "Here is some text!", 18);
}
- (BOOL)knowsPageRange:(NSRangePointer)range {
NSRect bounds = [self bounds];
float printHeight = [self calculatePrintHeight];
range->location = 1;
range->length = NSHeight(bounds)/printHeight + 1;
NSLog(@"Calculated Page Range");
return YES;
}
// Return the drawing rectangle for a particular page number
- (NSRect)rectForPage:(int)page {
NSRect bounds = [self bounds];
float pageHeight = [self calculatePrintHeight];
NSLog(@"Created Rect For View");
return NSMakeRect(NSMinX(bounds), NSMaxY(bounds) - page * pageHeight,
NSWidth(bounds), pageHeight);
}
//CUSTOM METHODS
// Calculate the vertical size of the view that fits on a single page
- (float)calculatePrintHeight {
extern NSPrintInfo *globalPrintInfo;
extern NSPrintOperation *globalPrintOperation;
// Obtain the print info object for the current operation
// Calculate the page height in points
NSSize paperSize = [globalPrintInfo paperSize];
float pageHeight = paperSize.height - [globalPrintInfo topMargin] - [globalPrintInfo bottomMargin];
// Convert height to the scaled view
float scale = [[[globalPrintInfo dictionary] objectForKey:NSPrintScalingFactor]
floatValue];
NSLog(@"Calculated Print Height:%f",(pageHeight/scale));
return (pageHeight/scale);
}
@end
我能得到我想要的东西现在,当我去打印预览时,接受还是认为由于某种原因还有第二页?不知道为什么现在。我会上传我所看到的...
请注意它是如何表示1的2?第二页虽然只是空白。