KMConvertWindowController.swift 58 KB

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