KMConvertWindowController.swift 57 KB

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