VYT
VYT

Reputation: 1071

How to setup printing in cocoa, swift?

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?

Upvotes: 5

Views: 2459

Answers (2)

Trygve Isaacson
Trygve Isaacson

Reputation: 11

The problem in the code originally posted is that options is being assigned twice, so the first value assigned, ShowsPaperSize is overwritten by the value ShowsOrientation. That's why you only see the ShowsOrientation option in the dialog.

By using multiple insert operations, you are adding options rather than overwriting each time. You can also do it this way which I think reads better:

operation.printPanel.options.insert([.showsPaperSize, .showsOrientation])

And finally, it also works to "set" the options, and by supplying the existing options as the first array value, you achieve the affect of appending:

operation.printPanel.options = [
    operation.printPanel.options,
    .showsPaperSize,
    .showsOrientation
]

(The first array element operation.printPanel.options means that the old options are supplied in the list of new options.)

Upvotes: 1

VYT
VYT

Reputation: 1071

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)

Upvotes: 3

Related Questions