I need to print the content of my view using my mac app. I get the standard panel for print option. But while previewing my page setup is not proper.
I am using the following code for action on print button
- (void)print:(id)sender {
[[NSPrintOperation printOperationWithView:staticText] runOperation];
float horizontalMargin, verticalMargin;
NSSize bounds = [printInfo imageablePageBounds].size;
NSSize size = [printInfo paperSize];
horizontalMargin = 开发者_开发百科0;
verticalMargin = 0;
[self setPrintInfo:[NSPrintInfo sharedPrintInfo]];
[printInfo setLeftMargin:horizontalMargin];
[printInfo setRightMargin:horizontalMargin];
[printInfo setTopMargin:verticalMargin];
[printInfo setBottomMargin:verticalMargin];
}
After much research I got everything working fine.I am using below code & would like to share may be it might help someone in future
[self setPrintInfo:[NSPrintInfo sharedPrintInfo]];
[printInfo setVerticalPagination:NSAutoPagination];
float horizontalMargin, verticalMargin;
horizontalMargin = 0;
verticalMargin = -100;
[printInfo setLeftMargin:horizontalMargin];
[printInfo setRightMargin:horizontalMargin];
[printInfo setHorizontallyCentered:YES];
[printInfo setTopMargin:-600];
[printInfo setBottomMargin:verticalMargin];
[[NSPrintOperation printOperationWithView:sampleText] runOperation];
I ran into this myself.
My solution was to do this in Swift:
let printInfo = NSPrintInfo.sharedPrintInfo()
let printOperation = NSPrintOperation(view: webView.mainFrame.frameView.documentView, printInfo: printInfo)
printOperation.printInfo.orientation = NSPaperOrientation(rawValue: 1)! // switch to landscape
printOperation.printInfo.leftMargin = 10
printOperation.printInfo.rightMargin = 10
printOperation.printInfo.topMargin = 10
printOperation.printInfo.bottomMargin = 10
printOperation.runOperation()
NSPaperOrientation takes an integer value (0 = portrait, 1 = landscape)
Though can probably tidy things up a bit.
C# Version of John Griffiths Answer just in case someone is on Xamarin.Mac
var printInfo = NSPrintInfo.SharedPrintInfo;
var printOperation = NSPrintOperation.FromView(webContainer.MainFrame.FrameView.DocumentView, printInfo);
printOperation.PrintInfo.Orientation = NSPrintingOrientation.Portrait;
printOperation.PrintInfo.LeftMargin = 20;
printOperation.PrintInfo.RightMargin = 20;
printOperation.PrintInfo.TopMargin = 50;
printOperation.PrintInfo.BottomMargin = 50;
printOperation.RunOperation();
精彩评论