KMBatchOperateConvertViewController.swift 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. //
  2. // KMBatchOperateConvertViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/11/1.
  6. //
  7. import Cocoa
  8. typealias detailInfoViewMouseDownCallback = (_ mouseDown: Bool) -> ()
  9. class KMDetailInfoView: NSView{
  10. var mouseDownCallback: detailInfoViewMouseDownCallback?
  11. override func mouseDown(with event: NSEvent) {
  12. super.mouseDown(with: event)
  13. guard let callBack = mouseDownCallback else { return }
  14. callBack(true)
  15. }
  16. }
  17. class KMBatchOperateConvertViewController: KMBatchOperateBaseViewController{
  18. var dataSourcesArray: [KMConvertWithPDFType]?
  19. @IBOutlet var bottomView: NSView!
  20. @IBOutlet var convertButton: NSButton!
  21. @IBOutlet var buttonBottemOffset: NSLayoutConstraint!
  22. @IBOutlet var detailInfoLabel: NSTextField!
  23. @IBOutlet var detailInfoBox: NSBox!
  24. @IBOutlet var separateStypeTopOffset: NSLayoutConstraint!
  25. @IBOutlet var separateStypeBox: NSBox!
  26. @IBOutlet var wordButtonOne: NSButton!
  27. @IBOutlet var wordButtonTwo: NSButton!
  28. @IBOutlet var excelStyleBox: NSBox!
  29. @IBOutlet var allContentMenu: NSPopUpButton!
  30. @IBOutlet var allContentMenu2: NSMenuItem!
  31. @IBOutlet var allContentMenu3: NSMenuItem!
  32. @IBOutlet var onlyTextBtn: NSButton!
  33. @IBOutlet var onlyTableBtn: NSButton!
  34. @IBOutlet var allContentBtn: NSButton!
  35. @IBOutlet var tableMenu1: NSMenuItem!
  36. @IBOutlet var tableMenu2: NSMenuItem!
  37. @IBOutlet var tableMenu3: NSMenuItem!
  38. @IBOutlet var tableMenu: NSPopUpButton!
  39. @IBOutlet var csvExtractButton: NSButton!
  40. @IBOutlet var ocrBox: NSBox!
  41. @IBOutlet var ocrLabel: NSTextField!
  42. @IBOutlet var ocrButton: NSButton!
  43. @IBOutlet var ocrLanguageBox: NSPopUpButton!
  44. @IBOutlet var ocrTopOffset: NSLayoutConstraint!
  45. @IBOutlet var headerView: KMConvertCollectionViewHeader!
  46. @IBOutlet var headerTopOffset: NSLayoutConstraint!
  47. @IBOutlet var formatLabel: NSTextField!
  48. @IBOutlet var formatSelectButton: NSPopUpButton!
  49. var excelContentOption: CPDFConvertExcelContentOptions?
  50. var excelWorksheetOption: CPDFConvertExcelWorksheetOptions?
  51. var haveFiles: Bool = false
  52. private var ocrLanguage_: COCRLanguage = .chinese
  53. deinit {
  54. NotificationCenter.default.removeObserver(self)
  55. }
  56. override var interfaceStatus: KMBatchOperateInterfaceStatus?{
  57. set{
  58. super.interfaceStatus = newValue
  59. self.convertButton.isEnabled = true
  60. if newValue == .Processing {
  61. formatSelectButton.isEnabled = false
  62. self.convertButton.title = NSLocalizedString("Cancel", comment: "")
  63. self.convertButton.tag = 2
  64. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color())
  65. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  66. if self.convertType == .WordStandard {
  67. self.convertButton.title = NSLocalizedString("Convert", comment: "")
  68. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  69. self.convertButton.isEnabled = false
  70. }
  71. } else {
  72. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  73. var files: [URL] = []
  74. for url in self.successFilePathURLArray! {
  75. if FileManager.default.fileExists(atPath: url.path) {
  76. files.append(url)
  77. }
  78. }
  79. if files.count > 0 {
  80. let workspace = NSWorkspace.shared
  81. workspace.activateFileViewerSelecting(files)
  82. }
  83. }
  84. formatSelectButton.isEnabled = true
  85. self.convertButton.title = NSLocalizedString("Convert", comment: "")
  86. self.convertButton.tag = 1
  87. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color())
  88. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  89. }
  90. }
  91. get{
  92. return super.interfaceStatus
  93. }
  94. }
  95. override var convertType: KMConvertWithPDFType?{
  96. set{
  97. super.convertType = newValue
  98. changeAllFilesToConvertType(convertType!)
  99. fetchFileListViewController()?.reloadConvertInterface(withType: convertType!)
  100. }
  101. get{
  102. return super.convertType
  103. }
  104. }
  105. func changeAllFilesToConvertType(_ type: KMConvertWithPDFType) {
  106. for i in 0..<self.files!.count {
  107. let file = self.files![i]
  108. file.convertType = type
  109. }
  110. }
  111. func switchToConvertType(_ convertType: KMConvertWithPDFType) {
  112. self.convertType = convertType
  113. self.manualSelectRow(convertType)
  114. self.updateBottomView()
  115. }
  116. func manualSelectRow(_ convertType: KMConvertWithPDFType) {
  117. var index = -1
  118. updateFormatView()
  119. for i in 0..<self.dataSourcesArray!.count {
  120. let number = self.dataSourcesArray![i]
  121. if number.rawValue == convertType.rawValue {
  122. index = i
  123. break
  124. }
  125. }
  126. if index != -1 {
  127. self.formatSelectButton.selectItem(at: index)
  128. }
  129. }
  130. func updateBottomView() {
  131. var isShowUpgrade = false
  132. #if VERSION_FREE
  133. #if VERSION_DMG
  134. // 桌机版
  135. if IAPProductsManager.default().isAvailableAllFunction() {
  136. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  137. } else {
  138. isShowUpgrade = true
  139. }
  140. } else {
  141. isShowUpgrade = true
  142. }
  143. #else
  144. // 免费版
  145. if IAPProductsManager.default().isAvailableAllFunction() {
  146. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  147. } else {
  148. isShowUpgrade = true
  149. }
  150. } else {
  151. isShowUpgrade = true
  152. }
  153. #endif
  154. #else
  155. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  156. } else {
  157. isShowUpgrade = true
  158. }
  159. #endif
  160. if !isShowUpgrade {
  161. headerView.isHidden = true
  162. headerTopOffset.constant = 0
  163. } else {
  164. headerView.isHidden = false
  165. headerTopOffset.constant = self.headerView.frame.height
  166. }
  167. var moreLabelString = ""
  168. #if VERSION_FREE
  169. if !IAPProductsManager.default().isAvailableAllFunction() {
  170. moreLabelString = String(format: "%@ %@", KMLocalizedString("The first 10 pages for free"), KMLocalizedString("Unlimited Convert"))
  171. }else {
  172. if !IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  173. if .WordAdvance == self.convertType ||
  174. .WordStandard == self.convertType ||
  175. .Excel == self.convertType ||
  176. .PowerPoint == self.convertType ||
  177. .CSV == self.convertType ||
  178. .RTF == self.convertType ||
  179. .GIF == self.convertType ||
  180. .TIFF == self.convertType ||
  181. .JPEG2000 == self.convertType ||
  182. .BMP == self.convertType ||
  183. .TGA == self.convertType ||
  184. .Json == self.convertType {
  185. moreLabelString = String(format: "%@ %@", KMLocalizedString("The first 10 pages for free"), KMLocalizedString("Unlimited Convert"))
  186. }
  187. }
  188. }
  189. #else
  190. // 付费版
  191. if !IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  192. if .WordAdvance == self.convertType ||
  193. .WordStandard == self.convertType ||
  194. .Excel == self.convertType ||
  195. .PowerPoint == self.convertType ||
  196. .CSV == self.convertType ||
  197. .RTF == self.convertType ||
  198. .GIF == self.convertType ||
  199. .TIFF == self.convertType ||
  200. .JPEG2000 == self.convertType ||
  201. .BMP == self.convertType ||
  202. .TGA == self.convertType ||
  203. .Json == self.convertType {
  204. moreLabelString = String(format: "%@ %@", KMLocalizedString("The first 10 pages for free"), KMLocalizedString("Unlimited Convert"))
  205. }
  206. }
  207. #endif
  208. self.detailInfoLabel.stringValue = moreLabelString
  209. if let range = moreLabelString.range(of: NSLocalizedString("Unlimited Convert", comment: "")) {
  210. let newR: NSRange = moreLabelString.nsRange(from: range)!
  211. let attributedStr = NSMutableAttributedString(string: moreLabelString)
  212. attributedStr.addAttribute(.foregroundColor, value: NSColor.labelColor, range: NSRange(location: 0, length: newR.location - 1))
  213. attributedStr.addAttribute(.foregroundColor, value: NSColor(red: 8/255, green: 124/255, blue: 1, alpha: 1), range: newR)
  214. attributedStr.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: newR)
  215. detailInfoLabel.attributedStringValue = attributedStr
  216. }
  217. if moreLabelString.count > 0 {
  218. detailInfoBox.isHidden = false
  219. buttonBottemOffset.constant = 45
  220. } else {
  221. detailInfoBox.isHidden = true
  222. buttonBottemOffset.constant = 10
  223. }
  224. if self.convertType == .WordAdvance {
  225. separateStypeBox.isHidden = false
  226. excelStyleBox.isHidden = true
  227. csvExtractButton.isHidden = true
  228. ocrBox.isHidden = false
  229. wordButtonOne.title = NSLocalizedString("Retain Flowing Text", comment: "")
  230. wordButtonTwo.title = NSLocalizedString("Retain Page Layout", comment: "")
  231. ocrTopOffset.constant = 15 + separateStypeBox.frame.height
  232. } else if self.convertType == .Excel {
  233. separateStypeBox.isHidden = true
  234. excelStyleBox.isHidden = false
  235. csvExtractButton.isHidden = true
  236. ocrBox.isHidden = false
  237. ocrTopOffset.constant = 15 + excelStyleBox.frame.height
  238. } else if .Json == convertType {
  239. wordButtonOne.title = NSLocalizedString("Extract text content", comment: "")
  240. wordButtonTwo.title = NSLocalizedString("Extract tables", comment: "")
  241. separateStypeBox.isHidden = false
  242. excelStyleBox.isHidden = true
  243. csvExtractButton.isHidden = true
  244. ocrBox.isHidden = false
  245. ocrTopOffset.constant = 15 + separateStypeBox.frame.height
  246. } else if (.PowerPoint == convertType ||
  247. .RTF == convertType ||
  248. .HTML == convertType ||
  249. .Text == convertType) {
  250. separateStypeBox.isHidden = true
  251. excelStyleBox.isHidden = true
  252. csvExtractButton.isHidden = true
  253. ocrBox.isHidden = false
  254. ocrTopOffset.constant = 15
  255. } else if self.convertType == .CSV {
  256. separateStypeBox.isHidden = true
  257. excelStyleBox.isHidden = true
  258. csvExtractButton.isHidden = false
  259. ocrBox.isHidden = true
  260. self.csvExtractButton.title = NSLocalizedString("Extract Tables Only", comment: "")
  261. } else {
  262. separateStypeBox.isHidden = true
  263. excelStyleBox.isHidden = true
  264. csvExtractButton.isHidden = true
  265. ocrBox.isHidden = true
  266. }
  267. }
  268. override func viewDidLoad() {
  269. super.viewDidLoad()
  270. self.prepareData()
  271. updateFormatView()
  272. self.view.wantsLayer = true
  273. convertButton.wantsLayer = true
  274. convertButton.font = NSFont.systemFont(ofSize: 13)
  275. convertButton.layer?.cornerRadius = 1.0
  276. interfaceStatus = .PrepareProcess
  277. allContentBtn.state = .on
  278. allContentMenu.isEnabled = true
  279. tableMenu.isEnabled = false
  280. excelContentOption = .allContent
  281. excelWorksheetOption = .forEachPage
  282. onlyTextBtn.title = NSLocalizedString("Only Text", comment: "")
  283. onlyTableBtn.title = NSLocalizedString("Only Table", comment: "")
  284. allContentBtn.title = NSLocalizedString("All Content", comment: "")
  285. onlyTextBtn.toolTip = NSLocalizedString("Only Text", comment: "")
  286. onlyTableBtn.toolTip = NSLocalizedString("Only Table", comment: "")
  287. allContentBtn.toolTip = NSLocalizedString("All Content", comment: "")
  288. tableMenu1.title = NSLocalizedString("Create Sheet for each Table", comment: "")
  289. tableMenu2.title = NSLocalizedString("Create Sheet for each Page", comment: "")
  290. allContentMenu2.title = NSLocalizedString("Create Sheet for each Page", comment: "")
  291. tableMenu3.title = NSLocalizedString("Create single Sheet for File", comment: "")
  292. allContentMenu3.title = NSLocalizedString("Create single Sheet for File", comment: "")
  293. formatLabel.stringValue = KMLocalizedString("Format:", nil)
  294. ocrBox.title = KMLocalizedString("Text Recognition Settings", nil)
  295. ocrBox.titleFont = .SFProTextBoldFont(14.0)
  296. formatLabel.font = .SFProTextBoldFont(14.0)
  297. headerView.convertHeaderClickedCallBack = { [weak self] in
  298. self?.moreConvertInfoAction()
  299. }
  300. ocrLanguageBox.selectItem(at: 0)
  301. ocrLabel.textColor = KMAppearance.Layout.h2Color()
  302. ocrLanguageBox.isEnabled = false
  303. ocrButton.state = .off
  304. self.updateViewColor()
  305. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: Notification.Name(rawValue: "KMBatchFilesCountNotification"), object: nil)
  306. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductPurchasedNotification(notification:)), name: NSNotification.Name("KMIAPProductPurchasedNotification"), object: nil)
  307. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductRestoreFinishedNotification(notification:)), name: NSNotification.Name("KMIAPProductRestoreFinishedNotification"), object: nil)
  308. // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  309. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  310. NotificationCenter.default.addObserver(self, selector: #selector(deviceActivateStatusChanged(notification:)), name: NSNotification.Name("kDeviceActivateNotification"), object: nil)
  311. }
  312. @objc func batchFilesCountNotification(notification: NSNotification) {
  313. let arr: Array? = notification.object as? [KMBatchOperateFile]
  314. self.files? = arr ?? []
  315. if files!.count > 0 {
  316. self.convertButton.setTitleColor(NSColor.white)
  317. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  318. self.haveFiles = true
  319. } else {
  320. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  321. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  322. self.haveFiles = false
  323. }
  324. }
  325. @objc func themeChanged(notification: NSNotification) {
  326. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  327. self.updateViewColor()
  328. }
  329. }
  330. @objc func IAPProductRestoreFinishedNotification(notification: NSNotification) {
  331. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  332. self.updateBottomView()
  333. self.prepareData()
  334. self.updateFormatView()
  335. }
  336. }
  337. @objc func IAPProductPurchasedNotification(notification: NSNotification) {
  338. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  339. self.updateBottomView()
  340. self.prepareData()
  341. self.updateFormatView()
  342. }
  343. }
  344. @objc func deviceActivateStatusChanged(notification: NSNotification) {
  345. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  346. self.updateBottomView()
  347. self.prepareData()
  348. self.updateFormatView()
  349. }
  350. }
  351. func updateViewColor() {
  352. self.view.wantsLayer = true
  353. if KMAppearance.isDarkMode() {
  354. self.view.layer?.backgroundColor = NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor
  355. } else {
  356. self.view.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  357. }
  358. }
  359. func convertActionVC() {
  360. var vc: KMToolCompareWindowController? = nil
  361. if(.WordStandard == self.convertType ||
  362. .WordAdvance == self.convertType) {
  363. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  364. } else if (.Excel == self.convertType) {
  365. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 2)
  366. } else if (.PowerPoint == self.convertType) {
  367. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 3)
  368. } else if (self.convertType == .JPG || self.convertType == .PNG || self.convertType == .GIF || self.convertType == .TIFF || self.convertType == .TGA || self.convertType == .BMP){
  369. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 4)
  370. } else {
  371. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 0)
  372. }
  373. vc?.showWindow(nil)
  374. }
  375. func prepareData() {
  376. let arr: [KMConvertWithPDFType] = [.Excel, .PowerPoint, .RTF, .CSV, .HTML, .Text, .JPEG, .JPG, .PNG, .GIF, .TIFF, .TGA, .BMP, .JPEG2000, .Json]
  377. self.dataSourcesArray = arr
  378. var needShowAdvance = true
  379. var needShowDefault = true
  380. #if VERSION_FREE
  381. // 桌机版
  382. if IAPProductsManager.default().isAvailableAllFunction() {
  383. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  384. needShowAdvance = true
  385. needShowDefault = false
  386. }
  387. }
  388. #else
  389. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  390. needShowAdvance = true
  391. needShowDefault = false
  392. }
  393. #endif
  394. if needShowDefault {
  395. var wordType: KMConvertWithPDFType = .WordStandard
  396. self.dataSourcesArray?.insert(wordType, at: 0)
  397. }
  398. if needShowAdvance {
  399. var wordType: KMConvertWithPDFType = .WordAdvance
  400. self.dataSourcesArray?.insert(wordType, at: 0)
  401. }
  402. }
  403. func updateFormatView() {
  404. var isShowAdvanced = false
  405. #if VERSION_FREE
  406. #if VERSION_DMG
  407. // 桌机版
  408. isShowAdvanced = true
  409. if IAPProductsManager.default().isAvailableAllFunction() {
  410. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  411. isShowAdvanced = false
  412. }
  413. }
  414. #else
  415. // 免费版
  416. isShowAdvanced = true
  417. if IAPProductsManager.default().isAvailableAllFunction() {
  418. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  419. isShowAdvanced = false
  420. }
  421. }
  422. #endif
  423. #else
  424. isShowAdvanced = true
  425. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  426. isShowAdvanced = false
  427. }
  428. #endif
  429. self.formatSelectButton.removeAllItems()
  430. for i in 0..<self.dataSourcesArray!.count {
  431. let number = self.dataSourcesArray![i]
  432. let item = NSMenuItem()
  433. item.target = self
  434. if(number == .WordAdvance) {
  435. if isShowAdvanced {
  436. let originStr = " \(NSLocalizedString("Advanced", comment: "")) "
  437. let str = NSLocalizedString("Word (.docx)", comment: "")
  438. let attributedStr = NSMutableAttributedString(string: "\(str) \(originStr)")
  439. attributedStr.addAttribute(NSAttributedString.Key.backgroundColor, value: NSColor(red: 245.0/255.0, green: 142.0/255.0, blue: 38.0/255.0, alpha: 1.0), range: NSMakeRange(str.count + 2, originStr.count))
  440. attributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: NSColor.white, range: NSMakeRange(str.count + 2, originStr.count))
  441. item.attributedTitle = attributedStr
  442. } else {
  443. item.title = "Word (.docx)"
  444. }
  445. item.tag = KMConvertWithPDFType.WordAdvance.rawValue
  446. } else if(number == .WordStandard) {
  447. if isShowAdvanced {
  448. let originStr = " \(KMLocalizedString("Standard", nil)) "
  449. let str = KMLocalizedString("Word (.docx)", nil)
  450. let attributedStr = NSMutableAttributedString(string: "\(str) \(originStr)")
  451. attributedStr.addAttribute(NSAttributedString.Key.backgroundColor, value: NSColor(red: 40/255.0, green: 193.0/255.0, blue: 107.0/255.0, alpha: 1.0), range: NSMakeRange(str.count + 2, originStr.count))
  452. attributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: NSColor.white, range: NSMakeRange(str.count + 2, originStr.count))
  453. item.attributedTitle = attributedStr
  454. } else {
  455. item.title = "Word (.docx)"
  456. }
  457. item.tag = KMConvertWithPDFType.WordStandard.rawValue
  458. } else if(number == .Excel) {
  459. item.title = "Excel (.xlsx)"
  460. item.tag = KMConvertWithPDFType.Excel.rawValue
  461. } else if(number == .PowerPoint) {
  462. item.title = "PowerPoint (.pptx)"
  463. item.tag = KMConvertWithPDFType.PowerPoint.rawValue
  464. } else if(number == .RTF) {
  465. item.title = "RTF (.rtf)"
  466. item.tag = KMConvertWithPDFType.RTF.rawValue
  467. } else if(number == .CSV) {
  468. item.title = "CSV (.csv)"
  469. item.tag = KMConvertWithPDFType.CSV.rawValue
  470. } else if(number == .HTML) {
  471. item.title = "Html (.html)"
  472. item.tag = KMConvertWithPDFType.HTML.rawValue
  473. } else if(number == .Text) {
  474. item.title = "Text (.txt)"
  475. item.tag = KMConvertWithPDFType.Text.rawValue
  476. } else if(number == .JPEG) {
  477. item.title = "JPEG (.jpeg)"
  478. item.tag = KMConvertWithPDFType.JPEG.rawValue
  479. } else if(number == .JPG) {
  480. item.title = "JPG (.jpg)"
  481. item.tag = KMConvertWithPDFType.JPG.rawValue
  482. } else if(number == .PNG) {
  483. item.title = "PNG (.png)"
  484. item.tag = KMConvertWithPDFType.PNG.rawValue
  485. } else if(number == .GIF) {
  486. item.title = "GIF (.gif)"
  487. item.tag = KMConvertWithPDFType.GIF.rawValue
  488. } else if(number == .TIFF) {
  489. item.title = "TIFF (.tiff)"
  490. item.tag = KMConvertWithPDFType.TIFF.rawValue
  491. } else if(number == .TGA) {
  492. item.title = "TGA (.tga)"
  493. item.tag = KMConvertWithPDFType.TGA.rawValue
  494. } else if(number == .BMP) {
  495. item.title = "BMP (.bmp)"
  496. item.tag = KMConvertWithPDFType.BMP.rawValue
  497. } else if(number == .JPEG2000) {
  498. item.title = "JPEG-2000 (.jp2)"
  499. item.tag = KMConvertWithPDFType.JPEG2000.rawValue
  500. } else if(number == .Json) {
  501. item.title = "Json(.json)"
  502. item.tag = KMConvertWithPDFType.Json.rawValue
  503. }
  504. self.formatSelectButton.menu?.addItem(item)
  505. }
  506. }
  507. @IBAction func buttonClicked_Convert(_ sender: NSButton) {
  508. if !self.haveFiles { return }
  509. self.view.window?.makeFirstResponder(nil)
  510. for i in 0..<self.files!.count {
  511. let file = self.files?[i]
  512. // file?.excelParameter.allInOneSheet = true // 看代码是没有用
  513. file?.advanceWordParameter.isRetainLayout = (self.wordButtonTwo.state == .on) ? true : false
  514. // file?.excelParameter.isExtreactTabel = (self.extractButton.state == .on) ? true : false // 看代码是没有用
  515. file?.CSVParameter.isExtreactTabel = (self.csvExtractButton.state == .on) ? true : false
  516. file?.excelParameter.excelContentOption = self.excelContentOption
  517. file?.excelParameter.excelWorksheetOption = self.excelWorksheetOption
  518. file?.JSONParameter.isExtreactTabel = self.wordButtonTwo.state == .on
  519. var isAllowOCR = self.ocrBox.isHidden == false
  520. if isAllowOCR {
  521. isAllowOCR = self.ocrButton.state == .on
  522. }
  523. file?.isAllowOCR = isAllowOCR
  524. file?.ocrLanguage = isAllowOCR ? self.ocrLanguage_ : .chinese
  525. }
  526. if sender.tag == 1 {
  527. self.beginBatchOperation()
  528. } else {
  529. self.cancelBatchOperation()
  530. }
  531. }
  532. @IBAction func moreButtonAction(_ sender: NSButton) {
  533. self.moreConvertInfoAction()
  534. }
  535. @IBAction func layoutButtonAction(_ sender: Any) {
  536. }
  537. @IBAction func buttonClicked_Cancel(_ sender: Any) {
  538. let basePath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? ""
  539. let newPath = (basePath as NSString).appendingPathComponent(Bundle.main.bundleIdentifier ?? "")
  540. let filePath = newPath.stringByAppendingPathComponent("convert.pdf")
  541. if FileManager.default.fileExists(atPath: filePath) {
  542. try? FileManager.default.removeItem(atPath: filePath)
  543. }
  544. }
  545. func transform(withString string: String) {
  546. if string == NSLocalizedString("Create Sheet for each Table", comment: "") {
  547. self.excelWorksheetOption = .forEachTable
  548. } else if string == NSLocalizedString("Create Sheet for each Page", comment: "") {
  549. self.excelWorksheetOption = .forEachPage
  550. } else if string == NSLocalizedString("Create single Sheet for File", comment: "") {
  551. self.excelWorksheetOption = .forTheDocument
  552. }
  553. }
  554. @IBAction func buttonClicked_excelStyle(_ sender: NSButton) {
  555. if sender == onlyTextBtn {
  556. onlyTextBtn.state = NSControl.StateValue.on
  557. onlyTableBtn.state = NSControl.StateValue.off
  558. allContentBtn.state = NSControl.StateValue.off
  559. tableMenu.isEnabled = false
  560. allContentMenu.isEnabled = false
  561. excelContentOption = .onlyText
  562. excelWorksheetOption = .forEachTable
  563. } else if sender == onlyTableBtn {
  564. onlyTableBtn.state = NSControl.StateValue.on
  565. onlyTextBtn.state = NSControl.StateValue.off
  566. allContentBtn.state = NSControl.StateValue.off
  567. tableMenu.isEnabled = true
  568. allContentMenu.isEnabled = false
  569. excelContentOption = .onlyTable
  570. transform(withString: tableMenu.selectedItem?.title ?? "")
  571. } else if sender == allContentBtn {
  572. allContentBtn.state = NSControl.StateValue.on
  573. onlyTextBtn.state = NSControl.StateValue.off
  574. onlyTableBtn.state = NSControl.StateValue.off
  575. tableMenu.isEnabled = false
  576. allContentMenu.isEnabled = true
  577. excelContentOption = .allContent
  578. transform(withString: allContentMenu.selectedItem?.title ?? "")
  579. }
  580. }
  581. @IBAction func buttonClicked_tableMenu(_ sender: NSPopUpButton) {
  582. transform(withString: self.tableMenu.selectedItem!.title)
  583. }
  584. @IBAction func buttonClickec_allContentMenu(_ sender: NSButton) {
  585. transform(withString: self.allContentMenu.selectedItem!.title)
  586. }
  587. func intConvertType(num: Int) -> KMConvertWithPDFType {
  588. var type: KMConvertWithPDFType?
  589. switch num {
  590. case 0:
  591. type = .WordAdvance
  592. case 1:
  593. type = .WordStandard
  594. case 2:
  595. type = .Excel
  596. case 3:
  597. type = .PowerPoint
  598. case 4:
  599. type = .RTF
  600. case 5:
  601. type = .CSV
  602. case 6:
  603. type = .HTML
  604. case 7:
  605. type = .Text
  606. case 8:
  607. type = .JPEG
  608. case 9:
  609. type = .JPG
  610. case 10:
  611. type = .PNG
  612. case 11:
  613. type = .GIF
  614. case 12:
  615. type = .TIFF
  616. case 13:
  617. type = .TGA
  618. case 14:
  619. type = .BMP
  620. case 15:
  621. type = .JPEG2000
  622. case 16:
  623. type = .Json
  624. default:
  625. type = .WordAdvance
  626. }
  627. return type!
  628. }
  629. func moreConvertInfoAction() {
  630. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() == false {
  631. #if VERSION_DMG
  632. if IAPProductsManager.default().isAvailableAllFunction() && IAPProductsManager.default().isAvailableAdvancedPDFToOffice() == false {
  633. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  634. limitWC.continueBlock = { windowController in
  635. }
  636. limitWC.window?.center()
  637. limitWC.showWindow(nil)
  638. }else{
  639. let winC = KMPurchaseCompareWindowController.sharedInstance()
  640. if let viewC = self.view.window?.contentViewController, viewC.kEventTag == 1 {
  641. winC?.kEventName = "Onbrd_ConvertPDF_BuyNow"
  642. } else {
  643. if convertType == .WordStandard || convertType == .WordAdvance {
  644. winC?.kEventName = "Onbrd_PDFtoWord_BuyNow"
  645. } else if convertType == .Excel {
  646. winC?.kEventName = "Onbrd_PDFtoExcel_BuyNow"
  647. } else if convertType == .PowerPoint {
  648. winC?.kEventName = "Onbrd_PDFtoPPT_BuyNow"
  649. }
  650. }
  651. winC?.showWindow(nil)
  652. }
  653. #else
  654. if IAPProductsManager.default().isAvailableAllFunction() {
  655. self.convertActionVC()
  656. } else {
  657. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  658. }
  659. #endif
  660. }
  661. }
  662. @IBAction func buttonClicked_FormatSelect(_ sender: NSButton) {
  663. guard let item = self.formatSelectButton.selectedItem else { return }
  664. convertType = KMConvertWithPDFType(rawValue: item.tag) ?? .WordAdvance
  665. updateBottomView()
  666. }
  667. @IBAction func comboxClicked_OcrLanguage(_ sender: AnyObject?) {
  668. switch (ocrLanguageBox.indexOfSelectedItem) {
  669. case 0: //chinese
  670. self.ocrLanguage_ = .chinese
  671. break
  672. case 1: //chinese zht
  673. self.ocrLanguage_ = .chineseTraditional
  674. break
  675. case 2: //English
  676. self.ocrLanguage_ = .english
  677. break
  678. case 3:
  679. //Japanese
  680. self.ocrLanguage_ = .japanese
  681. break
  682. case 4:
  683. //Kroean
  684. self.ocrLanguage_ = .korean
  685. break
  686. case 5:
  687. //Serbian
  688. self.ocrLanguage_ = .serbian
  689. break
  690. case 6:
  691. //Occitan
  692. self.ocrLanguage_ = .occitan
  693. break
  694. case 7:
  695. //Danish
  696. self.ocrLanguage_ = .danish
  697. break
  698. case 8:
  699. //German
  700. self.ocrLanguage_ = .german
  701. break
  702. case 9:
  703. //French
  704. self.ocrLanguage_ = .french
  705. break
  706. case 10:
  707. //Italian
  708. self.ocrLanguage_ = .italian
  709. break
  710. case 11:
  711. //Spanish
  712. self.ocrLanguage_ = .spanish
  713. break
  714. case 12:
  715. //Portuguese
  716. self.ocrLanguage_ = .portuguese
  717. break
  718. case 13:
  719. //Maori
  720. self.ocrLanguage_ = .maori
  721. break
  722. case 14:
  723. //Malay
  724. self.ocrLanguage_ = .malay
  725. break
  726. case 15:
  727. //Malay
  728. self.ocrLanguage_ = .maltese
  729. break
  730. case 16:
  731. //Dutch
  732. self.ocrLanguage_ = .dutch
  733. break
  734. case 17:
  735. //Norwegian
  736. self.ocrLanguage_ = .norwegian
  737. break
  738. case 18:
  739. //Polish
  740. self.ocrLanguage_ = .polish
  741. break
  742. case 19:
  743. //Romanian
  744. self.ocrLanguage_ = .romanian
  745. break
  746. case 20:
  747. //Slovak
  748. self.ocrLanguage_ = .slovak
  749. break
  750. case 21:
  751. //Slovenian
  752. self.ocrLanguage_ = .slovenian
  753. break
  754. case 22:
  755. //Albanian
  756. self.ocrLanguage_ = .albanian
  757. break
  758. case 23:
  759. //Swedish
  760. self.ocrLanguage_ = .swedish
  761. break
  762. case 24:
  763. //Swahili
  764. self.ocrLanguage_ = .swahili
  765. break
  766. case 25:
  767. //Tagalog
  768. self.ocrLanguage_ = .tagalog
  769. break
  770. case 26:
  771. //Turish
  772. self.ocrLanguage_ = .turish
  773. break
  774. case 27:
  775. //Uzbek
  776. self.ocrLanguage_ = .uzbek
  777. break
  778. case 28:
  779. //Vietnamese
  780. self.ocrLanguage_ = .vietnamese
  781. break
  782. case 29:
  783. //Afrikaans
  784. self.ocrLanguage_ = .afrikaans
  785. break
  786. case 30:
  787. //Azerbaijani
  788. self.ocrLanguage_ = .azerbaijani
  789. break
  790. case 31:
  791. //Bosnian
  792. self.ocrLanguage_ = .bosnian
  793. break
  794. case 32:
  795. //Czech
  796. self.ocrLanguage_ = .czech
  797. break
  798. case 33: //Welsh
  799. self.ocrLanguage_ = .welsh
  800. break
  801. case 34: //Estonian
  802. self.ocrLanguage_ = .estonian
  803. break
  804. case 35: //Irish
  805. self.ocrLanguage_ = .irish
  806. break
  807. case 36: //Croatian
  808. self.ocrLanguage_ = .croatian
  809. break
  810. case 37: //Hungarian
  811. self.ocrLanguage_ = .hungarian
  812. break
  813. case 38: //Indonesian
  814. self.ocrLanguage_ = .indonesian
  815. break
  816. case 39: //Icelandic
  817. self.ocrLanguage_ = .icelandic
  818. break
  819. case 40: //Kurdish
  820. self.ocrLanguage_ = .kurdish
  821. break
  822. case 41: //Lithuanian
  823. self.ocrLanguage_ = .lithuanian
  824. break
  825. case 42: //Latvian
  826. self.ocrLanguage_ = .latvian
  827. break
  828. case 43: //Marathi
  829. self.ocrLanguage_ = .marathi
  830. break
  831. case 44: //Nepali
  832. self.ocrLanguage_ = .nepali
  833. break
  834. case 45: //Latvia
  835. self.ocrLanguage_ = .latvian
  836. break
  837. case 46: //Bihari
  838. self.ocrLanguage_ = .bihari
  839. break
  840. case 47: //Maithili
  841. self.ocrLanguage_ = .maithili
  842. break
  843. case 48: //Angika
  844. self.ocrLanguage_ = .angika
  845. break
  846. case 49: //Bhojpuri
  847. self.ocrLanguage_ = .bhojpuri
  848. break
  849. case 50: //Magahi
  850. self.ocrLanguage_ = .magahi
  851. break
  852. case 51: //Nagpur
  853. self.ocrLanguage_ = .nagpur
  854. break
  855. case 52: //Newari
  856. self.ocrLanguage_ = .newari
  857. break
  858. case 53: //GoanKonkani
  859. self.ocrLanguage_ = .goanKonkani
  860. break
  861. case 54: //SaudiArabia
  862. self.ocrLanguage_ = .saudiArabia
  863. break
  864. default:
  865. self.ocrLanguage_ = .english
  866. break
  867. }
  868. }
  869. @IBAction func comboxClicked_SupoortOCR(_ sender: AnyObject?) {
  870. if(ocrButton.state == .on) {
  871. ocrLabel.textColor = KMAppearance.titleColor()
  872. ocrLanguageBox.isEnabled = true
  873. } else {
  874. ocrLabel.textColor = KMAppearance.Layout.h2Color()
  875. ocrLanguageBox.isEnabled = false
  876. }
  877. }
  878. }