0

Using NSPrintInfo (shared or not) if I specify a paper size using printInfo?.paperSize that is not in the list of paper sizes defined in the NSPrintOperation panel paper size dropdown and set the margins to zero

printInfo?.leftMargin = 0
printInfo?.rightMargin = 0
printInfo?.topMargin = 0
printInfo?.bottomMargin = 0

the NSPrintOperation panel will respect the size and the margins and the print works perfectly with margins set to 0.0. But, if I use a paper size that is defined in the dropdown - for instance 8.5 x 11.0, even though I said use 0.0 for margins, it uses paper with "borders" of about 0.20". If I change that dropdown to say "US Letter Borderless 8.5 x 11.0" the margins are 0.0, printInfo.imageablePageBounds is correct, and the print works fine.

Is it even possible to make the NSPrintOperation panel default to borderless on paper sizes it has a definition for?

This is similar to this question where the OP did not find a optimal answer:

printer setup has irregular margins/page size

The app is a drag and drop WYSIWYG art editing program. I realize very few printers can successfully print to the edge of the paper. What I'm doing is controlling the margins in the code so the user does not have to go through that mess at print time. The user sets the margins at design time so they can visually see where any clipping will occur.

enter image description here

Source code for test:


//
//  AppDelegate.swift
//  PrinterTest
//
//  Created 4/7/23.
//

import Cocoa

@main
class AppDelegate: NSObject, NSApplicationDelegate {

  @IBOutlet var window: NSWindow!

  var printInfo: NSPrintInfo?
  var printOperation: NSPrintOperation?
  
  // NSView class used in the Print Panel's preview window - created with the dimensions of the user's paper request
  // just draws a red frame around it's bounds.
  
  class PrintPreview: NSView {
  
    var currentContext : CGContext {                            // convenience method for getting the Graphics Context
      let context = NSGraphicsContext.current
      return context!.cgContext
      }

    override func draw(_ dirtyRect: NSRect) {
      super.draw(dirtyRect)
      let context = currentContext
      context.saveGState()
      context.setLineWidth(4.0)
      context.setStrokeColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
      context.stroke(bounds)                                    // draw a red frame around the perimeter of the "project"
      context.restoreGState()
      }
      
   override init(frame frameRect: NSRect) {
    super.init(frame: frameRect)
    }
    
  required init?(coder: NSCoder) {
    fatalError("PrintPreview init(coder:) has not been implemented")
    }
    
  }

  // notification sent when something in printInfoChanged

  @objc private func printInfoDidChange(notification: NSNotification) {
    let printSettings = printOperation!.printInfo.dictionary()
    
    print("-------- printInfoDidChange -------- ")
    for (key, value) in printSettings {
      print("\(key): \(value)")
      }
    print("------------------------------------ ")
    }

  @IBAction func startPrint(_ sender: Any) {
  
    let paperSize = NSSize(width: 8.5 * 72.0, height: 11.0 * 72.0)              // define paper size
    //let paperSize = NSSize(width: 7.0 * 72.0, height: 8.0 * 72.0)
    
    // create a simple NSView that will draw a red frame around it's bounds
    
    let printPreview = PrintPreview(frame: CGRect(x: 0, y: 0, width: paperSize.width, height: paperSize.height))
    
    // build the NSPrintInfo structure specifying 0 margins
    
    printInfo = NSPrintInfo.shared
    printInfo?.orientation = NSPrintInfo.PaperOrientation.portrait
    printInfo?.isHorizontallyCentered = true
    printInfo?.isVerticallyCentered = true
    printInfo?.horizontalPagination = NSPrintInfo.PaginationMode.fit
    printInfo?.verticalPagination = NSPrintInfo.PaginationMode.fit
    printInfo?.leftMargin = 0
    printInfo?.rightMargin = 0
    printInfo?.topMargin = 0
    printInfo?.bottomMargin = 0
    printInfo?.paperSize = paperSize
    
    // build the printOperation object
    
    printOperation = NSPrintOperation(view: printPreview, printInfo: printInfo!)
    printOperation!.printPanel.options = [.showsCopies, .showsPaperSize, .showsPageSetupAccessory, .showsPreview]
    printOperation?.jobTitle = "Print Test"
    printOperation?.showsPrintPanel = true
    printOperation!.run()
    }

  // app finished launching, setup a notification for when the print info object changes
  
  func applicationDidFinishLaunching(_ aNotification: Notification) {
      NotificationCenter.default.addObserver( self,
                                            selector: #selector(self.printInfoDidChange),
                                            name: Notification.Name("NSPrintInfoDidChange"),
                                            object: nil)
     }

  func applicationWillTerminate(_ aNotification: Notification) {
    }

  func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
    return true
    }
    
  func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
    return true
    }
}

Chris Walken
  • 378
  • 1
  • 9

0 Answers0