KMConvertWindowController.swift 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. offset += 5
  424. moreConvertBox.isHidden = true
  425. moreButton.isHidden = true
  426. if .Excel == convertType {
  427. separateStypeTopOffset.constant = separateStypeBox.frame.size.height + ocrBox.frame.size.height + 16 + 58
  428. extractTableTopOffset.constant = separateStypeBox.frame.size.height + 50
  429. ocrTopOffset.constant = 128
  430. separateStypeBox.isHidden = true
  431. extractTableButton.isHidden = true
  432. excelStyleBox.isHidden = false
  433. separateStypeLabel.isHidden = false
  434. ocrBox.isHidden = false
  435. separateStypeLabel.stringValue = NSLocalizedString("Excel Worksheet Settings", comment: "")
  436. separateButton.title = NSLocalizedString("Convert each page to a separate worksheet", comment: "")
  437. singleButton.title = NSLocalizedString("Convert all pages to a single worksheet", comment: "")
  438. buttonClicked_extractTable(extractTableButton)
  439. } else if .AdvancedWord == convertType {
  440. singleButton.setTitleColor(NSColor.labelColor)
  441. separateButton.setTitleColor(NSColor.labelColor)
  442. separateStypeLabel.textColor = NSColor.labelColor
  443. separateButton.state = NSControl.StateValue.on
  444. singleButton.isEnabled = true
  445. separateButton.isEnabled = true
  446. isAllInOneSheet = false
  447. separateStypeTopOffset.constant = separateStypeBox.frame.size.height + 40 + ocrBox.frame.size.height
  448. ocrTopOffset.constant = separateStypeBox.frame.size.height + 40
  449. extractTableButton.isHidden = true
  450. separateStypeBox.isHidden = false
  451. separateStypeLabel.isHidden = false
  452. separateStypeLabel.stringValue = NSLocalizedString("Layout Settings", comment: "")
  453. separateButton.title = NSLocalizedString("Retain Flowing Text", comment: "")
  454. singleButton.title = NSLocalizedString("Retain Page Layout", comment: "")
  455. ocrBox.isHidden = false
  456. } else if .Json == convertType {
  457. singleButton.setTitleColor(NSColor.labelColor)
  458. separateButton.setTitleColor(NSColor.labelColor)
  459. separateStypeLabel.textColor = NSColor.labelColor
  460. separateButton.state = NSControl.StateValue.on
  461. singleButton.isEnabled = true
  462. separateButton.isEnabled = true
  463. isAllInOneSheet = false
  464. separateStypeTopOffset.constant = separateStypeBox.frame.size.height + 40 + ocrBox.frame.size.height
  465. ocrTopOffset.constant = separateStypeBox.frame.size.height + 40
  466. extractTableButton.isHidden = true
  467. separateStypeBox.isHidden = false
  468. separateStypeLabel.isHidden = false
  469. separateStypeLabel.stringValue = NSLocalizedString("Json Worksheet Settings", comment: "")
  470. separateButton.title = NSLocalizedString("Extract text content", comment: "")
  471. singleButton.title = NSLocalizedString("Extract tables", comment: "")
  472. ocrBox.isHidden = false
  473. } else if .CSV == convertType {
  474. separateStypeTopOffset.constant = extractTableButton.frame.size.height + 30
  475. extractTableTopOffset.constant = 12
  476. extractTableButton.isHidden = false
  477. separateStypeBox.isHidden = true
  478. separateStypeLabel.isHidden = true
  479. ocrBox.isHidden = true
  480. } else if (.PPT == convertType || .RTF == convertType) {
  481. separateStypeTopOffset.constant = ocrBox.frame.size.height + 10
  482. ocrTopOffset.constant = 10
  483. separateStypeBox.isHidden = true
  484. extractTableButton.isHidden = true
  485. separateStypeLabel.isHidden = true
  486. ocrBox.isHidden = false
  487. } else if (.HTML == convertType ||
  488. .Text == convertType) {
  489. separateStypeTopOffset.constant = ocrBox.frame.size.height + 10
  490. ocrTopOffset.constant = 10
  491. separateStypeBox.isHidden = true
  492. extractTableButton.isHidden = true
  493. separateStypeLabel.isHidden = true
  494. ocrBox.isHidden = false
  495. } else {
  496. separateStypeTopOffset.constant = 10
  497. separateStypeBox.isHidden = true
  498. extractTableButton.isHidden = true
  499. separateStypeLabel.isHidden = true
  500. ocrBox.isHidden = true
  501. }
  502. buttonBottemOffset.constant = offset
  503. }
  504. @IBAction func buttonClicked_extractTable(_ sender: NSButton) {
  505. if extractTableButton.state == .on {
  506. singleButton.setTitleColor(KMAppearance.Layout.b15_1Color())
  507. separateButton.setTitleColor(KMAppearance.Layout.b15_1Color())
  508. separateStypeLabel.textColor = KMAppearance.Layout.b15_1Color()
  509. singleButton.isEnabled = false
  510. separateButton.isEnabled = false
  511. separateButton.state = .off
  512. singleButton.state = .off
  513. } else {
  514. singleButton.setTitleColor(NSColor.labelColor)
  515. separateButton.setTitleColor(NSColor.labelColor)
  516. separateStypeLabel.textColor = NSColor.labelColor
  517. separateButton.state = .on
  518. singleButton.isEnabled = true
  519. separateButton.isEnabled = true
  520. isAllInOneSheet = false
  521. }
  522. }
  523. @IBAction func buttonClicked_Cancel(_ sender: NSButton) {
  524. dismissSheet()
  525. }
  526. func dismissSheet() {
  527. if FileManager.default.fileExists(atPath: kConvertFileSavePath ?? "") {
  528. do {
  529. try FileManager.default.removeItem(atPath: kConvertFileSavePath ?? "")
  530. } catch {
  531. print("Error removing item at path: (error.localizedDescription)")
  532. }
  533. }
  534. self.km_quick_endSheet()
  535. }
  536. func showCriticalAlert(_ alertMsg: String?) {
  537. let alert = NSAlert()
  538. alert.alertStyle = .critical
  539. if let msg = alertMsg {
  540. alert.informativeText = msg
  541. } else {
  542. let st = self.pdfDocument?.documentURL.lastPathComponent.count ?? 0 > 0 ? self.pdfDocument?.documentURL.lastPathComponent : KMLocalizedString("Untitled.pdf", nil)
  543. alert.messageText = String(format: "%@ %@", st!, KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
  544. }
  545. alert.runModal()
  546. }
  547. func produceNewPDF(with pagesArray: [CPDFPage]) {
  548. tempPDFDocument = nil
  549. self.tempPDFDocument = CPDFDocument()
  550. var indexs = IndexSet()
  551. let pageCount = self.pdfDocument?.pageCount ?? 0
  552. for addedPage in pagesArray {
  553. let idx = addedPage.pageIndex()
  554. if idx >= 0 && idx < pageCount {
  555. indexs.insert(IndexSet.Element(idx))
  556. }
  557. }
  558. _ = self.tempPDFDocument?.importPages(indexs, from: self.pdfDocument, at: 0)
  559. self.prePDFView.document = self.tempPDFDocument
  560. self.prePDFView.layoutDocumentView()
  561. self.prePDFView.goToFirstPage(nil)
  562. self.currentPageIndexTextField.stringValue = "1"
  563. self.totalPageCountLabel.stringValue = " / \(self.tempPDFDocument!.pageCount)"
  564. }
  565. @IBAction func buttonClicked_FormatSelect(_ sender: NSButton) {
  566. guard let item = self.formatSelectButton.selectedItem else { return }
  567. self.convertType = KMConvertType(rawValue: item.tag) ?? .Word
  568. updataView()
  569. }
  570. func transform(with string: String) {
  571. if string == KMLocalizedString("Create Sheet for each Table", nil) {
  572. self.excelWorksheetOption = .forEachTable
  573. } else if string == KMLocalizedString("Create Sheet for each Page", nil) {
  574. self.excelWorksheetOption = .forEachPage
  575. } else if string == KMLocalizedString("Create single Sheet for File", nil) {
  576. self.excelWorksheetOption = .forTheDocument
  577. }
  578. }
  579. @IBAction func buttonClicked_excelStyle(_ sender: NSButton) {
  580. if sender.isEqual(self.onlyTextBtn) {
  581. self.tableMenu.isEnabled = false
  582. self.allContentMenu.isEnabled = false
  583. self.excelContentOption = .onlyText
  584. self.excelWorksheetOption = .forEachTable
  585. } else if (sender.isEqual(self.onlyTableBtn)) {
  586. self.tableMenu.isEnabled = true
  587. self.allContentMenu.isEnabled = false
  588. self.excelContentOption = .onlyTable
  589. transform(with: self.tableMenu.selectedItem?.title ?? "")
  590. } else if (sender.isEqual(self.allContentBtn)) {
  591. self.tableMenu.isEnabled = false
  592. self.allContentMenu.isEnabled = true
  593. self.excelContentOption = .allContent
  594. transform(with: self.allContentMenu.selectedItem?.title ?? "")
  595. }
  596. }
  597. @IBAction func buttonClicked_tableMenu(_ sender: NSButton) {
  598. transform(with: self.tableMenu.selectedItem?.title ?? "")
  599. }
  600. @IBAction func buttonClicked_allContentMenu(_ sender: NSButton) {
  601. transform(with: self.allContentMenu.selectedItem?.title ?? "")
  602. }
  603. @IBAction func buttonClicked_DpiSelect(_ sender: NSPopUpButton) {
  604. let index = sender.indexOfSelectedItem
  605. var dpi = 0
  606. if index == 0 {
  607. dpi = 50
  608. } else if index == 1 {
  609. dpi = 72
  610. } else if index == 2 {
  611. dpi = 96
  612. } else if index == 3 {
  613. dpi = 150
  614. } else if index == 4 {
  615. dpi = 300
  616. } else if index == 5 {
  617. dpi = 600
  618. }
  619. self.imageDPINumber = dpi
  620. }
  621. @IBAction func buttonClick_PreviousPage(_ sender: NSButton) {
  622. if self.prePDFView.canGoToPreviousPage() {
  623. self.prePDFView.goToPreviousPage(nil)
  624. let currentIndex = self.prePDFView.document?.index(for: self.prePDFView.currentPage()) ?? 0
  625. self.currentPageIndexTextField.stringValue = "\(currentIndex + 1)"
  626. }
  627. }
  628. @IBAction func buttonClicked_NextPage(_ sender: NSButton) {
  629. if self.prePDFView.canGoToNextPage() {
  630. self.prePDFView.goToNextPage(nil)
  631. let currentIndex = self.prePDFView.document?.index(for: self.prePDFView.currentPage())
  632. self.currentPageIndexTextField.stringValue = "\((currentIndex ?? 0) + 1)"
  633. }
  634. }
  635. @IBAction func buttonClicked_Batch(_ sender: NSButton) {
  636. if KMMemberInfo.shared.isMemberAllFunction == false {
  637. KMMemberInfo.shared.advancedFunctionUsage(type: .batch)
  638. return
  639. }
  640. if self.pdfDocument?.documentURL == nil {
  641. NSSound.beep()
  642. return
  643. }
  644. if self.callBackBlock != nil {
  645. self.callBackBlock!()
  646. self.callBackBlock = nil
  647. }
  648. dismissSheet()
  649. }
  650. @IBAction func buttonClicked_Convert(_ sender: Any) {
  651. if !KMMemberInfo.shared.isPermitConvert {
  652. KMMemberInfo.shared.advancedFunctionUsage (type: .toWord)
  653. } else {
  654. self.window?.makeFirstResponder(self.prePDFView)
  655. if (4 == pageRangeBox.indexOfSelectedItem) {
  656. if self.fileAttri_ == nil {
  657. self.fileAttri_ = KMFileAttribute()
  658. self.fileAttri_?.filePath = self.pdfDocument?.documentURL.path ?? ""
  659. }
  660. self.fileAttri_?.bAllPage = false
  661. self.fileAttri_?.pagesType = .custom
  662. self.fileAttri_?.pagesString = pageRangeBox.stringValue
  663. let pages = self.fileAttri_?.fetchSelectPages() ?? []
  664. if pages.isEmpty {
  665. return
  666. }
  667. }
  668. let openPanel = NSOpenPanel()
  669. openPanel.canChooseFiles = false
  670. openPanel.canChooseDirectories = true
  671. openPanel.canCreateDirectories = true
  672. openPanel.beginSheetModal(for: self.window!) { result in
  673. if result == .OK {
  674. self.savePath = openPanel.url!.path
  675. self.beginConvert()
  676. }
  677. }
  678. }
  679. }
  680. func beginConvert() {
  681. var isLimitConvert = false
  682. #if VERSION_FREE
  683. #if VERSION_DMG
  684. // 桌机版
  685. if KMMemberInfo.shared.isMemberAllFunction {
  686. if KMMemberInfo.shared.isPermitConvert {
  687. isLimitConvert = false
  688. } else {
  689. if (.PNG == self.convertType ||
  690. .JPEG == self.convertType ||
  691. .JPG == self.convertType ||
  692. .HTML == self.convertType ||
  693. .Text == self.convertType ||
  694. .Word == self.convertType) {
  695. isLimitConvert = false
  696. } else {
  697. isLimitConvert = true
  698. }
  699. }
  700. } else {
  701. isLimitConvert = true
  702. }
  703. #else
  704. // 免费版
  705. if KMMemberInfo.shared.isPermitConvert == false {
  706. isLimitConvert = true
  707. }
  708. #endif
  709. #else
  710. // 付费版
  711. if KMMemberInfo.shared.isPermitConvert {
  712. isLimitConvert = false
  713. } else {
  714. if (.PNG == self.convertType ||
  715. .JPEG == self.convertType ||
  716. .JPG == self.convertType ||
  717. .HTML == self.convertType ||
  718. .Text == self.convertType ||
  719. .Word == self.convertType) {
  720. isLimitConvert = false
  721. } else {
  722. isLimitConvert = true
  723. }
  724. }
  725. #endif
  726. self.showWaitting()
  727. let pages = NSMutableArray()
  728. DispatchQueue.global(qos: .background).async { [self] in
  729. let filePath = getConvertFileSavePath()
  730. if (FileManager.default.fileExists(atPath: filePath)) {
  731. try?FileManager.default.removeItem(atPath: filePath)
  732. }
  733. if self.prePDFView.document.write(toFile: filePath) {
  734. for i in 0..<self.prePDFView.document.pageCount {
  735. pages.add(i + 1)
  736. }
  737. }
  738. DispatchQueue.main.async {
  739. if pages.count < 1 {
  740. self.hideWaitting()
  741. let alert = NSAlert()
  742. alert.alertStyle = .critical
  743. 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: ""))"
  744. alert.runModal()
  745. return
  746. }
  747. let pageCount = pages.count
  748. var selectPages = [Int]()
  749. if isLimitConvert {
  750. if pageCount >= 10 {
  751. for i in 0..<10 {
  752. selectPages.append(pages[i] as! Int)
  753. }
  754. } else {
  755. selectPages = pages as! [Int]
  756. }
  757. } else {
  758. selectPages = pages as! [Int]
  759. }
  760. var convert = KMPDFConvert()
  761. if self.convertType == .Json {
  762. convert = KMPDFConvertJson()
  763. convert.isAllInOneSheet = self.isAllInOneSheet
  764. } else if self.convertType == .AdvancedWord || self.convertType == .Word {
  765. convert = KMPDFConvertWord()
  766. let data = convert as? KMPDFConvertWord
  767. data?.layoutOptions = self.isAllInOneSheet ? .retainPageLayout : .retainFlowingText
  768. convert.isAllInOneSheet = self.isAllInOneSheet
  769. } else if self.convertType == .Excel {
  770. convert = KMPDFConvertExcel()
  771. convert.isExtractText = false
  772. convert.isExtractTable = false
  773. convert.isAllInOneSheet = self.excelWorksheetOption == .forTheDocument
  774. if self.onlyTextBtn.state == .on {
  775. convert.isExtractText = true
  776. } else if self.onlyTableBtn.state == .on {
  777. convert.isExtractTable = true
  778. convert.extractTableIndex = self.excelWorksheetOption?.rawValue ?? 0
  779. }
  780. } else if self.convertType == .PPT {
  781. convert = KMPDFConvertPPT()
  782. } else if self.convertType == .Text {
  783. convert = KMPDFConvertText()
  784. } else if self.convertType == .RTF {
  785. convert = KMPDFConvertRTF()
  786. } else if self.convertType == .HTML {
  787. convert = KMPDFConvertHTML()
  788. } else if self.convertType == .CSV {
  789. convert = KMPDFConvertCSV()
  790. convert.isExtractTable = self.extractTableButton.state == .on
  791. } else {
  792. if self.convertType == .JPEG ||
  793. self.convertType == .JPG ||
  794. self.convertType == .PNG ||
  795. self.convertType == .GIF ||
  796. self.convertType == .TIFF ||
  797. self.convertType == .TGA ||
  798. self.convertType == .BMP ||
  799. self.convertType == .JP2 {
  800. convert = KMPDFConvertImage()
  801. let data = convert as? KMPDFConvertImage
  802. if self.convertType == .JPEG {
  803. data?.imageType = .JPEG
  804. } else if self.convertType == .JPG {
  805. data?.imageType = .JPEG
  806. } else if self.convertType == .PNG {
  807. data?.imageType = .PNG
  808. }
  809. // else if self.convertType == .GIF {
  810. // } else if self.convertType == .TIFF {
  811. // } else if self.convertType == .TGA {
  812. // } else if self.convertType == .BMP {
  813. // }
  814. data?.imageDpi = self.imageDPINumber
  815. }
  816. }
  817. convert.isAllowOCR = self.ocrButton.state == .on
  818. convert.ocrLanguage = convert.isAllowOCR ? self.ocrLanguage_ : .english
  819. convert.filePath = filePath
  820. convert.pages = selectPages
  821. // convert.isAllInOneSheet = self.isAllInOneSheet
  822. if self.convertType == .Word {
  823. convert.type = 0
  824. } else {
  825. convert.type = 1
  826. }
  827. // convert.isExtractTable = self.extractTableButton.state == .on
  828. convert.outputFolderPath = self.savePath
  829. convert.outputFileName = (self.pdfDocument?.documentURL.deletingPathExtension().lastPathComponent) ?? NSLocalizedString("Untitled", comment: "")
  830. if self.convertType == .AdvancedWord {
  831. convert.convertType = .word
  832. } else if self.convertType == .JPEG ||
  833. self.convertType == .JPG ||
  834. self.convertType == .PNG ||
  835. self.convertType == .GIF ||
  836. self.convertType == .TIFF ||
  837. self.convertType == .TGA ||
  838. self.convertType == .BMP ||
  839. self.convertType == .JP2 {
  840. convert.convertType = KMPDFConvertType(rawValue: self.convertType.rawValue) ?? .word
  841. convert.options = [KMPDFConvertOptionsKeyImageDPI : self.imageDPINumber,
  842. KMPDFConvertOptionsKeyImageWithAnnotation : true] as [String : Any]
  843. } else {
  844. convert.convertType = KMPDFConvertType(rawValue: self.convertType.rawValue) ?? .word
  845. }
  846. if self.convertType == .Excel {
  847. convert.excelContentOption = self.excelContentOption
  848. convert.excelWorksheetOption = self.excelWorksheetOption
  849. }
  850. convert.password = self.lockPassword
  851. KMPDFConvertManager.defaultManager.convert(convert: convert) { [weak self] (finished, error) in
  852. self?.hideWaitting()
  853. if finished {
  854. self?.dismissSheet()
  855. if FileManager.default.fileExists(atPath: convert.outputFilePath) {
  856. let workspace = NSWorkspace.shared
  857. let url = URL(fileURLWithPath: convert.outputFilePath)
  858. workspace.activateFileViewerSelecting([url])
  859. }
  860. } else if let error = error {
  861. let err: NSError = error as NSError
  862. var erroeString = ""
  863. switch err.code {
  864. case CPDFConverterEncryptError:
  865. erroeString = NSLocalizedString("Password required or incorrect password. Please re-enter your password and try again", comment: "")
  866. break
  867. case CPDFConverterPermissionError:
  868. erroeString = NSLocalizedString("The license doesn't allow the permission", comment: "")
  869. break
  870. case CPDFConverterMallocError:
  871. erroeString = NSLocalizedString("Malloc failure", comment: "")
  872. break
  873. case CPDFConverterUnknownError:
  874. erroeString = NSLocalizedString("Unknown error in processing conversion. Please try again later", comment: "")
  875. break
  876. case CPDFConverterPDFUnknownError:
  877. erroeString = NSLocalizedString("Unknown error in processing PDF. Please try again later", comment: "")
  878. break
  879. case CPDFConverterPDFFileError:
  880. erroeString = NSLocalizedString("File not found or could not be opened. Check if your file exists or choose another file to convert", comment: "")
  881. break
  882. case CPDFConverterPDFFormatError:
  883. erroeString = NSLocalizedString("File not in PDF format or corruptead. Change a PDF file and try again", comment: "")
  884. break
  885. case CPDFConverterPDFSecurityError:
  886. erroeString = NSLocalizedString("Unsupported security scheme", comment: "")
  887. break
  888. case CPDFConverterPDFPageError:
  889. erroeString = NSLocalizedString("Page not found or content error", comment: "")
  890. break
  891. default:
  892. erroeString = NSLocalizedString("Table not found", comment: "")
  893. break
  894. }
  895. let alert = NSAlert()
  896. alert.alertStyle = .critical
  897. alert.messageText = NSLocalizedString("Conversion Failed", comment: "")
  898. alert.informativeText = erroeString
  899. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  900. if #available(OSX 10.14, *) {
  901. alert.beginSheetModal(for: self?.window! ?? NSWindow.currentWindow(), completionHandler: nil)
  902. } else {
  903. alert.runModal()
  904. }
  905. }
  906. }
  907. }
  908. }
  909. }
  910. func getConvertFileSavePath() -> String {
  911. var path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last
  912. path?.append("/")
  913. path?.append(Bundle.main.bundleIdentifier!)
  914. if (FileManager.default.fileExists(atPath: path!) == false) {
  915. try?FileManager.default.createDirectory(atPath: path!, withIntermediateDirectories: false)
  916. }
  917. path?.append("/")
  918. path?.append("convert.pdf")
  919. return path!
  920. }
  921. @IBAction func buttonClicked_MoreConvert(_ sender: NSButton) {
  922. if sender == self.moreButton {
  923. FMTrackEventManager.defaultManager.trackEvent(event: "PUW", withProperties: ["PUW_Btn": "Btn_PUW_Converter_Premium_UnlimitedConvertw"])
  924. } else if sender == self.upgradeButton {
  925. FMTrackEventManager.defaultManager.trackEvent(event: "PUW", withProperties: ["PUW_Btn": "Btn_PUW_Converter_Premium_Upgrade"])
  926. }
  927. #if VERSION_DMG
  928. if KMMemberInfo.shared.isMemberAllFunction && !KMMemberInfo.shared.isPermitConvert {
  929. if KMMemberInfo.shared.userScenarioType == .dmg_type8 ||
  930. KMMemberInfo.shared.userScenarioType == .dmg_type9 {
  931. let alert = NSAlert()
  932. alert.alertStyle = .critical
  933. 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: "")
  934. alert.addButton(withTitle: NSLocalizedString("Got it", comment: ""))
  935. alert.runModal()
  936. } else {
  937. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  938. limitWC.continueBlock = { windowController in
  939. }
  940. limitWC.window?.center()
  941. limitWC.showWindow(nil)
  942. }
  943. } else {
  944. if KMMemberInfo.shared.userScenarioType == .dmg_type16 ||
  945. KMMemberInfo.shared.userScenarioType == .dmg_type17 {
  946. let alert = NSAlert()
  947. alert.alertStyle = .critical
  948. 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: "")
  949. alert.addButton(withTitle: NSLocalizedString("Got it", comment: ""))
  950. alert.runModal()
  951. } else {
  952. if KMMemberInfo.shared.isLogin {
  953. KMProductCompareWC.shared.orientation = false
  954. let winC = KMProductCompareWC.shared
  955. if convertType == .Word || convertType == .AdvancedWord {
  956. winC.kEventName = "Reading_PDFtoWord_BuyNow"
  957. } else if convertType == .Excel {
  958. winC.kEventName = "Reading_PDFtoExcel_BuyNow"
  959. } else if convertType == .PPT {
  960. winC.kEventName = "Reading_PDFtoPPT_BuyNow"
  961. } else if convertType == .RTF {
  962. winC.kEventName = "Reading_PDFtoRTF_BuyNow"
  963. } else if convertType == .CSV {
  964. winC.kEventName = "Reading_PDFtoCSV_BuyNow"
  965. } else if convertType == .Text {
  966. winC.kEventName = "Reading_PDFtoText_BuyNow"
  967. }
  968. if convertType == .JPEG || convertType == .JPG || convertType == .PNG || convertType == .GIF || convertType == .TIFF || convertType == .TGA || convertType == .BMP || convertType == .JP2 {
  969. winC.kEventName = "Reading_PDFtoImage_BuyNow"
  970. }
  971. winC.showWindow(nil)
  972. } else {
  973. KMLoginWindowsController.shared.openWindow() { [weak self] success in
  974. guard let self = self else { return }
  975. if success {
  976. KMProductCompareWC.shared.orientation = false
  977. let winC = KMProductCompareWC.shared
  978. if self.convertType == .Word || self.convertType == .AdvancedWord {
  979. winC.kEventName = "Reading_PDFtoWord_BuyNow"
  980. } else if self.convertType == .Excel {
  981. winC.kEventName = "Reading_PDFtoExcel_BuyNow"
  982. } else if self.convertType == .PPT {
  983. winC.kEventName = "Reading_PDFtoPPT_BuyNow"
  984. } else if self.convertType == .RTF {
  985. winC.kEventName = "Reading_PDFtoRTF_BuyNow"
  986. } else if self.convertType == .CSV {
  987. winC.kEventName = "Reading_PDFtoCSV_BuyNow"
  988. } else if self.convertType == .Text {
  989. winC.kEventName = "Reading_PDFtoText_BuyNow"
  990. }
  991. 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 {
  992. winC.kEventName = "Reading_PDFtoImage_BuyNow"
  993. }
  994. winC.showWindow(nil)
  995. }
  996. }
  997. }
  998. }
  999. }
  1000. #else
  1001. if !KMMemberInfo.shared.isPermitConvert {
  1002. KMMemberInfo.shared.advancedFunctionUsage(type: .toWord)
  1003. }
  1004. #endif
  1005. self.buttonClicked_Cancel(self.cancelButton)
  1006. }
  1007. @IBAction func buttonItemClick_SepSingle(_ sender: Any) {
  1008. if .on == self.singleButton.state {
  1009. self.isAllInOneSheet = true
  1010. } else {
  1011. self.isAllInOneSheet = false
  1012. }
  1013. }
  1014. func controlTextDidEndEditing(_ obj: Notification) {
  1015. let object = obj.object
  1016. if (object as AnyObject).isEqual(self.currentPageIndexTextField) == true {
  1017. let num: Int = Int(self.currentPageIndexTextField.stringValue) ?? 1
  1018. let targetPage = self.prePDFView.document.page(at: UInt(num - 1))
  1019. self.prePDFView.go(to: targetPage)
  1020. } else if (object as AnyObject).isEqual(self.pageRangeBox) == true {
  1021. // guard self.handInputButton.state == .on else { return }
  1022. let idx = self.pageRangeBox.indexOfSelectedItem
  1023. if idx >= 0 && idx <= 3 { // 0 1 2 3
  1024. return
  1025. } else {
  1026. // if self.pageRangeBox.indexOfSelectedItem != 4 { // 页面范围切换中
  1027. }
  1028. if self.fileAttri_ == nil {
  1029. self.fileAttri_ = KMFileAttribute()
  1030. self.fileAttri_?.filePath = self.pdfDocument?.documentURL.path ?? ""
  1031. }
  1032. // fileAttribute.pdfDocument = self.pdfDocument
  1033. self.fileAttri_?.bAllPage = false
  1034. self.fileAttri_?.pagesType = .custom
  1035. self.fileAttri_?.pagesString = self.pageRangeBox.stringValue
  1036. let pages = self.fileAttri_?.fetchSelectPages() ?? []
  1037. if pages.count > 0 {
  1038. let pagesArray = NSMutableArray()
  1039. for pageNum in pages {
  1040. let tpage = self.pdfDocument?.page(at: UInt(pageNum - 1))
  1041. pagesArray.add(tpage as Any)
  1042. }
  1043. self.produceNewPDF(with: pagesArray as! [CPDFPage])
  1044. } else {
  1045. self.showCriticalAlert(nil)
  1046. }
  1047. }
  1048. }
  1049. func controlTextDidChange(_ obj: Notification) {
  1050. if let textField = obj.object as? NSComboBox {
  1051. if textField.isEqual(pageRangeBox) {
  1052. if let stringValue = self.currentPageIndexTextField.formatter?.string(for: NSNumber(value: Float(self.currentPageIndexTextField.stringValue) ?? 0)) {
  1053. if let integerValue = Int(stringValue) {
  1054. var string = stringValue
  1055. if integerValue > self.prePDFView.document.pageCount {
  1056. string = "1-\(self.prePDFView.document.pageCount)"
  1057. } else if integerValue <= 0 {
  1058. string = "1"
  1059. }
  1060. self.currentPageIndexTextField.stringValue = string
  1061. }
  1062. }
  1063. }
  1064. }
  1065. }
  1066. @IBAction func comboxClicked_PageRange(_ sender: NSComboBox) {
  1067. if (0 == pageRangeBox.indexOfSelectedItem ||
  1068. 1 == pageRangeBox.indexOfSelectedItem ||
  1069. 2 == pageRangeBox.indexOfSelectedItem ||
  1070. 3 == pageRangeBox.indexOfSelectedItem) {
  1071. self.window?.makeFirstResponder(self)
  1072. pageRangeBox.isEditable = false
  1073. pageRangeBox.delegate = nil
  1074. } else {
  1075. pageRangeBox.stringValue = pageRangeBox.stringValue
  1076. pageRangeBox.isEditable = true
  1077. pageRangeBox.delegate = self
  1078. self.window?.makeFirstResponder(pageRangeBox)
  1079. }
  1080. self.lockPassword = ""
  1081. let pagesArray = NSMutableArray()
  1082. for i in 0..<(self.pdfDocument?.pageCount ?? 0) {
  1083. let copyPage = self.pdfDocument?.page(at: i)
  1084. pagesArray.add(copyPage!)
  1085. }
  1086. if (2 == pageRangeBox.indexOfSelectedItem) {
  1087. pagesArray.subArray(type: .Odd)
  1088. produceNewPDF(with: pagesArray as! [CPDFPage])
  1089. } else if (3 == pageRangeBox.indexOfSelectedItem) {
  1090. pagesArray.subArray(type: .Even)
  1091. produceNewPDF(with: pagesArray as! [CPDFPage])
  1092. } else if (1 == pageRangeBox.indexOfSelectedItem) {
  1093. pagesArray.removeAllObjects()
  1094. let page = self.prePDFView.currentPage()
  1095. if (page != nil) {
  1096. pagesArray.add(page as Any)
  1097. }
  1098. produceNewPDF(with: pagesArray as! [CPDFPage])
  1099. } else if 0 == pageRangeBox.indexOfSelectedItem {
  1100. produceNewPDF(with: pagesArray as! [CPDFPage])
  1101. }
  1102. }
  1103. @IBAction func comboxClicked_OcrLanguage(_ sender: AnyObject?) {
  1104. switch (ocrLanguageBox.indexOfSelectedItem) {
  1105. case 0: //chinese
  1106. self.ocrLanguage_ = .chinese
  1107. break
  1108. case 1: //chinese zht
  1109. self.ocrLanguage_ = .chineseTraditional
  1110. break
  1111. case 2: //English
  1112. self.ocrLanguage_ = .english
  1113. break
  1114. case 3:
  1115. //Japanese
  1116. self.ocrLanguage_ = .japanese
  1117. break
  1118. case 4:
  1119. //Kroean
  1120. self.ocrLanguage_ = .korean
  1121. break
  1122. case 5:
  1123. //Serbian
  1124. self.ocrLanguage_ = .serbian
  1125. break
  1126. case 6:
  1127. //Occitan
  1128. self.ocrLanguage_ = .occitan
  1129. break
  1130. case 7:
  1131. //Danish
  1132. self.ocrLanguage_ = .danish
  1133. break
  1134. case 8:
  1135. //German
  1136. self.ocrLanguage_ = .german
  1137. break
  1138. case 9:
  1139. //French
  1140. self.ocrLanguage_ = .french
  1141. break
  1142. case 10:
  1143. //Italian
  1144. self.ocrLanguage_ = .italian
  1145. break
  1146. case 11:
  1147. //Spanish
  1148. self.ocrLanguage_ = .spanish
  1149. break
  1150. case 12:
  1151. //Portuguese
  1152. self.ocrLanguage_ = .portuguese
  1153. break
  1154. case 13:
  1155. //Maori
  1156. self.ocrLanguage_ = .maori
  1157. break
  1158. case 14:
  1159. //Malay
  1160. self.ocrLanguage_ = .malay
  1161. break
  1162. case 15:
  1163. //Malay
  1164. self.ocrLanguage_ = .maltese
  1165. break
  1166. case 16:
  1167. //Dutch
  1168. self.ocrLanguage_ = .dutch
  1169. break
  1170. case 17:
  1171. //Norwegian
  1172. self.ocrLanguage_ = .norwegian
  1173. break
  1174. case 18:
  1175. //Polish
  1176. self.ocrLanguage_ = .polish
  1177. break
  1178. case 19:
  1179. //Romanian
  1180. self.ocrLanguage_ = .romanian
  1181. break
  1182. case 20:
  1183. //Slovak
  1184. self.ocrLanguage_ = .slovak
  1185. break
  1186. case 21:
  1187. //Slovenian
  1188. self.ocrLanguage_ = .slovenian
  1189. break
  1190. case 22:
  1191. //Albanian
  1192. self.ocrLanguage_ = .albanian
  1193. break
  1194. case 23:
  1195. //Swedish
  1196. self.ocrLanguage_ = .swedish
  1197. break
  1198. case 24:
  1199. //Swahili
  1200. self.ocrLanguage_ = .swahili
  1201. break
  1202. case 25:
  1203. //Tagalog
  1204. self.ocrLanguage_ = .tagalog
  1205. break
  1206. case 26:
  1207. //Turish
  1208. self.ocrLanguage_ = .turish
  1209. break
  1210. case 27:
  1211. //Uzbek
  1212. self.ocrLanguage_ = .uzbek
  1213. break
  1214. case 28:
  1215. //Vietnamese
  1216. self.ocrLanguage_ = .vietnamese
  1217. break
  1218. case 29:
  1219. //Afrikaans
  1220. self.ocrLanguage_ = .afrikaans
  1221. break
  1222. case 30:
  1223. //Azerbaijani
  1224. self.ocrLanguage_ = .azerbaijani
  1225. break
  1226. case 31:
  1227. //Bosnian
  1228. self.ocrLanguage_ = .bosnian
  1229. break
  1230. case 32:
  1231. //Czech
  1232. self.ocrLanguage_ = .czech
  1233. break
  1234. case 33: //Welsh
  1235. self.ocrLanguage_ = .welsh
  1236. break
  1237. case 34: //Estonian
  1238. self.ocrLanguage_ = .estonian
  1239. break
  1240. case 35: //Irish
  1241. self.ocrLanguage_ = .irish
  1242. break
  1243. case 36: //Croatian
  1244. self.ocrLanguage_ = .croatian
  1245. break
  1246. case 37: //Hungarian
  1247. self.ocrLanguage_ = .hungarian
  1248. break
  1249. case 38: //Indonesian
  1250. self.ocrLanguage_ = .indonesian
  1251. break
  1252. case 39: //Icelandic
  1253. self.ocrLanguage_ = .icelandic
  1254. break
  1255. case 40: //Kurdish
  1256. self.ocrLanguage_ = .kurdish
  1257. break
  1258. case 41: //Lithuanian
  1259. self.ocrLanguage_ = .lithuanian
  1260. break
  1261. case 42: //Latvian
  1262. self.ocrLanguage_ = .latvian
  1263. break
  1264. case 43: //Marathi
  1265. self.ocrLanguage_ = .marathi
  1266. break
  1267. case 44: //Nepali
  1268. self.ocrLanguage_ = .nepali
  1269. break
  1270. case 45: //Latvia
  1271. self.ocrLanguage_ = .latvian
  1272. break
  1273. case 46: //Bihari
  1274. self.ocrLanguage_ = .bihari
  1275. break
  1276. case 47: //Maithili
  1277. self.ocrLanguage_ = .maithili
  1278. break
  1279. case 48: //Angika
  1280. self.ocrLanguage_ = .angika
  1281. break
  1282. case 49: //Bhojpuri
  1283. self.ocrLanguage_ = .bhojpuri
  1284. break
  1285. case 50: //Magahi
  1286. self.ocrLanguage_ = .magahi
  1287. break
  1288. case 51: //Nagpur
  1289. self.ocrLanguage_ = .nagpur
  1290. break
  1291. case 52: //Newari
  1292. self.ocrLanguage_ = .newari
  1293. break
  1294. case 53: //GoanKonkani
  1295. self.ocrLanguage_ = .goanKonkani
  1296. break
  1297. case 54: //SaudiArabia
  1298. self.ocrLanguage_ = .saudiArabia
  1299. break
  1300. default:
  1301. self.ocrLanguage_ = .english
  1302. break
  1303. }
  1304. }
  1305. @IBAction func comboxClicked_SupoortOCR(_ sender: AnyObject?) {
  1306. if(ocrButton.state == .on) {
  1307. ocrLabel.textColor = KMAppearance.titleColor()
  1308. ocrLanguageBox.isEnabled = true
  1309. } else {
  1310. ocrLabel.textColor = KMAppearance.Layout.h2Color()
  1311. ocrLanguageBox.isEnabled = false
  1312. }
  1313. }
  1314. }
  1315. @objc enum KMSubArrayType: Int {
  1316. case Odd = 0
  1317. case Even
  1318. }
  1319. extension NSMutableArray{
  1320. func subArray(type: KMSubArrayType) {
  1321. if self.count == 0 { return }
  1322. if type.rawValue > 1 || type.rawValue < 0 { return }
  1323. let newArray = NSMutableArray()
  1324. for i in 0..<self.count {
  1325. if i % 2 == type.rawValue {
  1326. newArray.add(self[i])
  1327. }
  1328. }
  1329. self.removeAllObjects()
  1330. self.addObjects(from: newArray as! [Any])
  1331. }
  1332. }