I'm printing an invoice with the following code.
The print job is created, but two pages print and things don't print where I expect them to print. I expect the title of the document to be "INVOICE" and to print at the top, centered horizontally, but it doesn't print there at all on the first page and it prints with the wrong centering on the second page.
What am I doing wrong?
The print job is created, but two pages print and things don't print where I expect them to print. I expect the title of the document to be "INVOICE" and to print at the top, centered horizontally, but it doesn't print there at all on the first page and it prints with the wrong centering on the second page.
What am I doing wrong?
Code:
//
// InvoiceView.m
// PrintTest
//
// Created by kaydell on 4/30/10.
// Copyright 2010 LevSolve.com. All rights reserved.
//
#import "InvoiceView.h"
#import "InvoiceDocument.h"
@interface TitleView : NSView {
NSString *title;
}
@end
@implementation InvoiceView
@synthesize document;
@synthesize logoView;
@synthesize paperSize;
@synthesize leftMargin;
@synthesize rightMargin;
@synthesize topMargin;
@synthesize bottomMargin;
- (id) initWithInvoiceDocument:(InvoiceDocument *)invDoc {
if (self = [super init]) {
// store a reference to the invoice doument
self.document = invDoc;
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
NSLog(@"%@",printInfo);
paperSize = printInfo.paperSize;
leftMargin = [printInfo leftMargin];
rightMargin = [printInfo rightMargin];
topMargin = [printInfo topMargin];
bottomMargin = [printInfo bottomMargin];
// make the frame non-empty or it won't print
NSRect rect = NSMakeRect(0.0, 0.0, paperSize.width-1, paperSize.height-1); // make frame rect non-empty
[self setFrame: rect];
// create a LogoView object to print a logo (if any)
self.logoView = [[LogoView alloc] init];
}
return self;
}
- (void)drawString:(NSString *)string centeredInRect:(NSRect)r withAttributes:(NSDictionary *)theAttribute
{
NSPoint stringOrigin;
NSSize stringSize;
stringSize = [string sizeWithAttributes:theAttribute];
stringOrigin.x = r.origin.x + (r.size.width - stringSize.width)/2;
stringOrigin.y = r.origin.y + (r.size.height - stringSize.height)/2;
[string drawAtPoint:stringOrigin withAttributes:theAttribute];
}
- (void)drawString:(NSString *)string rightJustifiedInRect:(NSRect)r withAttributes:(NSDictionary *)theAttribute
{
NSPoint stringOrigin;
NSSize stringSize;
stringSize = [string sizeWithAttributes:theAttribute];
stringOrigin.x = r.origin.x + (r.size.width - stringSize.width);
stringOrigin.y = r.origin.y;
[string drawAtPoint:stringOrigin withAttributes:theAttribute];
}
- (void) drawRect:(NSRect)dirtyRect { // <<<< This method is called twice, once for each page, but why are there two pages?
// write the fields of the invoice document out to the console (for debugging)
[document log];
// draw the title
NSRect r = NSMakeRect(leftMargin,topMargin,paperSize.width,12);
[self drawString:document.title centeredInRect:r withAttributes:nil];
// draw the logo
[logoView drawRect:dirtyRect];
}
- (void) print:(id)sender {
// create a print operation object
NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:self];
// do the print job
[printOperation runOperation];
}
- (void) dealloc {
[logoView release];
[super dealloc];
}
@end