1

我想为我的 Mac OSX 应用程序打印带有页眉和页脚(页码)的 myWebView 的内容。到目前为止,我可以使用以下代码打印 myWebView 的内容(来自此链接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];
}

我已将 PrintTextView 类添加到我的项目中。我一直在阅读 OSX 库文档和 NSPrintOperation 方法,但到目前为止,作为一个新手,我无法弄清楚如何添加页眉和页脚。感谢您的帮助、任何示例代码或指导。

4

1 回答 1

0

使用WebUIDelegateCTMacUser 建议的方法

#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];    
}
于 2015-04-27T12:35:48.067 回答