KMConvertWindowController.swift 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. //
  2. // KMConvertWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/11/8.
  6. //
  7. import Cocoa
  8. typealias convertBatchCallBack = () -> Void
  9. let kPreviewMaxWidth = 237.0
  10. let kPreviewMaxHeight = 325.0
  11. let kConvertFileSavePath = (kTempSavePath?.stringByAppendingPathComponent("convert.pdf"))
  12. @objcMembers class KMConvertWindowController: NSWindowController, NSTextFieldDelegate, NSWindowDelegate, NSComboBoxDelegate{
  13. @IBOutlet var prePDFViewHeight: NSLayoutConstraint!
  14. @IBOutlet var prePDFViewWidth: NSLayoutConstraint!
  15. var prePDFView: CPDFView!
  16. @IBOutlet var prePdfBGView: NSView!
  17. @IBOutlet var currentPageIndexTextField: NSTextField!
  18. @IBOutlet var totalPageCountLabel: NSTextField!
  19. @IBOutlet var formatLabel: NSTextField!
  20. @IBOutlet var formatSelectButton: NSPopUpButton!
  21. @IBOutlet var imageDPISelectButton: NSPopUpButton!
  22. @IBOutlet var moreButton: NSButton!
  23. @IBOutlet var buttonBottemOffset: NSLayoutConstraint!
  24. @IBOutlet var buttonTopOffset: NSLayoutConstraint!
  25. @IBOutlet var moreConvertBox: NSBox!
  26. @IBOutlet var moreConvertLabel: NSTextField!
  27. @IBOutlet var pageRangLabel: NSTextField!
  28. @IBOutlet var pageRangeBox: NSComboBox!
  29. @IBOutlet var convertButton: NSButton!
  30. @IBOutlet var cancelButton: NSButton!
  31. @IBOutlet var batchButton: NSButton!
  32. @IBOutlet var upgradeButton: NSButton!
  33. @IBOutlet var separateStypeTopOffset: NSLayoutConstraint!
  34. @IBOutlet var separateButton: NSButton!
  35. @IBOutlet var singleButton: NSButton!
  36. @IBOutlet var separateStypeBox: NSBox!
  37. @IBOutlet var extractTableButton: NSButton!
  38. @IBOutlet var extractTableTopOffset: NSLayoutConstraint!
  39. @IBOutlet var separateStypeLabel: NSTextField!
  40. @IBOutlet var excelStyleBox: NSBox!
  41. @IBOutlet var onlyTextBtn: NSButton!
  42. @IBOutlet var onlyTableBtn: NSButton!
  43. @IBOutlet var allContentBtn: NSButton!
  44. @IBOutlet var tableMenu1: NSMenuItem!
  45. @IBOutlet var tableMenu2: NSMenuItem!
  46. @IBOutlet var tableMenu3: NSMenuItem!
  47. @IBOutlet var allContentMenu2: NSMenuItem!
  48. @IBOutlet var allContentMenu3: NSMenuItem!
  49. @IBOutlet var tableMenu: NSPopUpButton!
  50. @IBOutlet var allContentMenu: NSPopUpButton!
  51. @IBOutlet var ocrBox: NSBox!
  52. @IBOutlet var ocrLabel: NSTextField!
  53. @IBOutlet var ocrButton: NSButton!
  54. @IBOutlet var ocrLanguageBox: NSPopUpButton!
  55. @IBOutlet var ocrTopOffset: NSLayoutConstraint!
  56. var isAllInOneSheet = false
  57. var modalSession: NSApplication.ModalSession?
  58. var pdfDocument: CPDFDocument?
  59. var maskView: KMBookletMaskView?
  60. var convertType: KMConvertType = .Word
  61. var imageDPINumber: Int = 0
  62. var callBackBlock: convertBatchCallBack?
  63. var currentPage: CPDFPage?
  64. var savePath: String = ""
  65. var lockPassword: String = ""
  66. var tempPDFDocument: CPDFDocument?
  67. var excelContentOption: CPDFConvertExcelContentOptions?
  68. var excelWorksheetOption: CPDFConvertExcelWorksheetOptions?
  69. private var fileAttri_: KMFileAttribute?
  70. private var ocrLanguage_: COCRLanguage = .english
  71. deinit {
  72. #if DEBUG
  73. NSLog("====KMConvertWindowController==deinit")
  74. #endif
  75. }
  76. convenience init(documemtV:CPDFDocument, currentPage:CPDFPage, convertT:KMConvertType, handle:@escaping convertBatchCallBack) {
  77. self.init(windowNibName: "KMConvertWindowController")
  78. if (documemtV.documentURL != nil) {
  79. let pageIndex = documemtV.index(for: currentPage)
  80. self.pdfDocument = CPDFDocument(url: documemtV.documentURL)
  81. let isLoc: Bool = self.pdfDocument!.isLocked
  82. if (!documemtV.isLocked && isLoc) {
  83. self.lockPassword = documemtV.password
  84. self.pdfDocument?.unlock(withPassword: self.lockPassword)
  85. }
  86. self.currentPage = self.pdfDocument?.page(at: pageIndex )
  87. } else {
  88. self.pdfDocument = documemtV
  89. self.currentPage = currentPage
  90. }
  91. self.callBackBlock = handle
  92. if convertT.rawValue > 15 || convertT.rawValue < 0 {
  93. self.convertType = .Word
  94. } else {
  95. self.convertType = convertT
  96. }
  97. }
  98. convenience init(localPath:String, handle:@escaping convertBatchCallBack) {
  99. self.init(windowNibName: "KMConvertWindowController")
  100. self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: localPath))
  101. self.callBackBlock = handle
  102. let isLoc: Bool = self.pdfDocument!.isLocked
  103. if (isLoc) {
  104. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
  105. let url = self.pdfDocument?.documentURL
  106. KMBaseWindowController.checkPassword(url: url!, type: .owner) { [weak self] success, resultPassword in
  107. if success {
  108. self?.lockPassword = resultPassword
  109. self?.pdfDocument?.unlock(withPassword: resultPassword)
  110. self?.currentPage = self?.pdfDocument?.page(at: 0)
  111. self?.convertType = .Word
  112. }
  113. }
  114. }
  115. }
  116. }
  117. override func windowDidLoad() {
  118. super.windowDidLoad()
  119. let preView: CPDFView = CPDFView(frame: self.prePdfBGView.bounds)
  120. self.prePdfBGView.addSubview(preView)
  121. self.prePDFView = preView
  122. self.ocrBox.fillColor = .gridColor
  123. localizedLanguage()
  124. self.currentPageIndexTextField.delegate = self;
  125. pageRangLabel.stringValue = KMLocalizedString("Page Range:", nil)
  126. pageRangeBox.addItem(withObjectValue: KMLocalizedString("All Pages", nil))
  127. pageRangeBox.addItem(withObjectValue: KMLocalizedString("Current Page", nil))
  128. pageRangeBox.addItem(withObjectValue: KMLocalizedString("Odd Pages Only", nil))
  129. pageRangeBox.addItem(withObjectValue: KMLocalizedString("Even Pages Only", nil))
  130. pageRangeBox.addItem(withObjectValue: KMLocalizedString("e.g. 1,3-5,10", nil))
  131. (pageRangeBox.cell as? NSComboBoxCell)?.placeholderString = KMLocalizedString("e.g. 1,3-5,10", nil)
  132. pageRangeBox.selectItem(at: 0)
  133. pageRangeBox.isEditable = false
  134. ocrLanguageBox.selectItem(at: 2)
  135. ocrLabel.textColor = KMAppearance.Layout.h2Color()
  136. ocrLanguageBox.isEnabled = false
  137. ocrButton.state = .off
  138. let isLoc: Bool = self.pdfDocument!.isLocked
  139. if (isLoc) {
  140. self.showWaitting()
  141. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  142. let url = URL(fileURLWithPath: self.pdfDocument!.documentURL.path)
  143. KMBaseWindowController.checkPassword(url: url, type: .owner) { result, password in
  144. self.hideWaitting()
  145. if password.count > 0 {
  146. let unlockSuccess: Bool = ((self.pdfDocument?.unlock(withPassword: password)) != nil)
  147. self.lockPassword = password
  148. if unlockSuccess {
  149. self.currentPage = self.pdfDocument?.page(at: 0)
  150. }
  151. self.configuView()
  152. } else {
  153. self.buttonClicked_Cancel(self.cancelButton)
  154. }
  155. }
  156. }
  157. } else {
  158. self.configuView()
  159. }
  160. if #available(macOS 10.14, *) {
  161. let appearanceName = NSApp.effectiveAppearance.bestMatch(from: [.aqua, .darkAqua])
  162. if appearanceName == .darkAqua {
  163. moreConvertLabel.textColor = NSColor.white
  164. }
  165. }
  166. isAllInOneSheet = false
  167. separateButton.state = .on
  168. singleButton.state = .off
  169. allContentBtn.state = .on
  170. allContentMenu.isEnabled = true
  171. tableMenu.isEnabled = false
  172. excelContentOption = .allContent
  173. excelWorksheetOption = .forEachPage
  174. NotificationCenter.default.addObserver(self, selector: #selector(PDFViewDocumentChangedNotification(notification:)), name: NSNotification.Name.CPDFViewPageChanged, object: nil)
  175. }
  176. @objc func PDFViewDocumentChangedNotification(notification: NSNotification) {
  177. let pageIndex = self.prePDFView.currentPageIndex+1
  178. self.currentPageIndexTextField.stringValue = "\(pageIndex)"
  179. }
  180. func windowShouldClose(_ sender: NSWindow) -> Bool {
  181. self.dismissSheet()
  182. return true
  183. }
  184. func localizedLanguage() {
  185. formatLabel.stringValue = KMLocalizedString("Format:", nil)
  186. separateButton.title = KMLocalizedString("Convert each page to a separate worksheet", nil)
  187. singleButton.title = KMLocalizedString("Convert all pages to a single worksheet", nil)
  188. ocrBox.title = KMLocalizedString("OCR Settings", nil)
  189. ocrLabel.stringValue = KMLocalizedString("Language Option", nil) + ":"
  190. ocrButton.title = KMLocalizedString("Recognize text", nil)
  191. separateStypeLabel.stringValue = KMLocalizedString("Excel Worksheet Settings", nil)
  192. extractTableButton.title = KMLocalizedString("Extract Tables Only", nil)
  193. batchButton.title = KMLocalizedString("Batch", nil)
  194. cancelButton.title = KMLocalizedString("Cancel", nil)
  195. convertButton.title = KMLocalizedString("Convert", nil)
  196. upgradeButton.title = KMLocalizedString("Upgrade", nil)
  197. onlyTextBtn.title = KMLocalizedString("Only Text", nil)
  198. onlyTableBtn.title = KMLocalizedString("Only Table", nil)
  199. allContentBtn.title = KMLocalizedString("All Content", nil)
  200. tableMenu1.title = KMLocalizedString("Create Sheet for each Table", nil)
  201. tableMenu2.title = KMLocalizedString("Create Sheet for each Page", nil)
  202. allContentMenu2.title = KMLocalizedString("Create Sheet for each Page", nil)
  203. tableMenu3.title = KMLocalizedString("Create single Sheet for File", nil)
  204. allContentMenu3.title = KMLocalizedString("Create single Sheet for File", nil)
  205. }
  206. func hideWaitting() {
  207. maskView?.removeFromSuperview()
  208. }
  209. func showWaitting() {
  210. if maskView == nil {
  211. maskView = KMBookletMaskView(frame: CGRect(x: 0, y: 0, width: self.window?.frame.size.width ?? 0, height: self.window?.frame.size.height ?? 0))
  212. }
  213. self.window?.contentView?.addSubview(maskView!)
  214. }
  215. func configuView() {
  216. self.prePDFView.autoScales = true
  217. self.prePDFView.wantsLayer = true
  218. self.prePDFView.document = self.pdfDocument
  219. self.imageDPISelectButton.removeAllItems()
  220. self.formatSelectButton.removeAllItems()
  221. let dpiArray = ["50 dpi", "72 dpi", "96 dpi", "150 dpi", "300 dpi", "600 dpi"]
  222. let formArray = ["Excel (.xlsx)", "PowerPoint (.pptx)", "RTF (.rtf)", "CSV (.csv)", "Html (.html)", "Text (.txt)", "JPEG (.jpeg)", "JPG (.jpg)", "PNG (.png)", "GIF (.gif)", "TIFF (.tiff)", "TGA (.tga)", "BMP (.bmp)", "JPEG-2000 (.jp2)","Json(.json)"]
  223. for (index, string) in formArray.enumerated() {
  224. let item = NSMenuItem()
  225. item.title = string
  226. item.target = self
  227. item.tag = index + 1
  228. self.formatSelectButton.menu?.addItem(item)
  229. }
  230. var isShowAdvanced = false
  231. let wordItem = NSMenuItem()
  232. wordItem.title = "Word (.docx)"
  233. wordItem.target = self
  234. var isShowUpgrade = false
  235. var convertSample: KMConvertType = .Word
  236. #if VERSION_FREE
  237. #if VERSION_DMG
  238. // 桌机版
  239. wordItem.tag = convertSample.rawValue
  240. isShowAdvanced = true
  241. if KMMemberInfo.shared.isMemberAllFunction {
  242. if KMMemberInfo.shared.isPermitConvert {
  243. convertSample = .AdvancedWord
  244. wordItem.tag = convertSample.rawValue
  245. isShowAdvanced = false
  246. } else {
  247. isShowUpgrade = true
  248. }
  249. } else {
  250. isShowUpgrade = true
  251. }
  252. #else
  253. // 免费版
  254. convertSample = .Word
  255. wordItem.tag = convertSample.rawValue
  256. isShowAdvanced = true
  257. if KMMemberInfo.shared.isMemberAllFunction {
  258. if KMMemberInfo.shared.isPermitConvert {
  259. convertSample = .AdvancedWord
  260. wordItem.tag = convertSample.rawValue
  261. isShowAdvanced = false
  262. } else {
  263. isShowUpgrade = true
  264. }
  265. } else {
  266. isShowUpgrade = true
  267. }
  268. #endif
  269. #else
  270. convertSample = .Word
  271. wordItem.tag = convertSample.rawValue
  272. // 付费版
  273. isShowAdvanced = true
  274. if KMMemberInfo.shared.isPermitConvert {
  275. isShowAdvanced = false
  276. convertSample = .AdvancedWord
  277. wordItem.tag = convertSample.rawValue
  278. } else {
  279. isShowUpgrade = true
  280. }
  281. #endif
  282. if wordItem.title != "" {
  283. if isShowAdvanced {
  284. let originStr = " \(KMLocalizedString("Standard", nil)) "
  285. let str = KMLocalizedString("Word (.docx)", nil)
  286. let attributedStr = NSMutableAttributedString(string: "\(str) \(originStr)")
  287. 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))
  288. attributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: NSColor.white, range: NSMakeRange(str.count + 2, originStr.count))
  289. let item = NSMenuItem()
  290. item.attributedTitle = attributedStr
  291. item.target = self
  292. item.tag = wordItem.tag
  293. self.formatSelectButton.menu?.insertItem(item, at: 0)
  294. } else {
  295. self.formatSelectButton.menu?.insertItem(wordItem, at: 0)
  296. }
  297. }
  298. if !isShowUpgrade {
  299. self.upgradeButton.isHidden = true
  300. }
  301. var selectIndex = 0
  302. if isShowAdvanced {
  303. let originStr = " \(NSLocalizedString("Advanced", comment: "")) "
  304. let str = NSLocalizedString("Word (.docx)", comment: "")
  305. let attributedStr = NSMutableAttributedString(string: "\(str) \(originStr)")
  306. 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))
  307. attributedStr.addAttribute(NSAttributedString.Key.foregroundColor, value: NSColor.white, range: NSMakeRange(str.count + 2, originStr.count))
  308. let item = NSMenuItem()
  309. item.attributedTitle = attributedStr
  310. item.target = self
  311. convertSample = .AdvancedWord
  312. item.tag = convertSample.rawValue
  313. self.formatSelectButton.menu?.insertItem(item, at: 0)
  314. }
  315. if self.convertType == .Word {
  316. selectIndex = 0
  317. } else {
  318. selectIndex = self.convertType.rawValue
  319. if isShowAdvanced {
  320. selectIndex += 1
  321. }
  322. if wordItem.title == "" {
  323. selectIndex -= 1
  324. }
  325. }
  326. self.formatSelectButton.selectItem(at: selectIndex)
  327. let item = self.formatSelectButton.selectedItem
  328. self.convertType = KMConvertType(rawValue: item?.tag ?? 0) ?? .Word
  329. self.imageDPISelectButton.isHidden = true
  330. self.buttonTopOffset.constant = -26
  331. self.imageDPISelectButton.addItems(withTitles: dpiArray)
  332. self.imageDPISelectButton.selectItem(at: 3)
  333. self.imageDPINumber = 150
  334. self.totalPageCountLabel.stringValue = " / \(self.pdfDocument?.pageCount ?? 1)"
  335. self.totalPageCountLabel.textColor = KMAppearance.Layout.h2Color()
  336. self.updataView()
  337. self.prePDFView.go(to: currentPage)
  338. let pageIndex = self.prePDFView.currentPageIndex+1
  339. self.currentPageIndexTextField.stringValue = "\(pageIndex)"
  340. if let documentURL = self.pdfDocument?.documentURL{
  341. self.pdfDocument = CPDFDocument(url: documentURL)
  342. if !lockPassword.isEmpty && lockPassword.count > 0{
  343. self.pdfDocument?.unlock(withPassword: lockPassword)
  344. }
  345. self.prePDFView.document = self.pdfDocument
  346. self.fileAttri_ = KMFileAttribute()
  347. self.fileAttri_?.filePath = documentURL.path
  348. }
  349. self.prePDFView.setDisplay(.singlePage)
  350. self.prePDFView.layoutDocumentView()
  351. }
  352. func updataView() {
  353. var moreLabelString: String = ""
  354. var offset: CGFloat = 0
  355. excelStyleBox.isHidden = true
  356. #if VERSION_FREE
  357. // 座机版
  358. if !KMMemberInfo.shared.isMemberAllFunction {
  359. moreLabelString = String(format: "%@ %@", KMLocalizedString("The first 10 pages for free"), KMLocalizedString("Unlimited Convert"))
  360. } else {
  361. if !KMMemberInfo.shared.isPermitConvert {
  362. if .Word == self.convertType ||
  363. self.convertType == .AdvancedWord ||
  364. .Excel == self.convertType ||
  365. .PPT == self.convertType ||
  366. .CSV == self.convertType ||
  367. .RTF == self.convertType ||
  368. .GIF == self.convertType ||
  369. .TIFF == self.convertType ||
  370. .JP2 == self.convertType ||
  371. .BMP == self.convertType ||
  372. .TGA == self.convertType ||
  373. .Json == self.convertType {
  374. moreLabelString = String(format: "%@ %@", KMLocalizedString("The first 10 pages for free"), KMLocalizedString("Unlimited Convert"))
  375. }
  376. }
  377. }
  378. #else
  379. // 付费版
  380. if !(KMMemberInfo.shared.isPermitConvert) {
  381. if .Word == self.convertType ||
  382. self.convertType == .AdvancedWord ||
  383. .Excel == self.convertType ||
  384. .PPT == self.convertType ||
  385. .CSV == self.convertType ||
  386. .RTF == self.convertType ||
  387. .GIF == self.convertType ||
  388. .TIFF == self.convertType ||
  389. .JP2 == self.convertType ||
  390. .BMP == self.convertType ||
  391. .TGA == self.convertType ||
  392. .Json == self.convertType {
  393. moreLabelString = String(format: "%@ %@", KMLocalizedString("The first 10 pages for free"), KMLocalizedString("Unlimited Convert"))
  394. }
  395. }
  396. #endif
  397. if convertType.rawValue >= 7 && convertType.rawValue <= 14 {
  398. offset += imageDPISelectButton.frame.size.height
  399. imageDPISelectButton.isHidden = false
  400. buttonTopOffset.constant = 5
  401. } else {
  402. imageDPISelectButton.isHidden = true
  403. buttonTopOffset.constant = -26
  404. }
  405. if moreLabelString.count < 1 {
  406. offset += 5
  407. moreConvertBox.isHidden = true
  408. moreButton.isHidden = true
  409. } else {
  410. offset += (moreButton.frame.size.height + 5)
  411. moreConvertBox.isHidden = false
  412. moreButton.isHidden = false
  413. moreConvertLabel.stringValue = moreLabelString
  414. if let range = moreLabelString.range(of: NSLocalizedString("Unlimited Convert", comment: "")) {
  415. let newR: NSRange = moreLabelString.nsRange(from: range)!
  416. let attributedStr = NSMutableAttributedString(string: moreLabelString)
  417. attributedStr.addAttribute(.foregroundColor, value: NSColor.labelColor, range: NSRange(location: 0, length: newR.location - 1))
  418. attributedStr.addAttribute(.foregroundColor, value: NSColor(red: 8/255, green: 124/255, blue: 1, alpha: 1), range: newR)
  419. attributedStr.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: newR)
  420. moreConvertLabel.attributedStringValue = attributedStr
  421. }
  422. }
  423. if .Excel == convertType {
  424. separateStypeTopOffset.constant = separateStypeBox.frame.size.height + ocrBox.frame.size.height + 16 + 58
  425. extractTableTopOffset.constant = separateStypeBox.frame.size.height + 50
  426. ocrTopOffset.constant = 128
  427. separateStypeBox.isHidden = true
  428. extractTableButton.isHidden = true
  429. excelStyleBox.isHidden = false
  430. separateStypeLabel.isHidden = false
  431. ocrBox.isHidden = false
  432. separateStypeLabel.stringValue = NSLocalizedString("Excel Worksheet Settings", comment: "")
  433. separateButton.title = NSLocalizedString("Convert each page to a separate worksheet", comment: "")
  434. singleButton.title = NSLocalizedString("Convert all pages to a single worksheet", comment: "")
  435. buttonClicked_extractTable(extractTableButton)
  436. } else if .AdvancedWord == convertType {
  437. singleButton.setTitleColor(NSColor.labelColor)
  438. separateButton.setTitleColor(NSColor.labelColor)
  439. separateStypeLabel.textColor = NSColor.labelColor
  440. separateButton.state = NSControl.StateValue.on
  441. singleButton.isEnabled = true
  442. separateButton.isEnabled = true
  443. isAllInOneSheet = false
  444. separateStypeTopOffset.constant = separateStypeBox.frame.size.height + 40 + ocrBox.frame.size.height
  445. ocrTopOffset.constant = separateStypeBox.frame.size.height + 40
  446. extractTableButton.isHidden = true
  447. separateStypeBox.isHidden = false
  448. separateStypeLabel.isHidden = false
  449. separateStypeLabel.stringValue = NSLocalizedString("Layout Settings", comment: "")
  450. separateButton.title = NSLocalizedString("Retain Flowing Text", comment: "")
  451. singleButton.title = NSLocalizedString("Retain Page Layout", comment: "")
  452. ocrBox.isHidden = false
  453. } else if .Json == convertType {
  454. singleButton.setTitleColor(NSColor.labelColor)
  455. separateButton.setTitleColor(NSColor.labelColor)
  456. separateStypeLabel.textColor = NSColor.labelColor
  457. separateButton.state = NSControl.StateValue.on
  458. singleButton.isEnabled = true
  459. separateButton.isEnabled = true
  460. isAllInOneSheet = false
  461. separateStypeTopOffset.constant = separateStypeBox.frame.size.height + 40 + ocrBox.frame.size.height
  462. ocrTopOffset.constant = separateStypeBox.frame.size.height + 40
  463. extractTableButton.isHidden = true
  464. separateStypeBox.isHidden = false
  465. separateStypeLabel.isHidden = false
  466. separateStypeLabel.stringValue = NSLocalizedString("Json Worksheet Settings", comment: "")
  467. separateButton.title = NSLocalizedString("Extract text content", comment: "")
  468. singleButton.title = NSLocalizedString("Extract tables", comment: "")
  469. ocrBox.isHidden = false
  470. } else if .CSV == convertType {
  471. separateStypeTopOffset.constant = extractTableButton.frame.size.height + 30
  472. extractTableTopOffset.constant = 12
  473. extractTableButton.isHidden = false
  474. separateStypeBox.isHidden = true
  475. separateStypeLabel.isHidden = true
  476. ocrBox.isHidden = true
  477. } else if (.PPT == convertType || .RTF == convertType) {
  478. separateStypeTopOffset.constant = ocrBox.frame.size.height + 10
  479. ocrTopOffset.constant = 10
  480. separateStypeBox.isHidden = true
  481. extractTableButton.isHidden = true
  482. separateStypeLabel.isHidden = true
  483. ocrBox.isHidden = false
  484. } else if (.HTML == convertType ||
  485. .Text == convertType) {
  486. separateStypeTopOffset.constant = ocrBox.frame.size.height + 10
  487. ocrTopOffset.constant = 10
  488. separateStypeBox.isHidden = true
  489. extractTableButton.isHidden = true
  490. separateStypeLabel.isHidden = true
  491. ocrBox.isHidden = false
  492. } else {
  493. separateStypeTopOffset.constant = 10
  494. separateStypeBox.isHidden = true
  495. extractTableButton.isHidden = true
  496. separateStypeLabel.isHidden = true
  497. ocrBox.isHidden = true
  498. }
  499. buttonBottemOffset.constant = offset
  500. }
  501. @IBAction func buttonClicked_extractTable(_ sender: NSButton) {
  502. if extractTableButton.state == .on {
  503. singleButton.setTitleColor(KMAppearance.Layout.b15_1Color())
  504. separateButton.setTitleColor(KMAppearance.Layout.b15_1Color())
  505. separateStypeLabel.textColor = KMAppearance.Layout.b15_1Color()
  506. singleButton.isEnabled = false
  507. separateButton.isEnabled = false
  508. separateButton.state = .off
  509. singleButton.state = .off
  510. } else {
  511. singleButton.setTitleColor(NSColor.labelColor)
  512. separateButton.setTitleColor(NSColor.labelColor)
  513. separateStypeLabel.textColor = NSColor.labelColor
  514. separateButton.state = .on
  515. singleButton.isEnabled = true
  516. separateButton.isEnabled = true
  517. isAllInOneSheet = false
  518. }
  519. }
  520. @IBAction func buttonClicked_Cancel(_ sender: NSButton) {
  521. dismissSheet()
  522. }
  523. func dismissSheet() {
  524. if FileManager.default.fileExists(atPath: kConvertFileSavePath ?? "") {
  525. do {
  526. try FileManager.default.removeItem(atPath: kConvertFileSavePath ?? "")
  527. } catch {
  528. print("Error removing item at path: (error.localizedDescription)")
  529. }
  530. }
  531. self.km_quick_endSheet()
  532. }
  533. func showCriticalAlert(_ alertMsg: String?) {
  534. let alert = NSAlert()
  535. alert.alertStyle = .critical
  536. if let msg = alertMsg {
  537. alert.informativeText = msg
  538. } else {
  539. let st = self.pdfDocument?.documentURL.lastPathComponent.count ?? 0 > 0 ? self.pdfDocument?.documentURL.lastPathComponent : KMLocalizedString("Untitled.pdf", nil)
  540. alert.messageText = String(format: "%@ %@", st!, KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
  541. }
  542. alert.runModal()
  543. }
  544. func produceNewPDF(with pagesArray: [CPDFPage]) {
  545. tempPDFDocument = nil
  546. self.tempPDFDocument = CPDFDocument()
  547. var indexs = IndexSet()
  548. let pageCount = self.pdfDocument?.pageCount ?? 0
  549. for addedPage in pagesArray {
  550. let idx = addedPage.pageIndex()
  551. if idx >= 0 && idx < pageCount {
  552. indexs.insert(IndexSet.Element(idx))
  553. }
  554. }
  555. _ = self.tempPDFDocument?.importPages(indexs, from: self.pdfDocument, at: 0)
  556. self.prePDFView.document = self.tempPDFDocument
  557. self.prePDFView.layoutDocumentView()
  558. self.prePDFView.goToFirstPage(nil)
  559. self.currentPageIndexTextField.stringValue = "1"
  560. self.totalPageCountLabel.stringValue = " / \(self.tempPDFDocument!.pageCount)"
  561. }
  562. @IBAction func buttonClicked_FormatSelect(_ sender: NSButton) {
  563. guard let item = self.formatSelectButton.selectedItem else { return }
  564. self.convertType = KMConvertType(rawValue: item.tag) ?? .Word
  565. updataView()
  566. }
  567. func transform(with string: String) {
  568. if string == KMLocalizedString("Create Sheet for each Table", nil) {
  569. self.excelWorksheetOption = .forEachTable
  570. } else if string == KMLocalizedString("Create Sheet for each Page", nil) {
  571. self.excelWorksheetOption = .forEachPage
  572. } else if string == KMLocalizedString("Create single Sheet for File", nil) {
  573. self.excelWorksheetOption = .forTheDocument
  574. }
  575. }
  576. @IBAction func buttonClicked_excelStyle(_ sender: NSButton) {
  577. if sender.isEqual(self.onlyTextBtn) {
  578. self.tableMenu.isEnabled = false
  579. self.allContentMenu.isEnabled = false
  580. self.excelContentOption = .onlyText
  581. self.excelWorksheetOption = .forEachTable
  582. } else if (sender.isEqual(self.onlyTableBtn)) {
  583. self.tableMenu.isEnabled = true
  584. self.allContentMenu.isEnabled = false
  585. self.excelContentOption = .onlyTable
  586. transform(with: self.tableMenu.selectedItem?.title ?? "")
  587. } else if (sender.isEqual(self.allContentBtn)) {
  588. self.tableMenu.isEnabled = false
  589. self.allContentMenu.isEnabled = true
  590. self.excelContentOption = .allContent
  591. transform(with: self.allContentMenu.selectedItem?.title ?? "")
  592. }
  593. }
  594. @IBAction func buttonClicked_tableMenu(_ sender: NSButton) {
  595. transform(with: self.tableMenu.selectedItem?.title ?? "")
  596. }
  597. @IBAction func buttonClicked_allContentMenu(_ sender: NSButton) {
  598. transform(with: self.allContentMenu.selectedItem?.title ?? "")
  599. }
  600. @IBAction func buttonClicked_DpiSelect(_ sender: NSPopUpButton) {
  601. let index = sender.indexOfSelectedItem
  602. var dpi = 0
  603. if index == 0 {
  604. dpi = 50
  605. } else if index == 1 {
  606. dpi = 72
  607. } else if index == 2 {
  608. dpi = 96
  609. } else if index == 3 {
  610. dpi = 150
  611. } else if index == 4 {
  612. dpi = 300
  613. } else if index == 5 {
  614. dpi = 600
  615. }
  616. self.imageDPINumber = dpi
  617. }
  618. @IBAction func buttonClick_PreviousPage(_ sender: NSButton) {
  619. if self.prePDFView.canGoToPreviousPage() {
  620. self.prePDFView.goToPreviousPage(nil)
  621. let currentIndex = self.prePDFView.document?.index(for: self.prePDFView.currentPage()) ?? 0
  622. self.currentPageIndexTextField.stringValue = "\(currentIndex + 1)"
  623. }
  624. }
  625. @IBAction func buttonClicked_NextPage(_ sender: NSButton) {
  626. if self.prePDFView.canGoToNextPage() {
  627. self.prePDFView.goToNextPage(nil)
  628. let currentIndex = self.prePDFView.document?.index(for: self.prePDFView.currentPage())
  629. self.currentPageIndexTextField.stringValue = "\((currentIndex ?? 0) + 1)"
  630. }
  631. }
  632. @IBAction func buttonClicked_Batch(_ sender: NSButton) {
  633. if KMMemberInfo.shared.isMemberAllFunction == false {
  634. KMMemberInfo.shared.advancedFunctionUsage()
  635. return
  636. }
  637. if self.pdfDocument?.documentURL == nil {
  638. NSSound.beep()
  639. return
  640. }
  641. if self.callBackBlock != nil {
  642. self.callBackBlock!()
  643. self.callBackBlock = nil
  644. }
  645. dismissSheet()
  646. }
  647. @IBAction func buttonClicked_Convert(_ sender: Any) {
  648. self.window?.makeFirstResponder(self.prePDFView)
  649. if (4 == pageRangeBox.indexOfSelectedItem) {
  650. if self.fileAttri_ == nil {
  651. self.fileAttri_ = KMFileAttribute()
  652. self.fileAttri_?.filePath = self.pdfDocument?.documentURL.path ?? ""
  653. }
  654. // fileAttri_.pdfDocument = self.pdfDocument
  655. self.fileAttri_?.bAllPage = false
  656. self.fileAttri_?.pagesType = .custom
  657. self.fileAttri_?.pagesString = pageRangeBox.stringValue
  658. let pages = self.fileAttri_?.fetchSelectPages() ?? []
  659. if pages.isEmpty {
  660. return
  661. }
  662. }
  663. let openPanel = NSOpenPanel()
  664. openPanel.canChooseFiles = false
  665. openPanel.canChooseDirectories = true
  666. openPanel.canCreateDirectories = true
  667. openPanel.beginSheetModal(for: self.window!) { result in
  668. if result == .OK {
  669. self.savePath = openPanel.url!.path
  670. self.beginConvert()
  671. }
  672. }
  673. }
  674. func beginConvert() {
  675. var isLimitConvert = false
  676. #if VERSION_FREE
  677. #if VERSION_DMG
  678. // 桌机版
  679. if KMMemberInfo.shared.isMemberAllFunction {
  680. if KMMemberInfo.shared.isPermitConvert {
  681. isLimitConvert = false
  682. } else {
  683. if (.PNG == self.convertType ||
  684. .JPEG == self.convertType ||
  685. .JPG == self.convertType ||
  686. .HTML == self.convertType ||
  687. .Text == self.convertType ||
  688. .Word == self.convertType) {
  689. isLimitConvert = false
  690. } else {
  691. isLimitConvert = true
  692. }
  693. }
  694. } else {
  695. isLimitConvert = true
  696. }
  697. #else
  698. // 免费版
  699. if KMMemberInfo.shared.isPermitConvert == false {
  700. isLimitConvert = true
  701. }
  702. #endif
  703. #else
  704. // 付费版
  705. if KMMemberInfo.shared.isPermitConvert {
  706. isLimitConvert = false
  707. } else {
  708. if (.PNG == self.convertType ||
  709. .JPEG == self.convertType ||
  710. .JPG == self.convertType ||
  711. .HTML == self.convertType ||
  712. .Text == self.convertType ||
  713. .Word == self.convertType) {
  714. isLimitConvert = false
  715. } else {
  716. isLimitConvert = true
  717. }
  718. }
  719. #endif
  720. self.showWaitting()
  721. let pages = NSMutableArray()
  722. DispatchQueue.global(qos: .background).async { [self] in
  723. let filePath = getConvertFileSavePath()
  724. if (FileManager.default.fileExists(atPath: filePath)) {
  725. try?FileManager.default.removeItem(atPath: filePath)
  726. }
  727. if self.prePDFView.document.write(toFile: filePath) {
  728. for i in 0..<self.prePDFView.document.pageCount {
  729. pages.add(i + 1)
  730. }
  731. }
  732. DispatchQueue.main.async {
  733. if pages.count < 1 {
  734. self.hideWaitting()
  735. let alert = NSAlert()
  736. alert.alertStyle = .critical
  737. alert.messageText = "\(self.pdfDocument?.documentURL.path.lastPathComponent ?? NSLocalizedString("Untitled", comment: "")) \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
  738. alert.runModal()
  739. return
  740. }
  741. let pageCount = pages.count
  742. var selectPages = [Int]()
  743. if isLimitConvert {
  744. if pageCount >= 10 {
  745. for i in 0..<10 {
  746. selectPages.append(pages[i] as! Int)
  747. }
  748. } else {
  749. selectPages = pages as! [Int]
  750. }
  751. } else {
  752. selectPages = pages as! [Int]
  753. }
  754. var convert = KMPDFConvert()
  755. if self.convertType == .Json {
  756. convert = KMPDFConvertJson()
  757. convert.isAllInOneSheet = self.isAllInOneSheet
  758. } else if self.convertType == .AdvancedWord || self.convertType == .Word {
  759. convert = KMPDFConvertWord()
  760. let data = convert as? KMPDFConvertWord
  761. data?.layoutOptions = self.isAllInOneSheet ? .retainPageLayout : .retainFlowingText
  762. convert.isAllInOneSheet = self.isAllInOneSheet
  763. } else if self.convertType == .Excel {
  764. convert = KMPDFConvertExcel()
  765. convert.isExtractText = false
  766. convert.isExtractTable = false
  767. convert.isAllInOneSheet = self.excelWorksheetOption == .forTheDocument
  768. if self.onlyTextBtn.state == .on {
  769. convert.isExtractText = true
  770. } else if self.onlyTableBtn.state == .on {
  771. convert.isExtractTable = true
  772. convert.extractTableIndex = self.excelWorksheetOption?.rawValue ?? 0
  773. }
  774. } else if self.convertType == .PPT {
  775. convert = KMPDFConvertPPT()
  776. } else if self.convertType == .Text {
  777. convert = KMPDFConvertText()
  778. } else if self.convertType == .RTF {
  779. convert = KMPDFConvertRTF()
  780. } else if self.convertType == .HTML {
  781. convert = KMPDFConvertHTML()
  782. } else if self.convertType == .CSV {
  783. convert = KMPDFConvertCSV()
  784. convert.isExtractTable = self.extractTableButton.state == .on
  785. } else {
  786. if self.convertType == .JPEG ||
  787. self.convertType == .JPG ||
  788. self.convertType == .PNG ||
  789. self.convertType == .GIF ||
  790. self.convertType == .TIFF ||
  791. self.convertType == .TGA ||
  792. self.convertType == .BMP ||
  793. self.convertType == .JP2 {
  794. convert = KMPDFConvertImage()
  795. let data = convert as? KMPDFConvertImage
  796. if self.convertType == .JPEG {
  797. data?.imageType = .JPEG
  798. } else if self.convertType == .JPG {
  799. data?.imageType = .JPEG
  800. } else if self.convertType == .PNG {
  801. data?.imageType = .PNG
  802. }
  803. // else if self.convertType == .GIF {
  804. // } else if self.convertType == .TIFF {
  805. // } else if self.convertType == .TGA {
  806. // } else if self.convertType == .BMP {
  807. // }
  808. data?.imageDpi = self.imageDPINumber
  809. }
  810. }
  811. convert.isAllowOCR = self.ocrButton.state == .on
  812. convert.ocrLanguage = convert.isAllowOCR ? self.ocrLanguage_ : .english
  813. convert.filePath = filePath
  814. convert.pages = selectPages
  815. // convert.isAllInOneSheet = self.isAllInOneSheet
  816. if self.convertType == .Word {
  817. convert.type = 0
  818. } else {
  819. convert.type = 1
  820. }
  821. // convert.isExtractTable = self.extractTableButton.state == .on
  822. convert.outputFolderPath = self.savePath
  823. convert.outputFileName = (self.pdfDocument?.documentURL.deletingPathExtension().lastPathComponent) ?? NSLocalizedString("Untitled", comment: "")
  824. if self.convertType == .AdvancedWord {
  825. convert.convertType = .word
  826. } else if self.convertType == .JPEG ||
  827. self.convertType == .JPG ||
  828. self.convertType == .PNG ||
  829. self.convertType == .GIF ||
  830. self.convertType == .TIFF ||
  831. self.convertType == .TGA ||
  832. self.convertType == .BMP ||
  833. self.convertType == .JP2 {
  834. convert.convertType = KMPDFConvertType(rawValue: self.convertType.rawValue) ?? .word
  835. convert.options = [KMPDFConvertOptionsKeyImageDPI : self.imageDPINumber,
  836. KMPDFConvertOptionsKeyImageWithAnnotation : true] as [String : Any]
  837. } else {
  838. convert.convertType = KMPDFConvertType(rawValue: self.convertType.rawValue) ?? .word
  839. }
  840. if self.convertType == .Excel {
  841. convert.excelContentOption = self.excelContentOption
  842. convert.excelWorksheetOption = self.excelWorksheetOption
  843. }
  844. convert.password = self.lockPassword
  845. KMPDFConvertManager.defaultManager.convert(convert: convert) { [weak self] (finished, error) in
  846. self?.hideWaitting()
  847. if finished {
  848. self?.dismissSheet()
  849. if FileManager.default.fileExists(atPath: convert.outputFilePath) {
  850. let workspace = NSWorkspace.shared
  851. let url = URL(fileURLWithPath: convert.outputFilePath)
  852. workspace.activateFileViewerSelecting([url])
  853. }
  854. } else if let error = error {
  855. let err: NSError = error as NSError
  856. var erroeString = ""
  857. switch err.code {
  858. case CPDFConverterEncryptError:
  859. erroeString = NSLocalizedString("Password required or incorrect password. Please re-enter your password and try again", comment: "")
  860. break
  861. case CPDFConverterPermissionError:
  862. erroeString = NSLocalizedString("The license doesn't allow the permission", comment: "")
  863. break
  864. case CPDFConverterMallocError:
  865. erroeString = NSLocalizedString("Malloc failure", comment: "")
  866. break
  867. case CPDFConverterUnknownError:
  868. erroeString = NSLocalizedString("Unknown error in processing conversion. Please try again later", comment: "")
  869. break
  870. case CPDFConverterPDFUnknownError:
  871. erroeString = NSLocalizedString("Unknown error in processing PDF. Please try again later", comment: "")
  872. break
  873. case CPDFConverterPDFFileError:
  874. erroeString = NSLocalizedString("File not found or could not be opened. Check if your file exists or choose another file to convert", comment: "")
  875. break
  876. case CPDFConverterPDFFormatError:
  877. erroeString = NSLocalizedString("File not in PDF format or corruptead. Change a PDF file and try again", comment: "")
  878. break
  879. case CPDFConverterPDFSecurityError:
  880. erroeString = NSLocalizedString("Unsupported security scheme", comment: "")
  881. break
  882. case CPDFConverterPDFPageError:
  883. erroeString = NSLocalizedString("Page not found or content error", comment: "")
  884. break
  885. default:
  886. erroeString = NSLocalizedString("Table not found", comment: "")
  887. break
  888. }
  889. let alert = NSAlert()
  890. alert.alertStyle = .critical
  891. alert.messageText = NSLocalizedString("Conversion Failed", comment: "")
  892. alert.informativeText = erroeString
  893. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  894. if #available(OSX 10.14, *) {
  895. alert.beginSheetModal(for: self?.window! ?? NSWindow.currentWindow(), completionHandler: nil)
  896. } else {
  897. alert.runModal()
  898. }
  899. }
  900. }
  901. }
  902. }
  903. }
  904. func getConvertFileSavePath() -> String {
  905. var path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last
  906. path?.append("/")
  907. path?.append(Bundle.main.bundleIdentifier!)
  908. if (FileManager.default.fileExists(atPath: path!) == false) {
  909. try?FileManager.default.createDirectory(atPath: path!, withIntermediateDirectories: false)
  910. }
  911. path?.append("/")
  912. path?.append("convert.pdf")
  913. return path!
  914. }
  915. @IBAction func buttonClicked_MoreConvert(_ sender: NSButton) {
  916. if sender == self.moreButton {
  917. FMTrackEventManager.defaultManager.trackEvent(event: "PUW", withProperties: ["PUW_Btn": "Btn_PUW_Converter_Premium_UnlimitedConvertw"])
  918. } else if sender == self.upgradeButton {
  919. FMTrackEventManager.defaultManager.trackEvent(event: "PUW", withProperties: ["PUW_Btn": "Btn_PUW_Converter_Premium_Upgrade"])
  920. }
  921. #if VERSION_DMG
  922. if KMMemberInfo.shared.isMemberAllFunction && !KMMemberInfo.shared.isPermitConvert {
  923. if KMMemberInfo.shared.userScenarioType == .dmg_type8 ||
  924. KMMemberInfo.shared.userScenarioType == .dmg_type9 {
  925. let alert = NSAlert()
  926. alert.alertStyle = .critical
  927. alert.informativeText = NSLocalizedString("Sorry, you do not have the rights to this paid feature and cannot use it. If you have any questions, please contact customer service at support@pdfreaderpro.com", comment: "")
  928. alert.addButton(withTitle: NSLocalizedString("Got it", comment: ""))
  929. alert.runModal()
  930. } else {
  931. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  932. limitWC.continueBlock = { windowController in
  933. }
  934. limitWC.window?.center()
  935. limitWC.showWindow(nil)
  936. }
  937. } else {
  938. if KMMemberInfo.shared.userScenarioType == .dmg_type16 ||
  939. KMMemberInfo.shared.userScenarioType == .dmg_type17 {
  940. let alert = NSAlert()
  941. alert.alertStyle = .critical
  942. alert.informativeText = NSLocalizedString("Sorry, you do not have the rights to this paid feature and cannot use it. If you have any questions, please contact customer service at support@pdfreaderpro.com", comment: "")
  943. alert.addButton(withTitle: NSLocalizedString("Got it", comment: ""))
  944. alert.runModal()
  945. } else {
  946. if KMMemberInfo.shared.isLogin {
  947. KMProductCompareWC.shared.orientation = false
  948. let winC = KMProductCompareWC.shared
  949. if convertType == .Word || convertType == .AdvancedWord {
  950. winC.kEventName = "Reading_PDFtoWord_BuyNow"
  951. } else if convertType == .Excel {
  952. winC.kEventName = "Reading_PDFtoExcel_BuyNow"
  953. } else if convertType == .PPT {
  954. winC.kEventName = "Reading_PDFtoPPT_BuyNow"
  955. } else if convertType == .RTF {
  956. winC.kEventName = "Reading_PDFtoRTF_BuyNow"
  957. } else if convertType == .CSV {
  958. winC.kEventName = "Reading_PDFtoCSV_BuyNow"
  959. } else if convertType == .Text {
  960. winC.kEventName = "Reading_PDFtoText_BuyNow"
  961. }
  962. if convertType == .JPEG || convertType == .JPG || convertType == .PNG || convertType == .GIF || convertType == .TIFF || convertType == .TGA || convertType == .BMP || convertType == .JP2 {
  963. winC.kEventName = "Reading_PDFtoImage_BuyNow"
  964. }
  965. winC.showWindow(nil)
  966. } else {
  967. KMLoginWindowsController.shared.openWindow() { [weak self] success in
  968. guard let self = self else { return }
  969. if success {
  970. KMProductCompareWC.shared.orientation = false
  971. let winC = KMProductCompareWC.shared
  972. if self.convertType == .Word || self.convertType == .AdvancedWord {
  973. winC.kEventName = "Reading_PDFtoWord_BuyNow"
  974. } else if self.convertType == .Excel {
  975. winC.kEventName = "Reading_PDFtoExcel_BuyNow"
  976. } else if self.convertType == .PPT {
  977. winC.kEventName = "Reading_PDFtoPPT_BuyNow"
  978. } else if self.convertType == .RTF {
  979. winC.kEventName = "Reading_PDFtoRTF_BuyNow"
  980. } else if self.convertType == .CSV {
  981. winC.kEventName = "Reading_PDFtoCSV_BuyNow"
  982. } else if self.convertType == .Text {
  983. winC.kEventName = "Reading_PDFtoText_BuyNow"
  984. }
  985. if self.convertType == .JPEG || self.convertType == .JPG || self.convertType == .PNG || self.convertType == .GIF || self.convertType == .TIFF || self.convertType == .TGA || self.convertType == .BMP || self.convertType == .JP2 {
  986. winC.kEventName = "Reading_PDFtoImage_BuyNow"
  987. }
  988. winC.showWindow(nil)
  989. }
  990. }
  991. }
  992. }
  993. }
  994. #else
  995. if !KMMemberInfo.shared.isPermitConvert {
  996. KMMemberInfo.shared.advancedFunctionUsage()
  997. }
  998. #endif
  999. self.buttonClicked_Cancel(self.cancelButton)
  1000. }
  1001. @IBAction func buttonItemClick_SepSingle(_ sender: Any) {
  1002. if .on == self.singleButton.state {
  1003. self.isAllInOneSheet = true
  1004. } else {
  1005. self.isAllInOneSheet = false
  1006. }
  1007. }
  1008. func controlTextDidEndEditing(_ obj: Notification) {
  1009. let object = obj.object
  1010. if (object as AnyObject).isEqual(self.currentPageIndexTextField) == true {
  1011. let num: Int = Int(self.currentPageIndexTextField.stringValue) ?? 1
  1012. let targetPage = self.prePDFView.document.page(at: UInt(num - 1))
  1013. self.prePDFView.go(to: targetPage)
  1014. } else if (object as AnyObject).isEqual(self.pageRangeBox) == true {
  1015. // guard self.handInputButton.state == .on else { return }
  1016. let idx = self.pageRangeBox.indexOfSelectedItem
  1017. if idx >= 0 && idx <= 3 { // 0 1 2 3
  1018. return
  1019. } else {
  1020. // if self.pageRangeBox.indexOfSelectedItem != 4 { // 页面范围切换中
  1021. }
  1022. if self.fileAttri_ == nil {
  1023. self.fileAttri_ = KMFileAttribute()
  1024. self.fileAttri_?.filePath = self.pdfDocument?.documentURL.path ?? ""
  1025. }
  1026. // fileAttribute.pdfDocument = self.pdfDocument
  1027. self.fileAttri_?.bAllPage = false
  1028. self.fileAttri_?.pagesType = .custom
  1029. self.fileAttri_?.pagesString = self.pageRangeBox.stringValue
  1030. let pages = self.fileAttri_?.fetchSelectPages() ?? []
  1031. if pages.count > 0 {
  1032. let pagesArray = NSMutableArray()
  1033. for pageNum in pages {
  1034. let tpage = self.pdfDocument?.page(at: UInt(pageNum - 1))
  1035. pagesArray.add(tpage as Any)
  1036. }
  1037. self.produceNewPDF(with: pagesArray as! [CPDFPage])
  1038. } else {
  1039. self.showCriticalAlert(nil)
  1040. }
  1041. }
  1042. }
  1043. func controlTextDidChange(_ obj: Notification) {
  1044. if let textField = obj.object as? NSComboBox {
  1045. if textField.isEqual(pageRangeBox) {
  1046. if let stringValue = self.currentPageIndexTextField.formatter?.string(for: NSNumber(value: Float(self.currentPageIndexTextField.stringValue) ?? 0)) {
  1047. if let integerValue = Int(stringValue) {
  1048. var string = stringValue
  1049. if integerValue > self.prePDFView.document.pageCount {
  1050. string = "1-\(self.prePDFView.document.pageCount)"
  1051. } else if integerValue <= 0 {
  1052. string = "1"
  1053. }
  1054. self.currentPageIndexTextField.stringValue = string
  1055. }
  1056. }
  1057. }
  1058. }
  1059. }
  1060. @IBAction func comboxClicked_PageRange(_ sender: NSComboBox) {
  1061. if (0 == pageRangeBox.indexOfSelectedItem ||
  1062. 1 == pageRangeBox.indexOfSelectedItem ||
  1063. 2 == pageRangeBox.indexOfSelectedItem ||
  1064. 3 == pageRangeBox.indexOfSelectedItem) {
  1065. self.window?.makeFirstResponder(self)
  1066. pageRangeBox.isEditable = false
  1067. pageRangeBox.delegate = nil
  1068. } else {
  1069. pageRangeBox.stringValue = pageRangeBox.stringValue
  1070. pageRangeBox.isEditable = true
  1071. pageRangeBox.delegate = self
  1072. self.window?.makeFirstResponder(pageRangeBox)
  1073. }
  1074. self.lockPassword = ""
  1075. let pagesArray = NSMutableArray()
  1076. for i in 0..<(self.pdfDocument?.pageCount ?? 0) {
  1077. let copyPage = self.pdfDocument?.page(at: i)
  1078. pagesArray.add(copyPage!)
  1079. }
  1080. if (2 == pageRangeBox.indexOfSelectedItem) {
  1081. pagesArray.subArray(type: .Odd)
  1082. produceNewPDF(with: pagesArray as! [CPDFPage])
  1083. } else if (3 == pageRangeBox.indexOfSelectedItem) {
  1084. pagesArray.subArray(type: .Even)
  1085. produceNewPDF(with: pagesArray as! [CPDFPage])
  1086. } else if (1 == pageRangeBox.indexOfSelectedItem) {
  1087. pagesArray.removeAllObjects()
  1088. let page = self.prePDFView.currentPage()
  1089. if (page != nil) {
  1090. pagesArray.add(page as Any)
  1091. }
  1092. produceNewPDF(with: pagesArray as! [CPDFPage])
  1093. } else if 0 == pageRangeBox.indexOfSelectedItem {
  1094. produceNewPDF(with: pagesArray as! [CPDFPage])
  1095. }
  1096. }
  1097. @IBAction func comboxClicked_OcrLanguage(_ sender: AnyObject?) {
  1098. switch (ocrLanguageBox.indexOfSelectedItem) {
  1099. case 0: //chinese
  1100. self.ocrLanguage_ = .chinese
  1101. break
  1102. case 1: //chinese zht
  1103. self.ocrLanguage_ = .chineseTraditional
  1104. break
  1105. case 2: //English
  1106. self.ocrLanguage_ = .english
  1107. break
  1108. case 3:
  1109. //Japanese
  1110. self.ocrLanguage_ = .japanese
  1111. break
  1112. case 4:
  1113. //Kroean
  1114. self.ocrLanguage_ = .korean
  1115. break
  1116. case 5:
  1117. //Serbian
  1118. self.ocrLanguage_ = .serbian
  1119. break
  1120. case 6:
  1121. //Occitan
  1122. self.ocrLanguage_ = .occitan
  1123. break
  1124. case 7:
  1125. //Danish
  1126. self.ocrLanguage_ = .danish
  1127. break
  1128. case 8:
  1129. //German
  1130. self.ocrLanguage_ = .german
  1131. break
  1132. case 9:
  1133. //French
  1134. self.ocrLanguage_ = .french
  1135. break
  1136. case 10:
  1137. //Italian
  1138. self.ocrLanguage_ = .italian
  1139. break
  1140. case 11:
  1141. //Spanish
  1142. self.ocrLanguage_ = .spanish
  1143. break
  1144. case 12:
  1145. //Portuguese
  1146. self.ocrLanguage_ = .portuguese
  1147. break
  1148. case 13:
  1149. //Maori
  1150. self.ocrLanguage_ = .maori
  1151. break
  1152. case 14:
  1153. //Malay
  1154. self.ocrLanguage_ = .malay
  1155. break
  1156. case 15:
  1157. //Malay
  1158. self.ocrLanguage_ = .maltese
  1159. break
  1160. case 16:
  1161. //Dutch
  1162. self.ocrLanguage_ = .dutch
  1163. break
  1164. case 17:
  1165. //Norwegian
  1166. self.ocrLanguage_ = .norwegian
  1167. break
  1168. case 18:
  1169. //Polish
  1170. self.ocrLanguage_ = .polish
  1171. break
  1172. case 19:
  1173. //Romanian
  1174. self.ocrLanguage_ = .romanian
  1175. break
  1176. case 20:
  1177. //Slovak
  1178. self.ocrLanguage_ = .slovak
  1179. break
  1180. case 21:
  1181. //Slovenian
  1182. self.ocrLanguage_ = .slovenian
  1183. break
  1184. case 22:
  1185. //Albanian
  1186. self.ocrLanguage_ = .albanian
  1187. break
  1188. case 23:
  1189. //Swedish
  1190. self.ocrLanguage_ = .swedish
  1191. break
  1192. case 24:
  1193. //Swahili
  1194. self.ocrLanguage_ = .swahili
  1195. break
  1196. case 25:
  1197. //Tagalog
  1198. self.ocrLanguage_ = .tagalog
  1199. break
  1200. case 26:
  1201. //Turish
  1202. self.ocrLanguage_ = .turish
  1203. break
  1204. case 27:
  1205. //Uzbek
  1206. self.ocrLanguage_ = .uzbek
  1207. break
  1208. case 28:
  1209. //Vietnamese
  1210. self.ocrLanguage_ = .vietnamese
  1211. break
  1212. case 29:
  1213. //Afrikaans
  1214. self.ocrLanguage_ = .afrikaans
  1215. break
  1216. case 30:
  1217. //Azerbaijani
  1218. self.ocrLanguage_ = .azerbaijani
  1219. break
  1220. case 31:
  1221. //Bosnian
  1222. self.ocrLanguage_ = .bosnian
  1223. break
  1224. case 32:
  1225. //Czech
  1226. self.ocrLanguage_ = .czech
  1227. break
  1228. case 33: //Welsh
  1229. self.ocrLanguage_ = .welsh
  1230. break
  1231. case 34: //Estonian
  1232. self.ocrLanguage_ = .estonian
  1233. break
  1234. case 35: //Irish
  1235. self.ocrLanguage_ = .irish
  1236. break
  1237. case 36: //Croatian
  1238. self.ocrLanguage_ = .croatian
  1239. break
  1240. case 37: //Hungarian
  1241. self.ocrLanguage_ = .hungarian
  1242. break
  1243. case 38: //Indonesian
  1244. self.ocrLanguage_ = .indonesian
  1245. break
  1246. case 39: //Icelandic
  1247. self.ocrLanguage_ = .icelandic
  1248. break
  1249. case 40: //Kurdish
  1250. self.ocrLanguage_ = .kurdish
  1251. break
  1252. case 41: //Lithuanian
  1253. self.ocrLanguage_ = .lithuanian
  1254. break
  1255. case 42: //Latvian
  1256. self.ocrLanguage_ = .latvian
  1257. break
  1258. case 43: //Marathi
  1259. self.ocrLanguage_ = .marathi
  1260. break
  1261. case 44: //Nepali
  1262. self.ocrLanguage_ = .nepali
  1263. break
  1264. case 45: //Latvia
  1265. self.ocrLanguage_ = .latvian
  1266. break
  1267. case 46: //Bihari
  1268. self.ocrLanguage_ = .bihari
  1269. break
  1270. case 47: //Maithili
  1271. self.ocrLanguage_ = .maithili
  1272. break
  1273. case 48: //Angika
  1274. self.ocrLanguage_ = .angika
  1275. break
  1276. case 49: //Bhojpuri
  1277. self.ocrLanguage_ = .bhojpuri
  1278. break
  1279. case 50: //Magahi
  1280. self.ocrLanguage_ = .magahi
  1281. break
  1282. case 51: //Nagpur
  1283. self.ocrLanguage_ = .nagpur
  1284. break
  1285. case 52: //Newari
  1286. self.ocrLanguage_ = .newari
  1287. break
  1288. case 53: //GoanKonkani
  1289. self.ocrLanguage_ = .goanKonkani
  1290. break
  1291. case 54: //SaudiArabia
  1292. self.ocrLanguage_ = .saudiArabia
  1293. break
  1294. default:
  1295. self.ocrLanguage_ = .english
  1296. break
  1297. }
  1298. }
  1299. @IBAction func comboxClicked_SupoortOCR(_ sender: AnyObject?) {
  1300. if(ocrButton.state == .on) {
  1301. ocrLabel.textColor = KMAppearance.titleColor()
  1302. ocrLanguageBox.isEnabled = true
  1303. } else {
  1304. ocrLabel.textColor = KMAppearance.Layout.h2Color()
  1305. ocrLanguageBox.isEnabled = false
  1306. }
  1307. }
  1308. }
  1309. @objc enum KMSubArrayType: Int {
  1310. case Odd = 0
  1311. case Even
  1312. }
  1313. extension NSMutableArray{
  1314. func subArray(type: KMSubArrayType) {
  1315. if self.count == 0 { return }
  1316. if type.rawValue > 1 || type.rawValue < 0 { return }
  1317. let newArray = NSMutableArray()
  1318. for i in 0..<self.count {
  1319. if i % 2 == type.rawValue {
  1320. newArray.add(self[i])
  1321. }
  1322. }
  1323. self.removeAllObjects()
  1324. self.addObjects(from: newArray as! [Any])
  1325. }
  1326. }