KMPDFEditInsertBlankPageWindow.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //
  2. // KMPDFEditInsertBlankPageWindow.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/11/13.
  6. //
  7. import Cocoa
  8. class KMPDFEditInsertBlankPageWindow: NSWindowController {
  9. var currentPage: Int = 1
  10. var insertLocation: Int = 0
  11. @IBOutlet var progress: NSProgressIndicator!
  12. @IBOutlet var pageCountLabel: NSTextField!
  13. @IBOutlet var pageSizeLabel: NSTextField!
  14. @IBOutlet var currentSizeButton: NSButton!
  15. @IBOutlet var standardButton: NSButton!
  16. @IBOutlet var customButton: NSButton!
  17. @IBOutlet var standardComboBox: NSComboBox!
  18. @IBOutlet var pageWidthTextField: NSTextField!
  19. @IBOutlet var pageHeightTextField: NSTextField!
  20. @IBOutlet var companyComboBox: NSComboBox!
  21. @IBOutlet var orientationLabel: NSTextField!
  22. @IBOutlet var portraitButton: NSButton!
  23. @IBOutlet var landscapeButton: NSButton!
  24. @IBOutlet var positionLabel: NSTextField!
  25. @IBOutlet var firstButton: NSButton!
  26. @IBOutlet var lastButton: NSButton!
  27. @IBOutlet var pageButton: NSButton!
  28. @IBOutlet var pageTextField: NSTextField!
  29. @IBOutlet var locationRangePopUpButton: NSPopUpButton!
  30. @IBOutlet var cancelButton: NSButton!
  31. @IBOutlet var insertButton: NSButton!
  32. @IBOutlet var byPageStepper: NSStepper!
  33. var pdfDocument: CPDFDocument?
  34. var insertPages: [CPDFPage] = []
  35. private var _password: String?
  36. private var _lastString: String = ""
  37. private var _insertPageIndex: Int = 0
  38. var callback: ((CPDFDocument?, String?, [CPDFPage]?, Int) -> Void)?
  39. deinit {
  40. KMPrint("KMPDFEditInsertBlankPageWindow deinit.")
  41. }
  42. convenience init(document: CPDFDocument) {
  43. self.init(windowNibName: "KMPDFEditInsertBlankPageWindow")
  44. // self.type = KMPDFEditPageEdit;
  45. self.pdfDocument = document;
  46. }
  47. convenience init(fileURL documentPath: URL) {
  48. self.init(windowNibName: "KMPDFEditInsertBlankPageWindow")
  49. // self.type = KMPDFEditManagement;
  50. self.currentPage = 1;
  51. self.pdfDocument = CPDFDocument(url: documentPath)
  52. }
  53. override func windowDidLoad() {
  54. super.windowDidLoad()
  55. self.insertPages = []
  56. self.progress.isHidden = true
  57. self.pageTextField.isEditable = false
  58. let paperArray = KMPageSizeTool.paperSize()
  59. self.standardComboBox.addItems(withObjectValues: paperArray)
  60. self.standardComboBox.selectItem(at: 1)
  61. self.companyComboBox.selectItem(at: 0)
  62. self.standardComboBox.isEditable = false
  63. self.companyComboBox.isEditable = false
  64. self.firstButton.title = KMLocalizedString("First", nil)
  65. self.lastButton.title = KMLocalizedString("Last", nil)
  66. self.pageButton.title = KMLocalizedString("Page", nil)
  67. self.portraitButton.title = KMLocalizedString("Portrait Pages", nil)
  68. self.landscapeButton.title = KMLocalizedString("Landscape Pages", nil)
  69. self.standardButton.title = KMLocalizedString("Standard", nil)
  70. self.customButton.title = KMLocalizedString("Custom", nil)
  71. self.orientationLabel.stringValue = KMLocalizedString("Orientation", nil)
  72. self.cancelButton.title = KMLocalizedString("Cancel", nil)
  73. self.insertButton.title = KMLocalizedString("Insert", nil)
  74. self.positionLabel.stringValue = KMLocalizedString("Position", nil)
  75. self.pageSizeLabel.stringValue = KMLocalizedString("Page Size", nil)
  76. self.currentSizeButton.state = .on
  77. self.portraitButton.state = .on
  78. self.standardComboBox.isEnabled = false
  79. self.pageWidthTextField.isEnabled = false
  80. self.pageHeightTextField.isEnabled = false
  81. self.companyComboBox.isEnabled = false
  82. self.pageTextField.formatter = TextFieldFormatter()
  83. self.pageTextField.delegate = self
  84. let menu = NSMenu()
  85. _ = menu.addItem(title: KMLocalizedString("After", nil), action: #selector(after_Action), target: self)
  86. _ = menu.addItem(title: KMLocalizedString("Before", nil), action: #selector(before_Action), target: self)
  87. self.locationRangePopUpButton.menu = menu
  88. self.locationRangePopUpButton.isEnabled = false
  89. if let data = self.pdfDocument?.isLocked, data {
  90. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  91. // PasswordWindowController *passwordVC = [[PasswordWindowController alloc] initWithWindowNibName:@"PasswordWindowController"];
  92. // passwordVC.fileURL = [self.pdfDocument documentURL];
  93. // [passwordVC beginSheetModalForWindow:self.window completionHandler:^(NSString *password) {
  94. // if (password) {
  95. // [_pdfDocument unlockWithPassword:password];
  96. // self.password = password;
  97. // _pageCountLabel.stringValue = [NSString stringWithFormat:@"/%ld",_pdfDocument.pageCount];
  98. // _byPageStepper.minValue = 1.0;
  99. // _byPageStepper.maxValue = _pdfDocument.pageCount;
  100. // }
  101. // }
  102. }
  103. } else {
  104. self.byPageStepper.minValue = 1.0
  105. self.byPageStepper.maxValue = Double(self.pdfDocument?.pageCount ?? 0)
  106. self.pageCountLabel.stringValue = String(format: "/%ld", self.pdfDocument?.pageCount ?? 0)
  107. }
  108. if (self.insertLocation == 2) {
  109. self.lastButton.state = .on
  110. } else if (self.insertLocation == 3) {
  111. self.pageButton.state = .on
  112. self.locationRangePopUpButton.isEnabled = true
  113. self.pageTextField.isEditable = true
  114. self.byPageStepper.isEnabled = true
  115. } else {
  116. self.firstButton.state = .on
  117. }
  118. self.pageTextField.stringValue = String(format: "%ld", self.currentPage)
  119. self.byPageStepper.integerValue = Int(self.pageTextField.stringValue) ?? 0
  120. let tSize = KMPageSizeTool.getPaperSize(paperName:self.standardComboBox.stringValue)
  121. self.pageWidthTextField.stringValue = KMPageSizeTool.conversion(withUnit: self.companyComboBox.stringValue, value: tSize.width)
  122. self.pageHeightTextField.stringValue = KMPageSizeTool.conversion(withUnit: self.companyComboBox.stringValue, value: tSize.height)
  123. self.standardComboBox.stringValue = String(format: "%@(%@ x %@mm)", KMLocalizedString("A4", nil),"210","297")
  124. self._lastString = "mm"
  125. let page = self.pdfDocument?.page(at: UInt(self.currentPage-1)) as? CPDFPage
  126. let rect = page?.bounds(for: .mediaBox) ?? .zero
  127. let width = KMPageSizeTool.conversion(withUnit: "mm", value: (CGRectGetWidth(rect)/595 * 210))
  128. let height = KMPageSizeTool.conversion(withUnit: "mm", value: (CGRectGetHeight(rect)/842 * 297))
  129. self.currentSizeButton.title = String(format: "%@(%@ x %@mm)", KMLocalizedString("Current Size", nil),width,height)
  130. }
  131. @IBAction func byPageStepperAction(_ sender: AnyObject?) {
  132. self.pageTextField.stringValue = String(format: "%ld", self.byPageStepper.integerValue)
  133. }
  134. @IBAction func buttonItemClicked_Location(_ sender: AnyObject?) {
  135. if (self.pageButton.isEqual(to: sender)) {
  136. self.pageTextField.isEditable = true
  137. self.locationRangePopUpButton.isEnabled = true
  138. self.byPageStepper.isEnabled = true
  139. } else{
  140. self.pageTextField.isEditable = false
  141. self.locationRangePopUpButton.isEnabled = false
  142. self.byPageStepper.isEnabled = false
  143. }
  144. }
  145. @IBAction func buttonItenClicked_Size(_ sender: NSButton?) {
  146. self.currentSizeButton.state = .off
  147. self.standardButton.state = .off
  148. self.customButton.state = .off
  149. sender?.state = .on
  150. if(sender == self.currentSizeButton) {
  151. self.standardComboBox.isEnabled = false
  152. self.pageWidthTextField.isEnabled = false
  153. self.pageHeightTextField.isEnabled = false
  154. self.companyComboBox.isEnabled = false
  155. } else if (sender == self.standardButton) {
  156. self.standardComboBox.isEnabled = true
  157. self.pageWidthTextField.isEnabled = false
  158. self.pageHeightTextField.isEnabled = false
  159. self.companyComboBox.isEnabled = false
  160. } else if (sender == self.customButton) {
  161. self.standardComboBox.isEnabled = false
  162. self.pageWidthTextField.isEnabled = true
  163. self.pageHeightTextField.isEnabled = true
  164. self.companyComboBox.isEnabled = true
  165. }
  166. }
  167. @IBAction func buttonItemClicked_Orientation(_ sender: NSButton?) {
  168. self.portraitButton.state = .off
  169. self.landscapeButton.state = .off
  170. sender?.state = .on
  171. var width = self.pageWidthTextField.stringValue
  172. var height = self.pageHeightTextField.stringValue
  173. if(self.pageWidthTextField.stringValue.stringToCGFloat() > self.pageHeightTextField.floatValue.cgFloat) {
  174. width = self.pageHeightTextField.stringValue
  175. height = self.pageWidthTextField.stringValue
  176. }
  177. if(self.portraitButton.state == .on) {
  178. self.pageWidthTextField.stringValue = width
  179. self.pageHeightTextField.stringValue = height
  180. } else {
  181. self.pageWidthTextField.stringValue = height
  182. self.pageHeightTextField.stringValue = width
  183. }
  184. }
  185. @IBAction func buttonItemClicked_Cancel(_ sender: AnyObject?) {
  186. self.pdfDocument = nil
  187. guard let block = self.callback else {
  188. return
  189. }
  190. block(self.pdfDocument, nil, nil, 0)
  191. }
  192. @IBAction func comboBoxItemClick_SplitSize(_ sender: AnyObject?) {
  193. if (self.standardComboBox.indexOfSelectedItem > self.standardComboBox.objectValues.count) {
  194. return
  195. }
  196. self.pageWidthTextField.isEnabled = false
  197. self.pageHeightTextField.isEnabled = false
  198. let size = self.standardComboBox.objectValues[self.standardComboBox.indexOfSelectedItem] as! String
  199. let tSize = KMPageSizeTool.getPaperSize(paperName:size)
  200. let width = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.width)
  201. let height = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.height)
  202. self.standardComboBox.stringValue = String(format: "%@(%@ x %@mm)", size,width,height)
  203. }
  204. @IBAction func comboBoxItemClick_customSize(_ sender: AnyObject?) {
  205. let height = self.pageHeightTextField.stringValue.stringToCGFloat()
  206. let width = self.pageWidthTextField.stringValue.stringToCGFloat()
  207. self.pageWidthTextField.stringValue = KMPageSizeTool.conversion(with: self.companyComboBox.stringValue, from: self._lastString, value: width)
  208. self.pageHeightTextField.stringValue = KMPageSizeTool.conversion(with: self.companyComboBox.stringValue, from: self._lastString, value: height)
  209. self._lastString = self.companyComboBox.stringValue
  210. }
  211. @IBAction func buttonItemClicked_Insert(_ sender: AnyObject?) {
  212. self.window?.makeFirstResponder(nil)
  213. var pages: [CPDFPage] = []
  214. var height: CGFloat = 0
  215. var width: CGFloat = 0
  216. if(self.currentSizeButton.state == .on) {
  217. let page = self.pdfDocument?.page(at: UInt(self.currentPage-1)) as? CPDFPage
  218. let rect = page?.bounds(for: .mediaBox) ?? .zero
  219. let w = KMPageSizeTool.conversion(withUnit: "mm", value: (CGRectGetWidth(rect)/595 * 210))
  220. let h = KMPageSizeTool.conversion(withUnit: "mm", value: (CGRectGetHeight(rect)/842 * 297))
  221. width = w.stringToCGFloat()
  222. height = h.stringToCGFloat()
  223. } else if self.standardButton.state == .on {
  224. if (self.standardComboBox.indexOfSelectedItem > self.standardComboBox.objectValues.count) {
  225. return
  226. }
  227. let size = self.standardComboBox.objectValues[self.standardComboBox.indexOfSelectedItem]
  228. let tSize = KMPageSizeTool.getPaperSize(paperName: size as! String)
  229. width = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.width).stringToCGFloat()
  230. height = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.height).stringToCGFloat()
  231. } else if (self.customButton.state == .on) {
  232. height = self.pageHeightTextField.floatValue.cgFloat
  233. width = self.pageWidthTextField.floatValue.cgFloat
  234. width = KMPageSizeTool.conversion(with: "mm", from: self.companyComboBox.stringValue, value: width).stringToCGFloat()
  235. height = KMPageSizeTool.conversion(with: "mm", from: self.companyComboBox.stringValue, value: height).stringToCGFloat()
  236. if(!(width > 0 && height > 0)) {
  237. let alert = NSAlert()
  238. alert.alertStyle = .critical
  239. alert.messageText = KMLocalizedString("Please enter the correct size.", nil)
  240. alert.runModal()
  241. return
  242. }
  243. }
  244. var page = CPDFPage()
  245. page.setBounds(NSMakeRect(0, 0, width * 595 / 210, height * 842 / 297), for: .mediaBox)
  246. if(self.landscapeButton.state == .on && self.customButton.state == .off) {
  247. page.rotation = 90
  248. }
  249. pages.append(page)
  250. var index = 0
  251. if (self.firstButton.state == .on) {
  252. index = 0
  253. } else if (self.lastButton.state == .on) {
  254. index = Int(self.pdfDocument?.pageCount ?? 0)
  255. } else {
  256. index = Int(self.pageTextField.stringValue) ?? 0
  257. if ((1 == self.locationRangePopUpButton.indexOfSelectedItem)) {
  258. index = index - 1
  259. }
  260. }
  261. for page in pages {
  262. self.insertPages.append(page)
  263. }
  264. self._insertPageIndex = index
  265. guard let block = self.callback else {
  266. return
  267. }
  268. block(self.pdfDocument, self._password, self.insertPages, self._insertPageIndex)
  269. }
  270. @IBAction func after_Action(_ sender: AnyObject?) {
  271. }
  272. @IBAction func before_Action(_ sender: AnyObject?) {
  273. }
  274. }
  275. extension KMPDFEditInsertBlankPageWindow: NSTextFieldDelegate {
  276. func controlTextDidChange(_ obj: Notification) {
  277. let textField = obj.object as? NSTextField
  278. if (textField == self.pageTextField) {
  279. if let sValue = Int(textField!.stringValue), sValue <= 0 {
  280. self.pageTextField.stringValue = "1"
  281. self.byPageStepper.integerValue = 1
  282. } else if let sValue = Double(textField!.stringValue), sValue > self.byPageStepper.maxValue {
  283. self.pageTextField.stringValue = String(format: "%.0f", self.byPageStepper.maxValue)
  284. self.byPageStepper.doubleValue = self.byPageStepper.maxValue
  285. } else {
  286. self.byPageStepper.integerValue = Int(self.pageTextField.stringValue) ?? 0
  287. }
  288. } else if (textField == self.pageWidthTextField) {
  289. if let fValue = textField?.floatValue, fValue > self.pageHeightTextField.floatValue {
  290. self.portraitButton.state = .off
  291. self.landscapeButton.state = .on
  292. } else {
  293. self.portraitButton.state = .on
  294. self.landscapeButton.state = .off
  295. }
  296. } else if (textField == self.pageHeightTextField) {
  297. if let fValue = textField?.floatValue, fValue > self.pageWidthTextField.floatValue {
  298. self.portraitButton.state = .on
  299. self.landscapeButton.state = .off
  300. } else {
  301. self.portraitButton.state = .off
  302. self.landscapeButton.state = .on
  303. }
  304. }
  305. }
  306. }