KMEditImagePropertyViewController.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. //
  2. // KMEditImagePropertyViewController.swift
  3. // PDF Master
  4. //
  5. // Created by lxy on 2022/12/28.
  6. //
  7. import Cocoa
  8. class KMEditImagePropertyViewController: NSViewController {
  9. @IBOutlet weak var titleLabel: NSTextField!
  10. @IBOutlet weak var editImageView: NSImageView!
  11. @IBOutlet weak var opacityTitleLabel: NSTextField!
  12. @IBOutlet weak var opacitySlider: NSSlider!
  13. @IBOutlet weak var opacityPopUpButton: KMComboBox!
  14. @IBOutlet weak var alignTitleLabel: NSTextField!
  15. @IBOutlet weak var cutButton: NSButton!
  16. @IBOutlet weak var replaceButton: NSButton!
  17. @IBOutlet weak var exportButton: NSButton!
  18. @IBOutlet weak var headerBox: NSBox!
  19. @IBOutlet weak var cancelCutButton: NSButton!
  20. @IBOutlet weak var cutRightConstraint: NSLayoutConstraint!
  21. @IBOutlet weak var headerBoxHeight: NSLayoutConstraint!
  22. @IBOutlet weak var imageBox: NSBox!
  23. @IBOutlet weak var imageBoxHeight: NSLayoutConstraint!
  24. @IBOutlet weak var opacityBoxHeight: NSLayoutConstraint!
  25. @IBOutlet weak var opacityBox: NSBox!
  26. @IBOutlet weak var buttonBoxHeight: NSLayoutConstraint!
  27. @IBOutlet weak var buttonBox: NSBox!
  28. @IBOutlet weak var areasLeftButton: NSButton!
  29. @IBOutlet weak var areasVerticalCenterButton: NSButton!
  30. @IBOutlet weak var areasHorzontalButton: NSButton!
  31. @IBOutlet weak var areasVerticalButton: NSButton!
  32. @IBOutlet weak var areasBotoomButton: NSButton!
  33. @IBOutlet weak var areasHorizontalCenterButton: NSButton!
  34. @IBOutlet weak var areasTopButton: NSButton!
  35. @IBOutlet weak var areasRightButton: NSButton!
  36. let items = ["10","20","50","80","100"]
  37. var imagesAreas : [CPDFEditImageArea] = []
  38. var listView : CPDFListView!
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. self.updateButtonState(hidden: true)
  42. self.editImageView.wantsLayer = true
  43. self.editImageView.layer?.backgroundColor = NSColor.white.cgColor
  44. self.opacityPopUpButton.removeAllItems()
  45. for i in 0 ... items.count-1 {
  46. self.opacityPopUpButton.addItem(withObjectValue: "\(items[i])%")
  47. }
  48. self.opacityPopUpButton.selectItem(at:items.count-1)
  49. self.titleLabel.stringValue = NSLocalizedString("Image", comment: "")
  50. self.opacityTitleLabel.stringValue = NSLocalizedString("Opacity", comment: "")
  51. self.cutButton.title = NSLocalizedString("Crop", comment: "")
  52. self.cancelCutButton.title = NSLocalizedString("Cancel", comment: "")
  53. self.replaceButton.title = NSLocalizedString("Replace", comment: "")
  54. self.exportButton.title = NSLocalizedString("Extract", comment: "")
  55. self.alignTitleLabel.stringValue = NSLocalizedString("Align", comment: "")
  56. self.updateButtonState(hidden: true)
  57. self.reloadData()
  58. }
  59. func reloadData() {
  60. self.formAearsAlignIcon()
  61. imagesAreas = []
  62. if self.listView.editingAreas()?.count ?? 0 < 1 {
  63. return
  64. }
  65. let areas = self.listView.editingAreas()
  66. for i in 0 ... areas!.count-1 {
  67. if areas![i] is CPDFEditImageArea {
  68. imagesAreas.append(areas![i] as! CPDFEditImageArea)
  69. }
  70. }
  71. if imagesAreas.count == 1 && imagesAreas.count == areas!.count{ //单个图片
  72. self.listView.selectImageAreas = imagesAreas.first
  73. self.headerBox.isHidden = false
  74. self.headerBoxHeight.constant = 170
  75. self.imageBox.isHidden = false
  76. self.imageBoxHeight.constant = 90
  77. self.opacityBox.isHidden = false
  78. self.opacityBoxHeight.constant = 48
  79. self.buttonBox.isHidden = false
  80. self.cutButton.isHidden = false
  81. self.cancelCutButton.isHidden = false
  82. self.replaceButton.isHidden = false
  83. self.buttonBoxHeight.constant = 100
  84. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  85. self.opacitySlider.objectValue = self.listView.opacity(for: imagesAreas.first)
  86. self.opacityPopUpButton.stringValue = "\(Int(self.listView.opacity(for: imagesAreas.first)*100))%"
  87. } else if imagesAreas.count > 1 && imagesAreas.count == areas!.count { //多选图片
  88. self.headerBox.isHidden = false
  89. self.headerBoxHeight.constant = 80
  90. self.imageBox.isHidden = true
  91. self.imageBoxHeight.constant = 0
  92. self.opacityBox.isHidden = false
  93. self.opacityBoxHeight.constant = 48
  94. self.buttonBox.isHidden = false
  95. self.cutButton.isHidden = true
  96. self.cancelCutButton.isHidden = true
  97. self.replaceButton.isHidden = true
  98. self.buttonBoxHeight.constant = 52
  99. var opacity = self.listView.opacity(for: imagesAreas.first)
  100. for area in imagesAreas {
  101. let newOpacity = self.listView.opacity(for: area)
  102. if (opacity < newOpacity) {
  103. opacity = newOpacity
  104. }
  105. }
  106. self.opacitySlider.objectValue = opacity
  107. self.opacityPopUpButton.stringValue = "\(Int(opacity)*100))%"
  108. } else if imagesAreas.count > 0 && imagesAreas.count != areas!.count { //多选图片跟文字
  109. self.headerBox.isHidden = true
  110. self.headerBoxHeight.constant = 0
  111. self.imageBox.isHidden = true
  112. self.imageBoxHeight.constant = 0
  113. self.opacityBox.isHidden = true
  114. self.opacityBoxHeight.constant = 0
  115. self.buttonBox.isHidden = true
  116. self.buttonBoxHeight.constant = 0
  117. }
  118. }
  119. //MARK: action
  120. @IBAction func selectOpacityAction(_ sender: KMComboBox) {
  121. let value = sender.indexOfItem(withObjectValue: sender.stringValue)
  122. if self.listView.editingAreas()?.count ?? 0 < 1 {
  123. return
  124. }
  125. self.updateImageAreasOpacity(opacity: Float("\(items[value])")!/100.0)
  126. self.opacitySlider.objectValue = Float("\(items[value])")!/100.0
  127. }
  128. @IBAction func updateopacitySlider(_ sender: NSSlider) {
  129. let value = sender.floatValue
  130. if self.listView.editingAreas()?.count ?? 0 < 1 {
  131. return
  132. }
  133. self.updateImageAreasOpacity(opacity:value)
  134. self.opacityPopUpButton.stringValue = "\(Int(value*100))%"
  135. }
  136. @IBAction func rightRotationImageAction(_ sender: Any) {
  137. if self.listView.editingAreas()?.count ?? 0 < 1 {
  138. return
  139. }
  140. let areas = self.listView.editingAreas()
  141. if areas!.count == 1 && (areas!.first is CPDFEditImageArea) {
  142. let imageArea = areas!.first as! CPDFEditImageArea
  143. self.listView.rotate(with: imageArea, rotate: 90)
  144. self.listView.selectImageAreas = imageArea
  145. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  146. } else if areas!.count > 1 {
  147. for imagesArea in imagesAreas {
  148. self.listView.rotate(with: imagesArea, rotate: 90)
  149. }
  150. }
  151. }
  152. @IBAction func leftRotationImageAction(_ sender: Any) {
  153. if self.listView.editingAreas()?.count ?? 0 < 1 {
  154. return
  155. }
  156. let areas = self.listView.editingAreas()
  157. if areas!.count == 1 && (areas!.first is CPDFEditImageArea) {
  158. self.listView.rotate(with: self.listView.selectImageAreas, rotate: -90)
  159. if self.listView.editingAreas()!.count == 1 && (self.listView.editingAreas()!.first is CPDFEditImageArea) {
  160. self.listView.selectImageAreas = self.listView.editingAreas()!.first as! CPDFEditImageArea
  161. }
  162. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  163. } else {
  164. for imagesArea in imagesAreas {
  165. self.listView.rotate(with: imagesArea, rotate: -90)
  166. }
  167. }
  168. }
  169. @IBAction func cutImageAction(_ sender: Any) {
  170. if self.listView.selectImageAreas == nil {
  171. return
  172. }
  173. if self.listView.cropAreas != nil && self.listView.selectImageAreas != nil{
  174. self.listView.cropEditImageArea(self.listView.selectImageAreas, withBounds: self.listView.cropAreas.cropRect)
  175. } else if self.listView.selectImageAreas != nil {
  176. self.listView.isEditImage = true
  177. self.listView.enterCrop(with: self.listView.selectImageAreas)
  178. self.updateButtonState(hidden: false)
  179. }
  180. }
  181. @IBAction func cancelCutImageAction(_ sender: Any) {
  182. if self.listView.selectImageAreas == nil {
  183. return
  184. }
  185. self.listView.exitCrop(with: self.listView.selectImageAreas)
  186. self.listView.cropAreas = nil
  187. self.listView.isEditImage = false
  188. self.updateButtonState(hidden: true)
  189. }
  190. @IBAction func replaceImageAction(_ sender: NSButton) {
  191. if self.listView.selectImageAreas == nil {
  192. return
  193. }
  194. let panel = NSOpenPanel()
  195. panel.allowsMultipleSelection = false
  196. panel.allowedFileTypes = ["png","jpg"]
  197. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  198. if response == .OK {
  199. let openPath = panel.url?.path
  200. // let s = self.listView.extractImage(with: self.selectImageAreas, toImagePath: openPath!)
  201. let s = self.listView.replace(self.listView.selectImageAreas, imagePath: openPath!)
  202. if s {
  203. }
  204. }
  205. }
  206. }
  207. @available(macOS 13.0, *)
  208. @IBAction func exportImageAction(_ sender: Any) {
  209. if self.listView.selectImageAreas == nil {
  210. return
  211. }
  212. if imagesAreas.count == 1 {
  213. let panel = NSSavePanel()
  214. panel.nameFieldStringValue = "\(NSLocalizedString("Untitled", comment: "")).png"
  215. let button = NSButton.init(checkboxWithTitle: NSLocalizedString("Open the document after saving", comment: ""), target: nil, action: nil)
  216. button.state = .on
  217. panel.accessoryView = button
  218. panel.isExtensionHidden = true
  219. let response = panel.runModal()
  220. if response == .OK {
  221. let url = panel.url
  222. let result = self.listView.extractImage(with: self.listView.selectImageAreas, toImagePath: url!.path)
  223. if result {
  224. if button.state == .on { /// 开启文档
  225. NSWorkspace.shared.openFile(url!.path)
  226. } else {
  227. }
  228. }
  229. }
  230. } else if imagesAreas.count > 1 {
  231. let panel = NSOpenPanel()
  232. panel.canChooseFiles = false
  233. panel.canChooseDirectories = true
  234. panel.canCreateDirectories = true
  235. panel.allowsMultipleSelection = false
  236. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  237. if response == .OK {
  238. let outputURL = panel.url
  239. let s = self.listView.document.documentURL.lastPathComponent
  240. let folderPath = self.listView.document.documentURL.deletingPathExtension().lastPathComponent + "_extract"
  241. var filePath = outputURL?.path.stringByAppendingPathComponent(folderPath)
  242. var i = 1
  243. let testFilePath = filePath
  244. while FileManager.default.fileExists(atPath: filePath!) {
  245. filePath = testFilePath! + "\(i)"
  246. i = i + 1
  247. }
  248. try? FileManager.default.createDirectory(atPath: filePath!, withIntermediateDirectories: false, attributes: nil)
  249. var saveURLs : [URL] = []
  250. for j in 0 ... self.imagesAreas.count-1 {
  251. let documentFileName = self.listView.document.documentURL.deletingPathExtension().lastPathComponent
  252. var outPath = filePath!
  253. outPath = outPath.stringByAppendingPathComponent(documentFileName)
  254. outPath = outPath + "page \(j+1)"
  255. outPath = outPath.stringByAppendingPathExtension("png")
  256. let result = self.listView.extractImage(with: self.imagesAreas[j], toImagePath: outPath)
  257. if result {
  258. saveURLs.append(URL(fileURLWithPath: outPath))
  259. }
  260. }
  261. NSWorkspace.shared.activateFileViewerSelecting(saveURLs)
  262. }
  263. }
  264. }
  265. }
  266. override func mouseDown(with event: NSEvent) {
  267. }
  268. //MARK: 对齐
  269. @IBAction func areasAlignStyleAction(_ sender: NSButton) {
  270. self.formAearsAlignMangent(stype: CPDFActiveAreasAlignType.init(rawValue: sender.tag) ?? .Left)
  271. }
  272. func updateButtonState(hidden:Bool) {
  273. if hidden {
  274. self.cancelCutButton.isHidden = true
  275. self.cutRightConstraint.constant = 10
  276. self.cutButton.title = NSLocalizedString("cut", comment: "")
  277. self.replaceButton.isEnabled = true
  278. self.exportButton.isEnabled = true
  279. } else {
  280. self.cancelCutButton.isHidden = false
  281. self.cutRightConstraint.constant = 80
  282. self.cutButton.title = NSLocalizedString("sure cut", comment: "")
  283. self.replaceButton.isEnabled = false
  284. self.exportButton.isEnabled = false
  285. }
  286. }
  287. func updateImageAreasOpacity (opacity:Float) {
  288. for area in imagesAreas {
  289. self.listView.setOpacityEditArea(area, opacity: CGFloat(opacity))
  290. }
  291. if self.listView.editingAreas()!.count == 1 && (self.listView.editingAreas()!.first is CPDFEditImageArea) {
  292. self.listView.selectImageAreas = self.listView.editingAreas()!.first as! CPDFEditImageArea
  293. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  294. self.opacitySlider.objectValue = self.listView.opacity(for: self.listView.selectImageAreas)
  295. }
  296. }
  297. func formAearsAlignIcon() {
  298. let norIcons : [String] = ["KMImageNameEditPDFImage20","KMImageNameEditPDFImage21","KMImageNameEditPDFImage45","KMImageNameEditPDFImage46",
  299. "KMImageNameEditPDFImage47","KMImageNameEditPDFImage48","KMImageNameEditPDFImage49","KMImageNameEditPDFImage50"]
  300. let selectIcons : [String] = ["KMImageNameEditPDFImage20_1","KMImageNameEditPDFImage21_1","KMImageNameEditPDFImage45_1","KMImageNameEditPDFImage46_1",
  301. "KMImageNameEditPDFImage47_1","KMImageNameEditPDFImage48_1","KMImageNameEditPDFImage49_1","KMImageNameEditPDFImage50_1"]
  302. let areasButtons : [NSButton] = [self.areasVerticalButton,self.areasHorzontalButton,self.areasLeftButton,self.areasVerticalCenterButton,
  303. self.areasRightButton,self.areasTopButton,self.areasHorizontalCenterButton,self.areasBotoomButton]
  304. if self.listView.editingAreas()?.count ?? 0 < 2 {
  305. for i in 0 ... areasButtons.count-1 {
  306. areasButtons[i].image = NSImage(named: norIcons[i])
  307. }
  308. } else if self.listView.editingAreas()?.count ?? 0 == 2 {
  309. for i in 0 ... areasButtons.count-1 {
  310. if i == 0 || i == 1 {
  311. areasButtons[i].image = NSImage(named: norIcons[i])
  312. } else {
  313. areasButtons[i].image = NSImage(named: selectIcons[i])
  314. }
  315. }
  316. } else if self.listView.editingAreas()?.count ?? 0 > 2 {
  317. for i in 0 ... areasButtons.count-1 {
  318. areasButtons[i].image = NSImage(named: selectIcons[i])
  319. }
  320. }
  321. }
  322. private func formAearsAlignMangent(stype:CPDFActiveAreasAlignType) {
  323. if self.listView.editingAreas().count < 2 {
  324. return
  325. }
  326. var zeroRect = NSRect.null
  327. var highestRect = NSZeroRect
  328. var widthestRect = NSZeroRect
  329. let fristArea : CPDFEditArea = self.listView.editingAreas().first as! CPDFEditArea
  330. var leftestRect = fristArea.bounds
  331. var rightestRect = fristArea.bounds
  332. var topestRect = fristArea.bounds
  333. var bottomestRect = fristArea.bounds
  334. var leftestArea : CPDFEditArea = fristArea
  335. var rightestArea : CPDFEditArea = fristArea
  336. var topestArea : CPDFEditArea = fristArea
  337. var bottomestArea : CPDFEditArea = fristArea
  338. var totalWidth = 0.0
  339. var totalHeight = 0.0
  340. for i in 0 ... self.listView.editingAreas().count-1 {
  341. let area : CPDFEditArea = self.listView.editingAreas()[i] as! CPDFEditArea
  342. zeroRect = zeroRect.union(area.bounds)
  343. totalWidth = totalWidth + area.bounds.width
  344. totalHeight = totalHeight + area.bounds.height
  345. if area.bounds.height > highestRect.height {
  346. highestRect = area.bounds
  347. }
  348. if area.bounds.width > widthestRect.width {
  349. widthestRect = area.bounds
  350. }
  351. if leftestRect.minX > area.bounds.minX {
  352. leftestRect = area.bounds
  353. leftestArea = area
  354. }
  355. if area.bounds.maxX > rightestRect.maxX {
  356. rightestRect = area.bounds
  357. rightestArea = area
  358. }
  359. if area.bounds.maxY > topestRect.maxY {
  360. topestRect = area.bounds
  361. topestArea = area
  362. }
  363. if bottomestRect.minY > area.bounds.minY {
  364. bottomestRect = area.bounds
  365. bottomestArea = area
  366. }
  367. }
  368. if stype == .Left {
  369. var newBoundsArray : [String] = []
  370. for i in 0 ... self.listView.editingAreas().count-1 {
  371. let areas = self.listView.editingAreas()[i] as! CPDFEditArea
  372. var bounds = areas.bounds
  373. bounds.origin.x = zeroRect.origin.x
  374. newBoundsArray.append(NSStringFromRect(bounds))
  375. }
  376. self.changeAreasAlign(areas: self.listView.editingAreas(), newBounds: newBoundsArray)
  377. } else if stype == .Right {
  378. var newBounds : [String] = []
  379. for i in 0 ... self.listView.editingAreas().count-1 {
  380. let areas = self.listView.editingAreas()[i] as! CPDFEditArea
  381. var bounds = areas.bounds
  382. bounds.origin.x = zeroRect.maxX - bounds.size.width
  383. newBounds.append(NSStringFromRect(bounds))
  384. }
  385. self.changeAreasAlign(areas: self.listView.editingAreas(), newBounds: newBounds)
  386. } else if stype == .Top {
  387. var newBounds : [String] = []
  388. for i in 0 ... self.listView.editingAreas().count-1 {
  389. let areas = self.listView.editingAreas()[i] as! CPDFEditArea
  390. var bounds = areas.bounds
  391. bounds.origin.y = zeroRect.maxY - bounds.size.height
  392. newBounds.append(NSStringFromRect(bounds))
  393. }
  394. self.changeAreasAlign(areas: self.listView.editingAreas(), newBounds: newBounds)
  395. } else if stype == .Bottom {
  396. var newBounds : [String] = []
  397. for i in 0 ... self.listView.editingAreas().count-1 {
  398. let areas = self.listView.editingAreas()[i] as! CPDFEditArea
  399. var bounds = areas.bounds
  400. bounds.origin.y = zeroRect.minY
  401. newBounds.append(NSStringFromRect(bounds))
  402. }
  403. self.changeAreasAlign(areas: self.listView.editingAreas(), newBounds: newBounds)
  404. } else if stype == .Horizontally {
  405. var newBounds : [String] = []
  406. for i in 0 ... self.listView.editingAreas().count-1 {
  407. let areas = self.listView.editingAreas()[i] as! CPDFEditArea
  408. var bounds = areas.bounds
  409. bounds.origin.y = highestRect.midY - bounds.height/2
  410. newBounds.append(NSStringFromRect(bounds))
  411. }
  412. self.changeAreasAlign(areas: self.listView.editingAreas(), newBounds: newBounds)
  413. } else if stype == .Vertical {
  414. var newBounds : [String] = []
  415. for i in 0 ... self.listView.editingAreas().count-1 {
  416. let areas = self.listView.editingAreas()[i] as! CPDFEditArea
  417. var bounds = areas.bounds
  418. bounds.origin.x = widthestRect.midX - bounds.width/2
  419. newBounds.append(NSStringFromRect(bounds))
  420. }
  421. self.changeAreasAlign(areas: self.listView.editingAreas(), newBounds: newBounds)
  422. } else if stype == .DisHorizontally {
  423. let middleGap = zeroRect.width - leftestRect.width - rightestRect.width
  424. let otherAreasTotalWidth = totalWidth - leftestRect.width - rightestRect.width
  425. let gap = (middleGap - otherAreasTotalWidth)/CGFloat(self.listView.editingAreas().count - 1)
  426. var areasCopyArray : [CPDFEditArea] = self.listView.editingAreas() as! [CPDFEditArea]
  427. areasCopyArray.sorted(by: { obj1, obj2 in
  428. let area1 = obj1
  429. let area2 = obj2
  430. if area1.bounds.origin.x < area2.bounds.origin.x {
  431. return true
  432. } else {
  433. return false
  434. }
  435. })
  436. if let index = areasCopyArray.firstIndex(of: leftestArea) {
  437. areasCopyArray.remove(at: index)
  438. }
  439. if let index = areasCopyArray.firstIndex(of: rightestArea) {
  440. areasCopyArray.remove(at: index)
  441. }
  442. var newBounds : [String] = []
  443. var leftStartX = leftestRect.maxX + gap
  444. for i in 0 ... areasCopyArray.count-1 {
  445. let areas = areasCopyArray[i]
  446. var bounds = areas.bounds
  447. bounds.origin.x = leftStartX
  448. newBounds.append(NSStringFromRect(bounds))
  449. leftStartX = leftStartX + bounds.width + gap
  450. }
  451. self.changeAreasAlign(areas: areasCopyArray, newBounds: newBounds)
  452. } else if stype == .DisVertical {
  453. let middleGap = zeroRect.height - topestRect.height - bottomestRect.height
  454. let otherAreasTotalHeight = totalHeight - topestRect.height - bottomestRect.height
  455. let gap = (middleGap - otherAreasTotalHeight)/CGFloat(self.listView.editingAreas().count - 1)
  456. var areasCopyArray : [CPDFEditArea] = self.listView.editingAreas() as! [CPDFEditArea]
  457. areasCopyArray.sorted(by: { obj1, obj2 in
  458. let area1 = obj1
  459. let area2 = obj2
  460. if area1.bounds.origin.x < area2.bounds.origin.x {
  461. return true
  462. } else {
  463. return false
  464. }
  465. })
  466. if let index = areasCopyArray.firstIndex(of: topestArea) {
  467. areasCopyArray.remove(at: index)
  468. }
  469. if let index = areasCopyArray.firstIndex(of: bottomestArea) {
  470. areasCopyArray.remove(at: index)
  471. }
  472. var newBounds : [String] = []
  473. var bottomStartY = bottomestRect.maxY + gap
  474. for i in 0 ... areasCopyArray.count-1 {
  475. let areas = areasCopyArray[i]
  476. var bounds = areas.bounds
  477. bounds.origin.y = bottomStartY
  478. newBounds.append(NSStringFromRect(bounds))
  479. bottomStartY = bottomStartY + bounds.height + gap
  480. }
  481. self.changeAreasAlign(areas: areasCopyArray, newBounds: newBounds)
  482. }
  483. }
  484. private func changeAreasAlign(areas:[Any],newBounds:[String]) {
  485. var oldBounds : [String] = []
  486. for i in 0 ... areas.count-1 {
  487. let area : CPDFEditArea = areas[i] as! CPDFEditArea
  488. oldBounds.append(NSStringFromRect(area.bounds))
  489. self.listView.setBoundsEditArea(area, withBounds: NSRectFromString(newBounds[i]))
  490. }
  491. self.listView.setNeedsDisplayForVisiblePages()
  492. }
  493. }