On 19/02/2010, at 6:06 AM, Michael de Haan wrote:

> Hi all,
> May I ask about an issue I am having adding page numbers to a print out.
> I implemented the following method:
> 
> 
> - (void)drawPageBorderWithSize:(NSSize)borderSize
> {
>   // NSPrintOperation *po = [NSPrintOperation currentOperation];
>    //NSPrintInfo *pi = [po printInfo];
> 
>    NSRect currentFrame = [self frame]; 
>    NSRect newFrame= NSMakeRect(0, 0, borderSize.width, borderSize.height);
>    [self setFrame:newFrame];
> 
>    NSFont *f = [attributes objectForKey:NSFontAttributeName];
>    float capHeigth = [ f capHeight];
>    NSString *string = [ NSString stringWithFormat:@"Page %d", currentPage + 
> 1];
>    float stringX = (borderSize.width - (float)[string length])/2.0;
>    [self lockFocus];
>    [string drawAtPoint:NSMakePoint(stringX, borderSize.height - (1.7 * 
> capHeigth))
>    withAttributes:attributes];
>    [self unlockFocus];
>    [self setFrame:currentFrame];
> }
> 
> 
> A while ago, there were questions to the list about printInfo "margin" calls. 
> At that time, it appears  the consensus was that these calls are buggy. I 
> think they still are, as my code showed that the margins returned bore no 
> relationship to the margins set in page Setup.  As a workaround, I used the 
> above approach. What do others do to deduce the user's margin settings....or 
> it's possible that I am missing something else.


Hi Michael,

I'm not sure that they're buggy, it's just they don't return what you think 
they do, or what their name appears to imply. I added the following to 
NSPrintInfo as a category so I can get the actual paper margins set in Page 
Setup, it might come in handy for you too.



enum
{
        PrintInfoTopMarginIndex         = 0,
        PrintInfoLeftMarginIndex        = 1,
        PrintInfoBottomMarginIndex      = 2,
        PrintInfoRightMarginIndex       = 3
};


@implementation NSPrintInfo (PaperMargins)


- (NSArray*)    paperMargins
{
        PMPageFormat    format = (PMPageFormat)[self PMPageFormat];
        PMPaper         paper;
        OSStatus        err = PMGetPageFormatPaper( format, &paper );
        
        if( err == noErr )
        {
                PMPaperMargins  margins;
                err = PMPaperGetMargins( paper, &margins );
                
                if( err == noErr )
                {
                        NSMutableArray* array = [NSMutableArray array];
                        [array addObject:[NSNumber 
numberWithDouble:margins.top]];
                        [array addObject:[NSNumber 
numberWithDouble:margins.left]];
                        [array addObject:[NSNumber 
numberWithDouble:margins.bottom]];
                        [array addObject:[NSNumber 
numberWithDouble:margins.right]];
                         
                        return array;
                }
        }
        
        return nil;
}



- (double)              topPaperMargin
{
        return [[[self paperMargins] objectAtIndex:PrintInfoTopMarginIndex] 
doubleValue];
}



- (double)              leftPaperMargin
{
        return [[[self paperMargins] objectAtIndex:PrintInfoLeftMarginIndex] 
doubleValue];
}



- (double)              bottomPaperMargin
{
        return [[[self paperMargins] objectAtIndex:PrintInfoBottomMarginIndex] 
doubleValue];
}



- (double)              rightPaperMargin
{
        return [[[self paperMargins] objectAtIndex:PrintInfoRightMarginIndex] 
doubleValue];
}


@end


--Graham


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to