Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup printing in cocoa, swift?

Tags:

I have made printing functionality for custom NSView of NSPopover by the assigning the following action to button for this NSView in mainController:

@IBOutlet var plasmidMapIBOutlet: PlasmidMapView!

@IBAction func actionPrintfMap(sender: AnyObject)
{
    plasmidMapIBOutlet.print(sender)
}

It is working, but the print window has no option for Paper Size and Orientation, see screenshot below. enter image description here

  1. What should I do to get these options in the print window?
  2. And, how to make the NSView fitting to the printable area? Now it is not fitting.

I have figured out some moments, but not completely. So, I can setup the printing by the following code

 @IBAction func actionPrintMap(sender: AnyObject)
 {
    let printInfo = NSPrintInfo.sharedPrintInfo()
    let operation: NSPrintOperation = NSPrintOperation(view: plasmidMapIBOutlet, printInfo: printInfo)
    operation.printPanel.options = NSPrintPanelOptions.ShowsPaperSize
    operation.printPanel.options = NSPrintPanelOptions.ShowsOrientation
    operation.runOperation()

    //plasmidMapIBOutlet.print(sender)
 }

But, I still have problem. From the code above I can get only orientation (the last, ShowsOrientation), but not both PaperSize and Orientation. How can I manage both ShowsPaperSize and ShowsOrientation?

like image 293
VYT Avatar asked May 24 '16 14:05

VYT


1 Answers

Finally I have found the answer which is simple to write but it is not really obvious from apple documentation.

    operation.printPanel.options.insert(NSPrintPanelOptions.showsPaperSize)
    operation.printPanel.options.insert(NSPrintPanelOptions.showsOrientation)
like image 130
VYT Avatar answered Sep 28 '22 03:09

VYT