1

I want to print the content of myWebView with header and footer (page numbers) for my Mac OSX app. So far, I am able to print the content of myWebView using the following code (from this link Printing multiple pages in Objective-C).

- (IBAction)sendToPrinter:(id)sender {
    NSPrintInfo *printInfo;
    NSPrintInfo *sharedInfo;
    NSPrintOperation *printOp;
    NSMutableDictionary *printInfoDict;
    NSMutableDictionary *sharedInfoDict;
    NSDictionary *printSettings;
    sharedInfo = [NSPrintInfo sharedPrintInfo];
    sharedInfoDict = [sharedInfo dictionary];
    printInfoDict = [NSMutableDictionary dictionaryWithDictionary:sharedInfoDict];
    [printInfoDict setObject:NSPrintSpoolJob forKey:NSPrintJobDisposition];
    printInfo = [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
    [printInfo setHorizontalPagination: NSFitPagination];
    [printInfo setVerticalPagination: NSAutoPagination];
    [printInfo setVerticallyCentered:NO];
    [printInfo setLeftMargin:36];
    [printInfo setRightMargin:36];
    [printInfo setTopMargin:72];
    [printInfo setBottomMargin:72];
    [printInfo setScalingFactor:1.0];
    [[printInfo dictionary] setValue:[NSNumber numberWithBool:YES] forKey:NSPrintHeaderAndFooter];
    [[printInfo dictionary] addEntriesFromDictionary:printSettings];
PrintTextView *printView = [[PrintTextView alloc] initWithFrame:[printInfo imageablePageBounds]];
    printView.printJobTitle = @"Title of page to be printed inside the header"; 
    NSView *webViewContent = [[[myWebView mainFrame] frameView] documentView];
    printOp = [NSPrintOperation printOperationWithView:webViewContent printInfo: printInfo];
    [printOp runOperation];
}

I have added the PrintTextView class to my project. I have been reading the OSX library documents and NSPrintOperation methods but so far, being a newbie, I am unable to figure it out how to add header and footer. I appreciate your help, any sample code, or guidance.

Community
  • 1
  • 1
DORI
  • 54
  • 1
  • 8
  • Is anyone able to help me with the above question? – DORI Nov 26 '12 at 20:18
  • I thought it was difficult for my newbie skills to figure this out, now I can see it has been tough for everyone in this forum to answer my question, not even one answer or comment after 9 days! – DORI Dec 02 '12 at 21:30
  • 2
    Can you use `printOp = [[[myWebView mainFrame] frameView] printOperationWithPrintInfo: printInfo]`? There are 4 methods in `WebUIDelegate` that handle computing header and footer heights and actually drawing the header and footer. – CTMacUser Aug 18 '14 at 06:25

1 Answers1

0

Use WebUIDelegate methods as suggested by CTMacUser

#pragma mark - WebUIDelegate Methods

- (float)webViewHeaderHeight:(WebView *)sender
{
    return 20.0;
}

- (void)webView:(WebView *)sender drawHeaderInRect:(NSRect)rect
{
    NSString *headerString = @"Title of page to be printed inside the header";
    [headerString drawInRect:rect];    
}
j0ris
  • 171
  • 2
  • 8