KMBatchOperateConvertViewController.swift 38 KB

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