KMConvertWindowController.swift 57 KB

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