// // KMPDFEditInsertBlankPageWindow.swift // PDF Reader Pro // // Created by tangchao on 2023/11/13. // import Cocoa class KMPDFEditInsertBlankPageWindow: NSWindowController { var currentPage: Int = 1 var insertLocation: Int = 0 @IBOutlet var progress: NSProgressIndicator! @IBOutlet var pageCountLabel: NSTextField! @IBOutlet var pageSizeLabel: NSTextField! @IBOutlet var currentSizeButton: NSButton! @IBOutlet var standardButton: NSButton! @IBOutlet var customButton: NSButton! @IBOutlet var standardComboBox: NSComboBox! @IBOutlet var pageWidthTextField: NSTextField! @IBOutlet var pageHeightTextField: NSTextField! @IBOutlet var companyComboBox: NSComboBox! @IBOutlet var orientationLabel: NSTextField! @IBOutlet var portraitButton: NSButton! @IBOutlet var landscapeButton: NSButton! @IBOutlet var positionLabel: NSTextField! @IBOutlet var firstButton: NSButton! @IBOutlet var lastButton: NSButton! @IBOutlet var pageButton: NSButton! @IBOutlet var pageTextField: NSTextField! @IBOutlet var locationRangePopUpButton: NSPopUpButton! @IBOutlet var cancelButton: NSButton! @IBOutlet var insertButton: NSButton! @IBOutlet var byPageStepper: NSStepper! var pdfDocument: CPDFDocument? var insertPages: [CPDFPage] = [] private var _password: String? private var _lastString: String = "" private var _insertPageIndex: Int = 0 var callback: ((CPDFDocument?, String?, [CPDFPage]?, Int) -> Void)? var pageSize: NSSize = .zero var pageRotation: CGFloat = 0 deinit { KMPrint("KMPDFEditInsertBlankPageWindow deinit.") } convenience init(document: CPDFDocument) { self.init(windowNibName: "KMPDFEditInsertBlankPageWindow") // self.type = KMPDFEditPageEdit; self.pdfDocument = document; } convenience init(fileURL documentPath: URL) { self.init(windowNibName: "KMPDFEditInsertBlankPageWindow") // self.type = KMPDFEditManagement; self.currentPage = 1 self.pdfDocument = CPDFDocument(url: documentPath) } override func windowDidLoad() { super.windowDidLoad() self.insertPages = [] self.progress.isHidden = true self.pageTextField.isEditable = false let paperArray = KMPageSizeTool.paperSize() self.standardComboBox.addItems(withObjectValues: paperArray) self.standardComboBox.selectItem(at: 1) self.companyComboBox.selectItem(at: 0) self.standardComboBox.isEditable = false self.companyComboBox.isEditable = false self.firstButton.title = KMLocalizedString("First", nil) self.lastButton.title = KMLocalizedString("Last", nil) self.pageButton.title = KMLocalizedString("Page", nil) self.portraitButton.title = KMLocalizedString("Portrait Pages", nil) self.landscapeButton.title = KMLocalizedString("Landscape Pages", nil) self.standardButton.title = KMLocalizedString("Standard", nil) self.customButton.title = KMLocalizedString("Custom", nil) self.orientationLabel.stringValue = KMLocalizedString("Orientation", nil) self.cancelButton.title = KMLocalizedString("Cancel", nil) self.insertButton.title = KMLocalizedString("Insert", nil) self.positionLabel.stringValue = KMLocalizedString("Position", nil) self.pageSizeLabel.stringValue = KMLocalizedString("Page Size", nil) self.currentSizeButton.state = .on self.portraitButton.state = .on self.standardComboBox.isEnabled = false self.pageWidthTextField.isEnabled = false self.pageHeightTextField.isEnabled = false self.companyComboBox.isEnabled = false self.pageTextField.formatter = TextFieldFormatter() self.pageTextField.delegate = self let menu = NSMenu() _ = menu.addItem(title: KMLocalizedString("After", nil), action: #selector(after_Action), target: self) _ = menu.addItem(title: KMLocalizedString("Before", nil), action: #selector(before_Action), target: self) self.locationRangePopUpButton.menu = menu self.locationRangePopUpButton.isEnabled = false if let data = self.pdfDocument?.isLocked, data { DispatchQueue.main.asyncAfter(deadline: .now()+0.5) { } } else { self.byPageStepper.minValue = 1.0 self.byPageStepper.maxValue = Double(self.pdfDocument?.pageCount ?? 0) self.pageCountLabel.stringValue = String(format: "/%ld", self.pdfDocument?.pageCount ?? 0) } if (self.insertLocation == 2) { self.lastButton.state = .on } else if (self.insertLocation == 3) { self.pageButton.state = .on self.locationRangePopUpButton.isEnabled = true self.pageTextField.isEditable = true self.byPageStepper.isEnabled = true } else { self.firstButton.state = .on } self.pageTextField.stringValue = String(format: "%ld", self.currentPage) self.byPageStepper.integerValue = Int(self.pageTextField.stringValue) ?? 0 let tSize = KMPageSizeTool.getPaperSize(paperName:self.standardComboBox.stringValue) self.pageWidthTextField.stringValue = KMPageSizeTool.conversion(withUnit: self.companyComboBox.stringValue, value: tSize.width) self.pageHeightTextField.stringValue = KMPageSizeTool.conversion(withUnit: self.companyComboBox.stringValue, value: tSize.height) self.standardComboBox.stringValue = String(format: "%@(%@ x %@mm)", KMLocalizedString("A4", nil),"210","297") self._lastString = "mm" let page = self.pdfDocument?.page(at: UInt(self.currentPage-1)) as? CPDFPage let rect = page?.bounds(for: .mediaBox) ?? .zero let width = KMPageSizeTool.conversion(withUnit: "mm", value: (CGRectGetWidth(rect)/595 * 210)) let height = KMPageSizeTool.conversion(withUnit: "mm", value: (CGRectGetHeight(rect)/842 * 297)) self.currentSizeButton.title = String(format: "%@(%@ x %@mm)", KMLocalizedString("Current Size", nil),width,height) } @IBAction func byPageStepperAction(_ sender: AnyObject?) { self.pageTextField.stringValue = String(format: "%ld", self.byPageStepper.integerValue) } @IBAction func buttonItemClicked_Location(_ sender: AnyObject?) { if (self.pageButton.isEqual(to: sender)) { self.pageTextField.isEditable = true self.locationRangePopUpButton.isEnabled = true self.byPageStepper.isEnabled = true } else{ self.pageTextField.isEditable = false self.locationRangePopUpButton.isEnabled = false self.byPageStepper.isEnabled = false } } @IBAction func buttonItenClicked_Size(_ sender: NSButton?) { self.currentSizeButton.state = .off self.standardButton.state = .off self.customButton.state = .off sender?.state = .on if(sender == self.currentSizeButton) { self.standardComboBox.isEnabled = false self.pageWidthTextField.isEnabled = false self.pageHeightTextField.isEnabled = false self.companyComboBox.isEnabled = false } else if (sender == self.standardButton) { self.standardComboBox.isEnabled = true self.pageWidthTextField.isEnabled = false self.pageHeightTextField.isEnabled = false self.companyComboBox.isEnabled = false } else if (sender == self.customButton) { self.standardComboBox.isEnabled = false self.pageWidthTextField.isEnabled = true self.pageHeightTextField.isEnabled = true self.companyComboBox.isEnabled = true } } @IBAction func buttonItemClicked_Orientation(_ sender: NSButton?) { self.portraitButton.state = .off self.landscapeButton.state = .off sender?.state = .on var width = self.pageWidthTextField.stringValue var height = self.pageHeightTextField.stringValue if(self.pageWidthTextField.stringValue.stringToCGFloat() > self.pageHeightTextField.floatValue.cgFloat) { width = self.pageHeightTextField.stringValue height = self.pageWidthTextField.stringValue } if(self.portraitButton.state == .on) { self.pageWidthTextField.stringValue = width self.pageHeightTextField.stringValue = height } else { self.pageWidthTextField.stringValue = height self.pageHeightTextField.stringValue = width } } @IBAction func buttonItemClicked_Cancel(_ sender: AnyObject?) { self.pdfDocument = nil guard let block = self.callback else { return } block(self.pdfDocument, nil, nil, 0) } @IBAction func comboBoxItemClick_SplitSize(_ sender: AnyObject?) { if (self.standardComboBox.indexOfSelectedItem > self.standardComboBox.objectValues.count) { return } self.pageWidthTextField.isEnabled = false self.pageHeightTextField.isEnabled = false let size = self.standardComboBox.objectValues[self.standardComboBox.indexOfSelectedItem] as? String ?? "" let tSize = KMPageSizeTool.getPaperSize(paperName:size) let width = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.width) let height = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.height) self.standardComboBox.stringValue = String(format: "%@(%@ x %@mm)", size,width,height) } @IBAction func comboBoxItemClick_customSize(_ sender: AnyObject?) { let height = self.pageHeightTextField.stringValue.stringToCGFloat() let width = self.pageWidthTextField.stringValue.stringToCGFloat() self.pageWidthTextField.stringValue = KMPageSizeTool.conversion(with: self.companyComboBox.stringValue, from: self._lastString, value: width) self.pageHeightTextField.stringValue = KMPageSizeTool.conversion(with: self.companyComboBox.stringValue, from: self._lastString, value: height) self._lastString = self.companyComboBox.stringValue } @IBAction func buttonItemClicked_Insert(_ sender: AnyObject?) { self.window?.makeFirstResponder(nil) var pages: [CPDFPage] = [] var height: CGFloat = 0 var width: CGFloat = 0 if(self.currentSizeButton.state == .on) { let page = self.pdfDocument?.page(at: UInt(self.currentPage-1)) as? CPDFPage let rect = page?.bounds(for: .mediaBox) ?? .zero let w = KMPageSizeTool.conversion(withUnit: "mm", value: (CGRectGetWidth(rect)/595 * 210)) let h = KMPageSizeTool.conversion(withUnit: "mm", value: (CGRectGetHeight(rect)/842 * 297)) width = w.stringToCGFloat() height = h.stringToCGFloat() } else if self.standardButton.state == .on { if (self.standardComboBox.indexOfSelectedItem > self.standardComboBox.objectValues.count) { return } let size = self.standardComboBox.objectValues[self.standardComboBox.indexOfSelectedItem] as? String ?? "" let tSize = KMPageSizeTool.getPaperSize(paperName: size) width = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.width).stringToCGFloat() height = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.height).stringToCGFloat() } else if (self.customButton.state == .on) { height = self.pageHeightTextField.floatValue.cgFloat width = self.pageWidthTextField.floatValue.cgFloat width = KMPageSizeTool.conversion(with: "mm", from: self.companyComboBox.stringValue, value: width).stringToCGFloat() height = KMPageSizeTool.conversion(with: "mm", from: self.companyComboBox.stringValue, value: height).stringToCGFloat() if(!(width > 0 && height > 0)) { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = KMLocalizedString("Please enter the correct size.", nil) alert.runModal() return } } var page = CPDFPage() page.setBounds(NSMakeRect(0, 0, width * 595 / 210, height * 842 / 297), for: .mediaBox) self.pageSize = NSMakeSize(width * 595 / 210, height * 842 / 297) if(self.landscapeButton.state == .on && self.customButton.state == .off) { page.rotation = 90 self.pageRotation = 90 } else { if self.customButton.state == .on && self.landscapeButton.state == .on { self.pageRotation = 90 } } pages.append(page) var index = 0 if (self.firstButton.state == .on) { index = 0 } else if (self.lastButton.state == .on) { index = Int(self.pdfDocument?.pageCount ?? 0) } else { index = Int(self.pageTextField.stringValue) ?? 0 if ((1 == self.locationRangePopUpButton.indexOfSelectedItem)) { index = index - 1 } } for page in pages { self.insertPages.append(page) } self._insertPageIndex = index guard let block = self.callback else { return } block(self.pdfDocument, self._password, self.insertPages, self._insertPageIndex) } @IBAction func after_Action(_ sender: AnyObject?) { } @IBAction func before_Action(_ sender: AnyObject?) { } } extension KMPDFEditInsertBlankPageWindow: NSTextFieldDelegate { func controlTextDidChange(_ obj: Notification) { let textField = obj.object as? NSTextField if (textField == self.pageTextField) { if let sValue = Int(textField!.stringValue), sValue <= 0 { self.pageTextField.stringValue = "1" self.byPageStepper.integerValue = 1 } else if let sValue = Double(textField!.stringValue), sValue > self.byPageStepper.maxValue { self.pageTextField.stringValue = String(format: "%.0f", self.byPageStepper.maxValue) self.byPageStepper.doubleValue = self.byPageStepper.maxValue } else { self.byPageStepper.integerValue = Int(self.pageTextField.stringValue) ?? 0 } } else if (textField == self.pageWidthTextField) { if let fValue = textField?.floatValue, fValue > self.pageHeightTextField.floatValue { self.portraitButton.state = .off self.landscapeButton.state = .on } else { self.portraitButton.state = .on self.landscapeButton.state = .off } } else if (textField == self.pageHeightTextField) { if let fValue = textField?.floatValue, fValue > self.pageWidthTextField.floatValue { self.portraitButton.state = .on self.landscapeButton.state = .off } else { self.portraitButton.state = .off self.landscapeButton.state = .on } } } }