Gerrit Beuze
Gerrit Beuze

Reputation: 921

How to auto rotate page when printing NSDocument via NSView?

I have a NSDocument descendant that I would like to print to a single page - stretched to fit and auto rotated when the page is wider than high. The rendering view determines its bounds based on the content. rectForPage simply returns [self bounds]; knowsPageRange sets range.length = 1 and range.location = 1 and returns YES. Printing is done using the code below.

Everything works, execpt the auto -rotation. In the Print Panel I see the preview in the correct shape (wider than high, the correct size etc.), However when I then print, the page is fitted, but in portrait mode, unnecessary scaling down the image. If I do not set printInfo.paperSize, it auto rotates OK, but does not scale, instead it clips the image. Any idea?

Additional info: The "View PDF" from the print panel shows the page OK in Preview. Printing from Preview, then shows the image rotated in Preview's Print Panel to match the paper potrait orientation (that is: the landscape image itself appears rotated). This is different from my case where it shows unrotated in the print panel. Printing from Preview's Print Panel then does the correct thing.

- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)printSettings error:(NSError **)outError {
    MindMapRenderingView *renderingView = [[MindMapRenderingView alloc] initWithMindMap:_mindMap 
                                                                          printJobTitle:[_mindMap singleLineTitle]];
    NSSize pageSize = [renderingView bounds].size;
    NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:renderingView
                                                                      printInfo:[self printInfo]];
    [renderingView release];

    NSPrintInfo* printInfo = [printOperation printInfo];
    [[printInfo dictionary] addEntriesFromDictionary:printSettings];

    if (isLandscape)
        [printInfo setOrientation:NSLandscapeOrientation];
    else
        [printInfo setOrientation:NSPortraitOrientation];

    [printInfo setPaperSize:pageSize];

    [printInfo setHorizontalPagination:NSFitPagination];
    [printInfo setVerticalPagination:NSFitPagination];

    return printOperation;
}

Upvotes: 0

Views: 728

Answers (1)

Gerrit Beuze
Gerrit Beuze

Reputation: 921

Have not found a way to auto rotate, but after more experimenting I came up with the solution that mimics the way Preview prints the landscape pdf: I rotate the frame for the NSView that is used to draw the output. In drawRect: I also draw the output rotated using NSAffineTransform. That gives the required result. Only drawback is that using "View PDF" from the print panel now shows the pdf rotated too.

Upvotes: 0

Related Questions