KMConvertWindowController.swift 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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 IAPProductsManager.default().isAvailableAllFunction() {
  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 IAPProductsManager.default().isAvailableAllFunction() {
  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 !IAPProductsManager.default().isAvailableAllFunction() {
  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 !IAPProductsManager.default().isAvailableAllFunction() {
  644. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  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 IAPProductsManager.default().isAvailableAllFunction() {
  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 IAPProductsManager.default().isAvailableAllFunction() && !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. let winC = KMPurchaseCompareWindowController.sharedInstance()
  940. if convertType == .Word || convertType == .AdvancedWord {
  941. winC?.kEventName = "Reading_PDFtoWord_BuyNow"
  942. } else if convertType == .Excel {
  943. winC?.kEventName = "Reading_PDFtoExcel_BuyNow"
  944. } else if convertType == .PPT {
  945. winC?.kEventName = "Reading_PDFtoPPT_BuyNow"
  946. } else if convertType == .RTF {
  947. winC?.kEventName = "Reading_PDFtoRTF_BuyNow"
  948. } else if convertType == .CSV {
  949. winC?.kEventName = "Reading_PDFtoCSV_BuyNow"
  950. } else if convertType == .Text {
  951. winC?.kEventName = "Reading_PDFtoText_BuyNow"
  952. }
  953. if convertType == .JPEG || convertType == .JPG || convertType == .PNG || convertType == .GIF || convertType == .TIFF || convertType == .TGA || convertType == .BMP || convertType == .JP2 {
  954. winC?.kEventName = "Reading_PDFtoImage_BuyNow"
  955. }
  956. // else if convertType == . {
  957. // winC?.kEventName = "Reading_PDFtoExcel_BuyNow"
  958. // }
  959. winC?.showWindow(nil)
  960. }
  961. #else
  962. if IAPProductsManager.default().isAvailableAllFunction() {
  963. var vc: KMToolCompareWindowController? = nil
  964. if convertType == .Word || convertType == .AdvancedWord {
  965. // vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  966. vc = KMToolCompareWindowController.toolCompare(toolType: .Convert, selectNum: 1)
  967. } else if convertType == .Excel {
  968. // vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 2)
  969. vc = KMToolCompareWindowController.toolCompare(toolType: .Convert, selectNum: 2)
  970. } else if convertType == .PPT {
  971. // vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 3)
  972. vc = KMToolCompareWindowController.toolCompare(toolType: .Convert, selectNum: 3)
  973. } else if convertType.rawValue < 15 && convertType.rawValue > 7 {
  974. // vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 4)
  975. vc = KMToolCompareWindowController.toolCompare(toolType: .Convert, selectNum: 4)
  976. } else {
  977. // vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 0)
  978. vc = KMToolCompareWindowController.toolCompare(toolType: .Convert, selectNum: 0)
  979. }
  980. vc?.showWindow(nil)
  981. } else {
  982. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  983. }
  984. #endif
  985. self.buttonClicked_Cancel(self.cancelButton)
  986. }
  987. @IBAction func buttonItemClick_SepSingle(_ sender: Any) {
  988. if .on == self.singleButton.state {
  989. self.isAllInOneSheet = true
  990. } else {
  991. self.isAllInOneSheet = false
  992. }
  993. }
  994. func controlTextDidEndEditing(_ obj: Notification) {
  995. let object = obj.object
  996. if (object as AnyObject).isEqual(self.currentPageIndexTextField) == true {
  997. let num: Int = Int(self.currentPageIndexTextField.stringValue) ?? 1
  998. let targetPage = self.prePDFView.document.page(at: UInt(num - 1))
  999. self.prePDFView.go(to: targetPage)
  1000. } else if (object as AnyObject).isEqual(self.pageRangeBox) == true {
  1001. // guard self.handInputButton.state == .on else { return }
  1002. let idx = self.pageRangeBox.indexOfSelectedItem
  1003. if idx >= 0 && idx <= 3 { // 0 1 2 3
  1004. return
  1005. } else {
  1006. // if self.pageRangeBox.indexOfSelectedItem != 4 { // 页面范围切换中
  1007. }
  1008. if self.fileAttri_ == nil {
  1009. self.fileAttri_ = KMFileAttribute()
  1010. self.fileAttri_?.filePath = self.pdfDocument?.documentURL.path ?? ""
  1011. }
  1012. // fileAttribute.pdfDocument = self.pdfDocument
  1013. self.fileAttri_?.bAllPage = false
  1014. self.fileAttri_?.pagesType = .custom
  1015. self.fileAttri_?.pagesString = self.pageRangeBox.stringValue
  1016. let pages = self.fileAttri_?.fetchSelectPages() ?? []
  1017. if pages.count > 0 {
  1018. let pagesArray = NSMutableArray()
  1019. for pageNum in pages {
  1020. let tpage = self.pdfDocument?.page(at: UInt(pageNum - 1))
  1021. pagesArray.add(tpage as Any)
  1022. }
  1023. self.produceNewPDF(with: pagesArray as! [CPDFPage])
  1024. } else {
  1025. self.showCriticalAlert(nil)
  1026. }
  1027. }
  1028. }
  1029. func controlTextDidChange(_ obj: Notification) {
  1030. if let textField = obj.object as? NSComboBox {
  1031. if textField.isEqual(pageRangeBox) {
  1032. if let stringValue = self.currentPageIndexTextField.formatter?.string(for: NSNumber(value: Float(self.currentPageIndexTextField.stringValue) ?? 0)) {
  1033. if let integerValue = Int(stringValue) {
  1034. var string = stringValue
  1035. if integerValue > self.prePDFView.document.pageCount {
  1036. string = "1-\(self.prePDFView.document.pageCount)"
  1037. } else if integerValue <= 0 {
  1038. string = "1"
  1039. }
  1040. self.currentPageIndexTextField.stringValue = string
  1041. }
  1042. }
  1043. }
  1044. }
  1045. }
  1046. @IBAction func comboxClicked_PageRange(_ sender: NSComboBox) {
  1047. if (0 == pageRangeBox.indexOfSelectedItem ||
  1048. 1 == pageRangeBox.indexOfSelectedItem ||
  1049. 2 == pageRangeBox.indexOfSelectedItem ||
  1050. 3 == pageRangeBox.indexOfSelectedItem) {
  1051. self.window?.makeFirstResponder(self)
  1052. pageRangeBox.isEditable = false
  1053. pageRangeBox.delegate = nil
  1054. } else {
  1055. pageRangeBox.stringValue = pageRangeBox.stringValue
  1056. pageRangeBox.isEditable = true
  1057. pageRangeBox.delegate = self
  1058. self.window?.makeFirstResponder(pageRangeBox)
  1059. }
  1060. self.lockPassword = ""
  1061. let pagesArray = NSMutableArray()
  1062. for i in 0..<(self.pdfDocument?.pageCount ?? 0) {
  1063. let copyPage = self.pdfDocument?.page(at: i)
  1064. pagesArray.add(copyPage!)
  1065. }
  1066. if (2 == pageRangeBox.indexOfSelectedItem) {
  1067. pagesArray.subArray(type: .Odd)
  1068. produceNewPDF(with: pagesArray as! [CPDFPage])
  1069. } else if (3 == pageRangeBox.indexOfSelectedItem) {
  1070. pagesArray.subArray(type: .Even)
  1071. produceNewPDF(with: pagesArray as! [CPDFPage])
  1072. } else if (1 == pageRangeBox.indexOfSelectedItem) {
  1073. pagesArray.removeAllObjects()
  1074. let page = self.prePDFView.currentPage()
  1075. if (page != nil) {
  1076. pagesArray.add(page as Any)
  1077. }
  1078. produceNewPDF(with: pagesArray as! [CPDFPage])
  1079. }
  1080. }
  1081. @IBAction func comboxClicked_OcrLanguage(_ sender: AnyObject?) {
  1082. switch (ocrLanguageBox.indexOfSelectedItem) {
  1083. case 0: //chinese
  1084. self.ocrLanguage_ = .chinese
  1085. break
  1086. case 1: //chinese zht
  1087. self.ocrLanguage_ = .chineseTraditional
  1088. break
  1089. case 2: //English
  1090. self.ocrLanguage_ = .english
  1091. break
  1092. case 3:
  1093. //Japanese
  1094. self.ocrLanguage_ = .japanese
  1095. break
  1096. case 4:
  1097. //Kroean
  1098. self.ocrLanguage_ = .korean
  1099. break
  1100. case 5:
  1101. //Serbian
  1102. self.ocrLanguage_ = .serbian
  1103. break
  1104. case 6:
  1105. //Occitan
  1106. self.ocrLanguage_ = .occitan
  1107. break
  1108. case 7:
  1109. //Danish
  1110. self.ocrLanguage_ = .danish
  1111. break
  1112. case 8:
  1113. //German
  1114. self.ocrLanguage_ = .german
  1115. break
  1116. case 9:
  1117. //French
  1118. self.ocrLanguage_ = .french
  1119. break
  1120. case 10:
  1121. //Italian
  1122. self.ocrLanguage_ = .italian
  1123. break
  1124. case 11:
  1125. //Spanish
  1126. self.ocrLanguage_ = .spanish
  1127. break
  1128. case 12:
  1129. //Portuguese
  1130. self.ocrLanguage_ = .portuguese
  1131. break
  1132. case 13:
  1133. //Maori
  1134. self.ocrLanguage_ = .maori
  1135. break
  1136. case 14:
  1137. //Malay
  1138. self.ocrLanguage_ = .malay
  1139. break
  1140. case 15:
  1141. //Malay
  1142. self.ocrLanguage_ = .maltese
  1143. break
  1144. case 16:
  1145. //Dutch
  1146. self.ocrLanguage_ = .dutch
  1147. break
  1148. case 17:
  1149. //Norwegian
  1150. self.ocrLanguage_ = .norwegian
  1151. break
  1152. case 18:
  1153. //Polish
  1154. self.ocrLanguage_ = .polish
  1155. break
  1156. case 19:
  1157. //Romanian
  1158. self.ocrLanguage_ = .romanian
  1159. break
  1160. case 20:
  1161. //Slovak
  1162. self.ocrLanguage_ = .slovak
  1163. break
  1164. case 21:
  1165. //Slovenian
  1166. self.ocrLanguage_ = .slovenian
  1167. break
  1168. case 22:
  1169. //Albanian
  1170. self.ocrLanguage_ = .albanian
  1171. break
  1172. case 23:
  1173. //Swedish
  1174. self.ocrLanguage_ = .swedish
  1175. break
  1176. case 24:
  1177. //Swahili
  1178. self.ocrLanguage_ = .swahili
  1179. break
  1180. case 25:
  1181. //Tagalog
  1182. self.ocrLanguage_ = .tagalog
  1183. break
  1184. case 26:
  1185. //Turish
  1186. self.ocrLanguage_ = .turish
  1187. break
  1188. case 27:
  1189. //Uzbek
  1190. self.ocrLanguage_ = .uzbek
  1191. break
  1192. case 28:
  1193. //Vietnamese
  1194. self.ocrLanguage_ = .vietnamese
  1195. break
  1196. case 29:
  1197. //Afrikaans
  1198. self.ocrLanguage_ = .afrikaans
  1199. break
  1200. case 30:
  1201. //Azerbaijani
  1202. self.ocrLanguage_ = .azerbaijani
  1203. break
  1204. case 31:
  1205. //Bosnian
  1206. self.ocrLanguage_ = .bosnian
  1207. break
  1208. case 32:
  1209. //Czech
  1210. self.ocrLanguage_ = .czech
  1211. break
  1212. case 33: //Welsh
  1213. self.ocrLanguage_ = .welsh
  1214. break
  1215. case 34: //Estonian
  1216. self.ocrLanguage_ = .estonian
  1217. break
  1218. case 35: //Irish
  1219. self.ocrLanguage_ = .irish
  1220. break
  1221. case 36: //Croatian
  1222. self.ocrLanguage_ = .croatian
  1223. break
  1224. case 37: //Hungarian
  1225. self.ocrLanguage_ = .hungarian
  1226. break
  1227. case 38: //Indonesian
  1228. self.ocrLanguage_ = .indonesian
  1229. break
  1230. case 39: //Icelandic
  1231. self.ocrLanguage_ = .icelandic
  1232. break
  1233. case 40: //Kurdish
  1234. self.ocrLanguage_ = .kurdish
  1235. break
  1236. case 41: //Lithuanian
  1237. self.ocrLanguage_ = .lithuanian
  1238. break
  1239. case 42: //Latvian
  1240. self.ocrLanguage_ = .latvian
  1241. break
  1242. case 43: //Marathi
  1243. self.ocrLanguage_ = .marathi
  1244. break
  1245. case 44: //Nepali
  1246. self.ocrLanguage_ = .nepali
  1247. break
  1248. case 45: //Latvia
  1249. self.ocrLanguage_ = .latvian
  1250. break
  1251. case 46: //Bihari
  1252. self.ocrLanguage_ = .bihari
  1253. break
  1254. case 47: //Maithili
  1255. self.ocrLanguage_ = .maithili
  1256. break
  1257. case 48: //Angika
  1258. self.ocrLanguage_ = .angika
  1259. break
  1260. case 49: //Bhojpuri
  1261. self.ocrLanguage_ = .bhojpuri
  1262. break
  1263. case 50: //Magahi
  1264. self.ocrLanguage_ = .magahi
  1265. break
  1266. case 51: //Nagpur
  1267. self.ocrLanguage_ = .nagpur
  1268. break
  1269. case 52: //Newari
  1270. self.ocrLanguage_ = .newari
  1271. break
  1272. case 53: //GoanKonkani
  1273. self.ocrLanguage_ = .goanKonkani
  1274. break
  1275. case 54: //SaudiArabia
  1276. self.ocrLanguage_ = .saudiArabia
  1277. break
  1278. default:
  1279. self.ocrLanguage_ = .english
  1280. break
  1281. }
  1282. }
  1283. @IBAction func comboxClicked_SupoortOCR(_ sender: AnyObject?) {
  1284. if(ocrButton.state == .on) {
  1285. ocrLabel.textColor = KMAppearance.titleColor()
  1286. ocrLanguageBox.isEnabled = true
  1287. } else {
  1288. ocrLabel.textColor = KMAppearance.Layout.h2Color()
  1289. ocrLanguageBox.isEnabled = false
  1290. }
  1291. }
  1292. }
  1293. @objc enum KMSubArrayType: Int {
  1294. case Odd = 0
  1295. case Even
  1296. }
  1297. extension NSMutableArray{
  1298. func subArray(type: KMSubArrayType) {
  1299. if self.count == 0 { return }
  1300. if type.rawValue > 1 || type.rawValue < 0 { return }
  1301. let newArray = NSMutableArray()
  1302. for i in 0..<self.count {
  1303. if i % 2 == type.rawValue {
  1304. newArray.add(self[i])
  1305. }
  1306. }
  1307. self.removeAllObjects()
  1308. self.addObjects(from: newArray as! [Any])
  1309. }
  1310. }