KMBatchOperateConvertViewController.swift 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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. 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 = NSLocalizedString("Format:", comment: "")
  294. ocrBox.title = NSLocalizedString("OCR Settings", comment: "")
  295. ocrBox.titleFont = .SFProTextBoldFont(14.0)
  296. formatLabel.font = .SFProTextBoldFont(14.0)
  297. headerView.convertHeaderClickedCallBack = { [weak self] in
  298. self?.moreConvertInfoAction()
  299. }
  300. self.ocrButton.title = NSLocalizedString("Recognize text", comment: "")
  301. self.ocrLabel.stringValue = NSLocalizedString("Language Option", comment: "") + ":"
  302. ocrLanguageBox.selectItem(at: 2)
  303. ocrLabel.textColor = KMAppearance.Layout.h2Color()
  304. ocrLanguageBox.isEnabled = false
  305. ocrButton.state = .off
  306. self.updateViewColor()
  307. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: Notification.Name(rawValue: "KMBatchFilesCountNotification"), object: nil)
  308. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductPurchasedNotification(notification:)), name: NSNotification.Name("KMIAPProductPurchasedNotification"), object: nil)
  309. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductRestoreFinishedNotification(notification:)), name: NSNotification.Name("KMIAPProductRestoreFinishedNotification"), object: nil)
  310. // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  311. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  312. NotificationCenter.default.addObserver(self, selector: #selector(deviceActivateStatusChanged(notification:)), name: NSNotification.Name("kDeviceActivateNotification"), object: nil)
  313. }
  314. @objc func batchFilesCountNotification(notification: NSNotification) {
  315. let arr: Array? = notification.object as? [KMBatchOperateFile]
  316. self.files? = arr ?? []
  317. if files!.count > 0 {
  318. self.convertButton.setTitleColor(NSColor.white)
  319. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  320. self.haveFiles = true
  321. } else {
  322. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  323. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  324. self.haveFiles = false
  325. }
  326. }
  327. @objc func themeChanged(notification: NSNotification) {
  328. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  329. self.updateViewColor()
  330. }
  331. }
  332. @objc func IAPProductRestoreFinishedNotification(notification: NSNotification) {
  333. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  334. self.updateBottomView()
  335. self.prepareData()
  336. self.updateFormatView()
  337. }
  338. }
  339. @objc func IAPProductPurchasedNotification(notification: NSNotification) {
  340. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  341. self.updateBottomView()
  342. self.prepareData()
  343. self.updateFormatView()
  344. }
  345. }
  346. @objc func deviceActivateStatusChanged(notification: NSNotification) {
  347. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  348. self.updateBottomView()
  349. self.prepareData()
  350. self.updateFormatView()
  351. }
  352. }
  353. func updateViewColor() {
  354. self.view.wantsLayer = true
  355. if KMAppearance.isDarkMode() {
  356. self.view.layer?.backgroundColor = NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor
  357. } else {
  358. self.view.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  359. }
  360. }
  361. func convertActionVC() {
  362. var vc: KMToolCompareWindowController? = nil
  363. if(.WordStandard == self.convertType ||
  364. .WordAdvance == self.convertType) {
  365. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  366. } else if (.Excel == self.convertType) {
  367. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 2)
  368. } else if (.PowerPoint == self.convertType) {
  369. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 3)
  370. } else if (self.convertType == .JPG || self.convertType == .PNG || self.convertType == .GIF || self.convertType == .TIFF || self.convertType == .TGA || self.convertType == .BMP){
  371. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 4)
  372. } else {
  373. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 0)
  374. }
  375. vc?.showWindow(nil)
  376. }
  377. func prepareData() {
  378. let arr: [KMConvertWithPDFType] = [.Excel, .PowerPoint, .RTF, .CSV, .HTML, .Text, .JPEG, .JPG, .PNG, .GIF, .TIFF, .TGA, .BMP, .JPEG2000, .Json]
  379. self.dataSourcesArray = arr
  380. var needShowAdvance = true
  381. var needShowDefault = true
  382. #if VERSION_FREE
  383. // 桌机版
  384. if IAPProductsManager.default().isAvailableAllFunction() {
  385. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  386. needShowAdvance = true
  387. needShowDefault = false
  388. }
  389. }
  390. #else
  391. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  392. needShowAdvance = true
  393. needShowDefault = false
  394. }
  395. #endif
  396. if needShowDefault {
  397. var wordType: KMConvertWithPDFType = .WordStandard
  398. self.dataSourcesArray?.insert(wordType, at: 0)
  399. }
  400. if needShowAdvance {
  401. var wordType: KMConvertWithPDFType = .WordAdvance
  402. self.dataSourcesArray?.insert(wordType, at: 0)
  403. }
  404. }
  405. func updateFormatView() {
  406. var isShowAdvanced = false
  407. #if VERSION_FREE
  408. #if VERSION_DMG
  409. // 桌机版
  410. isShowAdvanced = true
  411. if IAPProductsManager.default().isAvailableAllFunction() {
  412. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  413. isShowAdvanced = false
  414. }
  415. }
  416. #else
  417. // 免费版
  418. isShowAdvanced = true
  419. if IAPProductsManager.default().isAvailableAllFunction() {
  420. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  421. isShowAdvanced = false
  422. }
  423. }
  424. #endif
  425. #else
  426. isShowAdvanced = true
  427. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  428. isShowAdvanced = false
  429. }
  430. #endif
  431. self.formatSelectButton.removeAllItems()
  432. for i in 0..<self.dataSourcesArray!.count {
  433. let number = self.dataSourcesArray![i]
  434. let item = NSMenuItem()
  435. item.target = self
  436. if(number == .WordAdvance) {
  437. if isShowAdvanced {
  438. let originStr = " \(NSLocalizedString("Advanced", comment: "")) "
  439. let str = NSLocalizedString("Word (.docx)", comment: "")
  440. let attributedStr = NSMutableAttributedString(string: "\(str) \(originStr)")
  441. 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))
  442. attributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: NSColor.white, range: NSMakeRange(str.count + 2, originStr.count))
  443. item.attributedTitle = attributedStr
  444. } else {
  445. item.title = "Word (.docx)"
  446. }
  447. item.tag = KMConvertWithPDFType.WordAdvance.rawValue
  448. } else if(number == .WordStandard) {
  449. if isShowAdvanced {
  450. let originStr = " \(NSLocalizedString("Standard", comment: "")) "
  451. let str = NSLocalizedString("Word (.docx)", comment: "")
  452. let attributedStr = NSMutableAttributedString(string: "\(str) \(originStr)")
  453. 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))
  454. attributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: NSColor.white, range: NSMakeRange(str.count + 2, originStr.count))
  455. item.attributedTitle = attributedStr
  456. } else {
  457. item.title = "Word (.docx)"
  458. }
  459. item.tag = KMConvertWithPDFType.WordStandard.rawValue
  460. } else if(number == .Excel) {
  461. item.title = "Excel (.xlsx)"
  462. item.tag = KMConvertWithPDFType.Excel.rawValue
  463. } else if(number == .PowerPoint) {
  464. item.title = "PowerPoint (.pptx)"
  465. item.tag = KMConvertWithPDFType.PowerPoint.rawValue
  466. } else if(number == .RTF) {
  467. item.title = "RTF (.rtf)"
  468. item.tag = KMConvertWithPDFType.RTF.rawValue
  469. } else if(number == .CSV) {
  470. item.title = "CSV (.csv)"
  471. item.tag = KMConvertWithPDFType.CSV.rawValue
  472. } else if(number == .HTML) {
  473. item.title = "Html (.html)"
  474. item.tag = KMConvertWithPDFType.HTML.rawValue
  475. } else if(number == .Text) {
  476. item.title = "Text (.txt)"
  477. item.tag = KMConvertWithPDFType.Text.rawValue
  478. } else if(number == .JPEG) {
  479. item.title = "JPEG (.jpeg)"
  480. item.tag = KMConvertWithPDFType.JPEG.rawValue
  481. } else if(number == .JPG) {
  482. item.title = "JPG (.jpg)"
  483. item.tag = KMConvertWithPDFType.JPG.rawValue
  484. } else if(number == .PNG) {
  485. item.title = "PNG (.png)"
  486. item.tag = KMConvertWithPDFType.PNG.rawValue
  487. } else if(number == .GIF) {
  488. item.title = "GIF (.gif)"
  489. item.tag = KMConvertWithPDFType.GIF.rawValue
  490. } else if(number == .TIFF) {
  491. item.title = "TIFF (.tiff)"
  492. item.tag = KMConvertWithPDFType.TIFF.rawValue
  493. } else if(number == .TGA) {
  494. item.title = "TGA (.tga)"
  495. item.tag = KMConvertWithPDFType.TGA.rawValue
  496. } else if(number == .BMP) {
  497. item.title = "BMP (.bmp)"
  498. item.tag = KMConvertWithPDFType.BMP.rawValue
  499. } else if(number == .JPEG2000) {
  500. item.title = "JPEG-2000 (.jp2)"
  501. item.tag = KMConvertWithPDFType.JPEG2000.rawValue
  502. } else if(number == .Json) {
  503. item.title = "Json(.json)"
  504. item.tag = KMConvertWithPDFType.Json.rawValue
  505. }
  506. self.formatSelectButton.menu?.addItem(item)
  507. }
  508. }
  509. @IBAction func buttonClicked_Convert(_ sender: NSButton) {
  510. if !self.haveFiles { return }
  511. self.view.window?.makeFirstResponder(nil)
  512. for i in 0..<self.files!.count {
  513. let file = self.files?[i]
  514. // file?.excelParameter.allInOneSheet = true // 看代码是没有用
  515. file?.advanceWordParameter.isRetainLayout = (self.wordButtonTwo.state == .on) ? true : false
  516. // file?.excelParameter.isExtreactTabel = (self.extractButton.state == .on) ? true : false // 看代码是没有用
  517. file?.CSVParameter.isExtreactTabel = (self.csvExtractButton.state == .on) ? true : false
  518. file?.excelParameter.excelContentOption = self.excelContentOption
  519. file?.excelParameter.excelWorksheetOption = self.excelWorksheetOption
  520. file?.JSONParameter.isExtreactTabel = self.wordButtonTwo.state == .on
  521. var isAllowOCR = self.ocrBox.isHidden == false
  522. if isAllowOCR {
  523. isAllowOCR = self.ocrButton.state == .on
  524. }
  525. file?.isAllowOCR = isAllowOCR
  526. file?.ocrLanguage = isAllowOCR ? self.ocrLanguage_ : .chinese
  527. }
  528. if sender.tag == 1 {
  529. self.beginBatchOperation()
  530. } else {
  531. self.cancelBatchOperation()
  532. }
  533. }
  534. @IBAction func moreButtonAction(_ sender: NSButton) {
  535. self.moreConvertInfoAction()
  536. }
  537. @IBAction func layoutButtonAction(_ sender: Any) {
  538. }
  539. @IBAction func buttonClicked_Cancel(_ sender: Any) {
  540. let basePath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? ""
  541. let newPath = (basePath as NSString).appendingPathComponent(Bundle.main.bundleIdentifier ?? "")
  542. let filePath = newPath.stringByAppendingPathComponent("convert.pdf")
  543. if FileManager.default.fileExists(atPath: filePath) {
  544. try? FileManager.default.removeItem(atPath: filePath)
  545. }
  546. }
  547. func transform(withString string: String) {
  548. if string == NSLocalizedString("Create Sheet for each Table", comment: "") {
  549. self.excelWorksheetOption = .forEachTable
  550. } else if string == NSLocalizedString("Create Sheet for each Page", comment: "") {
  551. self.excelWorksheetOption = .forEachPage
  552. } else if string == NSLocalizedString("Create single Sheet for File", comment: "") {
  553. self.excelWorksheetOption = .forTheDocument
  554. }
  555. }
  556. @IBAction func buttonClicked_excelStyle(_ sender: NSButton) {
  557. if sender == onlyTextBtn {
  558. onlyTextBtn.state = NSControl.StateValue.on
  559. onlyTableBtn.state = NSControl.StateValue.off
  560. allContentBtn.state = NSControl.StateValue.off
  561. tableMenu.isEnabled = false
  562. allContentMenu.isEnabled = false
  563. excelContentOption = .onlyText
  564. excelWorksheetOption = .forEachTable
  565. } else if sender == onlyTableBtn {
  566. onlyTableBtn.state = NSControl.StateValue.on
  567. onlyTextBtn.state = NSControl.StateValue.off
  568. allContentBtn.state = NSControl.StateValue.off
  569. tableMenu.isEnabled = true
  570. allContentMenu.isEnabled = false
  571. excelContentOption = .onlyTable
  572. transform(withString: tableMenu.selectedItem?.title ?? "")
  573. } else if sender == allContentBtn {
  574. allContentBtn.state = NSControl.StateValue.on
  575. onlyTextBtn.state = NSControl.StateValue.off
  576. onlyTableBtn.state = NSControl.StateValue.off
  577. tableMenu.isEnabled = false
  578. allContentMenu.isEnabled = true
  579. excelContentOption = .allContent
  580. transform(withString: allContentMenu.selectedItem?.title ?? "")
  581. }
  582. }
  583. @IBAction func buttonClicked_tableMenu(_ sender: NSPopUpButton) {
  584. transform(withString: self.tableMenu.selectedItem!.title)
  585. }
  586. @IBAction func buttonClickec_allContentMenu(_ sender: NSButton) {
  587. transform(withString: self.allContentMenu.selectedItem!.title)
  588. }
  589. func intConvertType(num: Int) -> KMConvertWithPDFType {
  590. var type: KMConvertWithPDFType?
  591. switch num {
  592. case 0:
  593. type = .WordAdvance
  594. case 1:
  595. type = .WordStandard
  596. case 2:
  597. type = .Excel
  598. case 3:
  599. type = .PowerPoint
  600. case 4:
  601. type = .RTF
  602. case 5:
  603. type = .CSV
  604. case 6:
  605. type = .HTML
  606. case 7:
  607. type = .Text
  608. case 8:
  609. type = .JPEG
  610. case 9:
  611. type = .JPG
  612. case 10:
  613. type = .PNG
  614. case 11:
  615. type = .GIF
  616. case 12:
  617. type = .TIFF
  618. case 13:
  619. type = .TGA
  620. case 14:
  621. type = .BMP
  622. case 15:
  623. type = .JPEG2000
  624. case 16:
  625. type = .Json
  626. default:
  627. type = .WordAdvance
  628. }
  629. return type!
  630. }
  631. func moreConvertInfoAction() {
  632. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() == false {
  633. #if VERSION_DMG
  634. if IAPProductsManager.default().isAvailableAllFunction() && IAPProductsManager.default().isAvailableAdvancedPDFToOffice() == false {
  635. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  636. limitWC.continueBlock = { windowController in
  637. }
  638. limitWC.window?.center()
  639. limitWC.showWindow(nil)
  640. }else{
  641. let winC = KMPurchaseCompareWindowController.sharedInstance()
  642. if let viewC = self.view.window?.contentViewController, viewC.kEventTag == 1 {
  643. winC?.kEventName = "Onbrd_ConvertPDF_BuyNow"
  644. } else {
  645. if convertType == .WordStandard || convertType == .WordAdvance {
  646. winC?.kEventName = "Onbrd_PDFtoWord_BuyNow"
  647. } else if convertType == .Excel {
  648. winC?.kEventName = "Onbrd_PDFtoExcel_BuyNow"
  649. } else if convertType == .PowerPoint {
  650. winC?.kEventName = "Onbrd_PDFtoPPT_BuyNow"
  651. }
  652. }
  653. winC?.showWindow(nil)
  654. }
  655. #else
  656. if IAPProductsManager.default().isAvailableAllFunction() {
  657. self.convertActionVC()
  658. } else {
  659. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  660. }
  661. #endif
  662. }
  663. }
  664. @IBAction func buttonClicked_FormatSelect(_ sender: NSButton) {
  665. guard let item = self.formatSelectButton.selectedItem else { return }
  666. convertType = KMConvertWithPDFType(rawValue: item.tag) ?? .WordAdvance
  667. updateBottomView()
  668. }
  669. @IBAction func comboxClicked_OcrLanguage(_ sender: AnyObject?) {
  670. switch (ocrLanguageBox.indexOfSelectedItem) {
  671. case 0: //chinese
  672. self.ocrLanguage_ = .chinese
  673. break
  674. case 1: //chinese zht
  675. self.ocrLanguage_ = .chineseTraditional
  676. break
  677. case 2: //English
  678. self.ocrLanguage_ = .english
  679. break
  680. case 3:
  681. //Japanese
  682. self.ocrLanguage_ = .japanese
  683. break
  684. case 4:
  685. //Kroean
  686. self.ocrLanguage_ = .korean
  687. break
  688. case 5:
  689. //Serbian
  690. self.ocrLanguage_ = .serbian
  691. break
  692. case 6:
  693. //Occitan
  694. self.ocrLanguage_ = .occitan
  695. break
  696. case 7:
  697. //Danish
  698. self.ocrLanguage_ = .danish
  699. break
  700. case 8:
  701. //German
  702. self.ocrLanguage_ = .german
  703. break
  704. case 9:
  705. //French
  706. self.ocrLanguage_ = .french
  707. break
  708. case 10:
  709. //Italian
  710. self.ocrLanguage_ = .italian
  711. break
  712. case 11:
  713. //Spanish
  714. self.ocrLanguage_ = .spanish
  715. break
  716. case 12:
  717. //Portuguese
  718. self.ocrLanguage_ = .portuguese
  719. break
  720. case 13:
  721. //Maori
  722. self.ocrLanguage_ = .maori
  723. break
  724. case 14:
  725. //Malay
  726. self.ocrLanguage_ = .malay
  727. break
  728. case 15:
  729. //Malay
  730. self.ocrLanguage_ = .maltese
  731. break
  732. case 16:
  733. //Dutch
  734. self.ocrLanguage_ = .dutch
  735. break
  736. case 17:
  737. //Norwegian
  738. self.ocrLanguage_ = .norwegian
  739. break
  740. case 18:
  741. //Polish
  742. self.ocrLanguage_ = .polish
  743. break
  744. case 19:
  745. //Romanian
  746. self.ocrLanguage_ = .romanian
  747. break
  748. case 20:
  749. //Slovak
  750. self.ocrLanguage_ = .slovak
  751. break
  752. case 21:
  753. //Slovenian
  754. self.ocrLanguage_ = .slovenian
  755. break
  756. case 22:
  757. //Albanian
  758. self.ocrLanguage_ = .albanian
  759. break
  760. case 23:
  761. //Swedish
  762. self.ocrLanguage_ = .swedish
  763. break
  764. case 24:
  765. //Swahili
  766. self.ocrLanguage_ = .swahili
  767. break
  768. case 25:
  769. //Tagalog
  770. self.ocrLanguage_ = .tagalog
  771. break
  772. case 26:
  773. //Turish
  774. self.ocrLanguage_ = .turish
  775. break
  776. case 27:
  777. //Uzbek
  778. self.ocrLanguage_ = .uzbek
  779. break
  780. case 28:
  781. //Vietnamese
  782. self.ocrLanguage_ = .vietnamese
  783. break
  784. case 29:
  785. //Afrikaans
  786. self.ocrLanguage_ = .afrikaans
  787. break
  788. case 30:
  789. //Azerbaijani
  790. self.ocrLanguage_ = .azerbaijani
  791. break
  792. case 31:
  793. //Bosnian
  794. self.ocrLanguage_ = .bosnian
  795. break
  796. case 32:
  797. //Czech
  798. self.ocrLanguage_ = .czech
  799. break
  800. case 33: //Welsh
  801. self.ocrLanguage_ = .welsh
  802. break
  803. case 34: //Estonian
  804. self.ocrLanguage_ = .estonian
  805. break
  806. case 35: //Irish
  807. self.ocrLanguage_ = .irish
  808. break
  809. case 36: //Croatian
  810. self.ocrLanguage_ = .croatian
  811. break
  812. case 37: //Hungarian
  813. self.ocrLanguage_ = .hungarian
  814. break
  815. case 38: //Indonesian
  816. self.ocrLanguage_ = .indonesian
  817. break
  818. case 39: //Icelandic
  819. self.ocrLanguage_ = .icelandic
  820. break
  821. case 40: //Kurdish
  822. self.ocrLanguage_ = .kurdish
  823. break
  824. case 41: //Lithuanian
  825. self.ocrLanguage_ = .lithuanian
  826. break
  827. case 42: //Latvian
  828. self.ocrLanguage_ = .latvian
  829. break
  830. case 43: //Marathi
  831. self.ocrLanguage_ = .marathi
  832. break
  833. case 44: //Nepali
  834. self.ocrLanguage_ = .nepali
  835. break
  836. case 45: //Latvia
  837. self.ocrLanguage_ = .latvian
  838. break
  839. case 46: //Bihari
  840. self.ocrLanguage_ = .bihari
  841. break
  842. case 47: //Maithili
  843. self.ocrLanguage_ = .maithili
  844. break
  845. case 48: //Angika
  846. self.ocrLanguage_ = .angika
  847. break
  848. case 49: //Bhojpuri
  849. self.ocrLanguage_ = .bhojpuri
  850. break
  851. case 50: //Magahi
  852. self.ocrLanguage_ = .magahi
  853. break
  854. case 51: //Nagpur
  855. self.ocrLanguage_ = .nagpur
  856. break
  857. case 52: //Newari
  858. self.ocrLanguage_ = .newari
  859. break
  860. case 53: //GoanKonkani
  861. self.ocrLanguage_ = .goanKonkani
  862. break
  863. case 54: //SaudiArabia
  864. self.ocrLanguage_ = .saudiArabia
  865. break
  866. default:
  867. self.ocrLanguage_ = .english
  868. break
  869. }
  870. }
  871. @IBAction func comboxClicked_SupoortOCR(_ sender: AnyObject?) {
  872. if(ocrButton.state == .on) {
  873. ocrLabel.textColor = KMAppearance.titleColor()
  874. ocrLanguageBox.isEnabled = true
  875. } else {
  876. ocrLabel.textColor = KMAppearance.Layout.h2Color()
  877. ocrLanguageBox.isEnabled = false
  878. }
  879. }
  880. }