Can Somebody Refer Me to Simple Source-Code for Printing?

Discussion in 'Mac Programming' started by kaydell.leavitt, Apr 29, 2010.

  1. kaydell.leavitt macrumors regular

    Joined:
    Apr 19, 2010
    #1
    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.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    If you have an NSView that you want to print, use
    Code:
    [view print:nil];
     
  3. kaydell.leavitt thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #3
    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:
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    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];
     
  5. kaydell.leavitt thread starter macrumors regular

    Joined:
    Apr 19, 2010
  6. kaydell.leavitt thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #6
    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:
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    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.
     
  8. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    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.

    Actually it does :)
     

Share This Page