KMPDFEditInsertBlankPageWindow.swift 16 KB

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