Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kaydell.leavitt

macrumors regular
Original poster
Apr 19, 2010
100
0
Hi, I need to have a simple example of source-code that implements printing on Mac OS X.

Can somebody give me a link to such sample code?

Thank you.
 
Thanks, this displays a print job dialog and prints a blank page, but my drawRect method doesn't get called.

Here is my code:

Code:
//
//  PrintTestAppDelegate.m
//  PrintTest
//
//  Created by kaydell on 4/27/10.
//  Copyright 2010 LevSolve.com. All rights reserved.
//

#import "PrintTestAppDelegate.h"

@implementation PrintTestAppDelegate

@synthesize window;
@synthesize printTestView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	printTestView = [[PrintTestView alloc] init];
}

- (IBAction) print: (id) sender {
	NSLog(@"print:");
	[printTestView print:nil]; // <<<<<<< this gets called <<<<<<
}

@end

Code:
//
//  PrintTestView.m
//  PrintTest
//
//  Created by kaydell on 4/27/10.
//  Copyright 2010 LevSolve.com. All rights reserved.
//

#import "PrintTestView.h"


@implementation PrintTestView

- (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) drawRect: (NSRect) dirtyRect { // <<<<<<< this doesn't get called
	NSRect bounds = [self bounds];
	[self drawString:@"Print test" centeredInRect:bounds withAttributes:nil];	
}

@end

-- Kaydell :confused:
 
Code:
printTestView = [[PrintTestView alloc] init];

You never give your view a frame, so it's size is 0x0, which results in nothing printed. Try giving it a size, like so:
Code:
NSRect frame = NSMakeRect(0.0, 0.0, 100.0, 100.0);
printTestView = [[PrintTestView alloc] initWithFrame:frame];
 
This Works, but...

The following code works, I mean that it prints the image, but I need to get the image from the current directory and I don't know how. (I tried just using the file name for the path name, but that didn't work for some reason.)

Code:
#import "PrintTestView.h"


@implementation PrintTestView

- (void) print:(id)sender {
	NSString *path = [@"~/Logo.png" stringByStandardizingPath];
	NSString *string = [NSString stringWithFormat:@"%@%@", @"file://localhost", path];
	NSURL *url = [[NSURL alloc] initWithString:string];
	NSData *data = [NSData dataWithContentsOfURL:url];
	NSImage *image = [[NSImage alloc] initWithData:data];
	[self setImage:image];
	[super print:sender];
}

@end
:):confused:
 
Code:
	NSString *path = [@"~/Logo.png" stringByStandardizingPath];

That won't work, because stringByStandardizingPath doesn't expand tildes. You need to use stringByExpandingTildeInPath.

Of course, ~ doesn't represent the working directory, so maybe that's not what you want. There are a number of path-related NSString methods. I recommend you look at them all, so you are familiar with what is there.
 
The following code works, I mean that it prints the image, but I need to get the image from the current directory and I don't know how. (I tried just using the file name for the path name, but that didn't work for some reason.)

:):confused:

The way you're building a URL is wrong. You should use the fileURLWithPath: method instead.

Second, no need to load the data directly. NSImage has several methods for loading directly from a file or URL. Check out the documentation.

Third, like chown33 mentioned, you're not properly loading a file via the current working directory. Look at NSFileManager for working with it.

That won't work, because stringByStandardizingPath doesn't expand tildes. You need to use stringByExpandingTildeInPath.

Actually it does :)
This method can make the following changes in the provided string:

Expand an initial tilde expression using stringByExpandingTildeInPath.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.