我有一个我正在开发的可可应用程序,我在其中创建了一个我想发送到打印机的 customView。在子类 NSView 中,我也设置了一些框架选项,代码如下。我有 2 个全局变量来保存在 main() 函数之外声明的打印信息。
- (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.width 和高度,感谢任何帮助。我看到的一些图片如下。
我的猜测是页面大小的宽度和高度与用于定义视图框架的像素单位类型的单位不同。
我的最终目标是制作一个程序,让用户选择他们想要的内容并根据他们选择的选项打印特定页面,但首先我必须弄清楚如何将我期望的“内容”放到“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 of 2 的?第二页只是空白的白色。