KMPDFEditInsertBlankPageWindow.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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: (CGRectGetWidth(rect)/595 * 210))
  119. let height = KMPageSizeTool.conversion(withUnit: "mm", value: (CGRectGetHeight(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: (CGRectGetWidth(rect)/595 * 210))
  211. let h = KMPageSizeTool.conversion(withUnit: "mm", value: (CGRectGetHeight(rect)/842 * 297))
  212. width = w.stringToCGFloat()
  213. height = h.stringToCGFloat()
  214. } else if self.standardButton.state == .on {
  215. if (self.standardComboBox.indexOfSelectedItem > self.standardComboBox.objectValues.count) {
  216. return
  217. }
  218. let size = self.standardComboBox.objectValues[self.standardComboBox.indexOfSelectedItem] as? String ?? ""
  219. let tSize = KMPageSizeTool.getPaperSize(paperName: size)
  220. width = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.width).stringToCGFloat()
  221. height = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.height).stringToCGFloat()
  222. } else if (self.customButton.state == .on) {
  223. height = self.pageHeightTextField.floatValue.cgFloat
  224. width = self.pageWidthTextField.floatValue.cgFloat
  225. width = KMPageSizeTool.conversion(with: "mm", from: self.companyComboBox.stringValue, value: width).stringToCGFloat()
  226. height = KMPageSizeTool.conversion(with: "mm", from: self.companyComboBox.stringValue, value: height).stringToCGFloat()
  227. if(!(width > 0 && height > 0)) {
  228. let alert = NSAlert()
  229. alert.alertStyle = .critical
  230. alert.messageText = KMLocalizedString("Please enter the correct size.", nil)
  231. alert.runModal()
  232. return
  233. }
  234. }
  235. var page = CPDFPage()
  236. page.setBounds(NSMakeRect(0, 0, width * 595 / 210, height * 842 / 297), for: .mediaBox)
  237. self.pageSize = NSMakeSize(width * 595 / 210, height * 842 / 297)
  238. if(self.landscapeButton.state == .on && self.customButton.state == .off) {
  239. page.rotation = 90
  240. self.pageRotation = 90
  241. } else {
  242. if self.customButton.state == .on && self.landscapeButton.state == .on {
  243. self.pageRotation = 90
  244. }
  245. }
  246. pages.append(page)
  247. var index = 0
  248. if (self.firstButton.state == .on) {
  249. index = 0
  250. } else if (self.lastButton.state == .on) {
  251. index = Int(self.pdfDocument?.pageCount ?? 0)
  252. } else {
  253. index = Int(self.pageTextField.stringValue) ?? 0
  254. if ((1 == self.locationRangePopUpButton.indexOfSelectedItem)) {
  255. index = index - 1
  256. }
  257. }
  258. for page in pages {
  259. self.insertPages.append(page)
  260. }
  261. self._insertPageIndex = index
  262. guard let block = self.callback else {
  263. return
  264. }
  265. block(self.pdfDocument, self._password, self.insertPages, self._insertPageIndex)
  266. }
  267. @IBAction func after_Action(_ sender: AnyObject?) {
  268. }
  269. @IBAction func before_Action(_ sender: AnyObject?) {
  270. }
  271. }
  272. extension KMPDFEditInsertBlankPageWindow: NSTextFieldDelegate {
  273. func controlTextDidChange(_ obj: Notification) {
  274. let textField = obj.object as? NSTextField
  275. if (textField == self.pageTextField) {
  276. if let sValue = Int(textField!.stringValue), sValue <= 0 {
  277. self.pageTextField.stringValue = "1"
  278. self.byPageStepper.integerValue = 1
  279. } else if let sValue = Double(textField!.stringValue), sValue > self.byPageStepper.maxValue {
  280. self.pageTextField.stringValue = String(format: "%.0f", self.byPageStepper.maxValue)
  281. self.byPageStepper.doubleValue = self.byPageStepper.maxValue
  282. } else {
  283. self.byPageStepper.integerValue = Int(self.pageTextField.stringValue) ?? 0
  284. }
  285. } else if (textField == self.pageWidthTextField) {
  286. if let fValue = textField?.floatValue, fValue > self.pageHeightTextField.floatValue {
  287. self.portraitButton.state = .off
  288. self.landscapeButton.state = .on
  289. } else {
  290. self.portraitButton.state = .on
  291. self.landscapeButton.state = .off
  292. }
  293. } else if (textField == self.pageHeightTextField) {
  294. if let fValue = textField?.floatValue, fValue > self.pageWidthTextField.floatValue {
  295. self.portraitButton.state = .on
  296. self.landscapeButton.state = .off
  297. } else {
  298. self.portraitButton.state = .off
  299. self.landscapeButton.state = .on
  300. }
  301. }
  302. }
  303. }