KMBatchOperateConvertViewController.swift 38 KB

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