KMPDFConvert.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. //
  2. // KMPDFConvert.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/7.
  6. //
  7. import Cocoa
  8. import PDFKit
  9. import ComPDFKit_Conversion
  10. let KMPDFConvertOptionsKeyImageDPI = "KMPDFConvertOptionsKeyImageDPI"
  11. let KMPDFConvertOptionsKeyImageWithAnnotation = "KMPDFConvertOptionsKeyImageWithAnnotation"
  12. enum KMPDFConvertType: Int {
  13. case word = 0
  14. case excel = 1
  15. case ppt = 2
  16. case rtf = 3
  17. case csv = 4
  18. case html = 5
  19. case text = 6
  20. case jpeg = 7
  21. case jpg = 8
  22. case png = 9
  23. case gif = 10
  24. case tiff = 11
  25. case tga = 12
  26. case bmp = 13
  27. case jp2 = 14
  28. static let image: KMPDFConvertType = .jpeg
  29. }
  30. typealias KMPDFConvertCallback = (_ finished: Bool, _ error: Error?) -> ()
  31. typealias KMPDFConvertProgress = (Int) -> ()
  32. class KMPDFConvert: Operation {
  33. var type: Int = 0
  34. var filePath: String = ""
  35. var password: String = ""
  36. var outputFileName: String = ""
  37. var outputFolderPath: String = ""
  38. var pages: [Int]!
  39. var convertType: KMPDFConvertType = .word
  40. var options: [String:Any]!
  41. var outputFilePath: String = ""
  42. var isSuccessful: Bool = false
  43. var isAllInOneSheet: Bool = false
  44. var isExtractTable: Bool = false
  45. var isExtractText: Bool = false
  46. /**
  47. 0 支持一个表格提取到单独的工作表
  48. 1 支持按页面提取表格到单独的工作表
  49. 2 支持将所有表格提取到一个工作表
  50. */
  51. var extractTableIndex: Int = 0
  52. var errorInfo: Error!
  53. // 是否使用OCR
  54. var isAllowOCR = false
  55. // var ocrLanguage: COCRLanguage?
  56. var isContainOCRBgImage = true
  57. var isContainAnnotations = true
  58. var isContainImages = true
  59. fileprivate var pathExtension: String = ""
  60. fileprivate var fpPDFConverter: CPDFConverterFP!
  61. fileprivate var converter: CPDFConverter!
  62. private var isCompletion: Bool = false
  63. var callback: KMPDFConvertCallback!
  64. var progress: KMPDFConvertProgress?
  65. var excelWorksheetOption: CPDFConvertExcelWorksheetOptions?
  66. var excelContentOption: CPDFConvertExcelContentOptions?
  67. public class func pathExtension(_ type: KMPDFConvertType) -> String {
  68. return self.pathExtension(type, nil)
  69. }
  70. public class func pathExtension(_ type: KMPDFConvertType, _ isExtractTable: Bool?) -> String {
  71. if type == .word {
  72. return "docx"
  73. } else if type == .excel {
  74. return "xlsx"
  75. } else if type == .ppt {
  76. return "pptx"
  77. } else if type == .rtf {
  78. return "rtf"
  79. } else if type == .csv {
  80. if isExtractTable != nil && isExtractTable! {
  81. return "zip"
  82. }
  83. return "csv"
  84. } else if type == .html {
  85. return "html"
  86. } else if type == .text {
  87. return "txt"
  88. } else if type == .jpeg {
  89. return "jpeg"
  90. } else if type == .jpg {
  91. return "jpg"
  92. } else if type == .png {
  93. return "png"
  94. } else if type == .gif {
  95. return "gif"
  96. } else if type == .tga {
  97. return "tga"
  98. } else if type == .bmp {
  99. return "bmp"
  100. } else if type == .jp2 {
  101. return "jp2"
  102. } else if type == .tiff {
  103. return "tiff"
  104. }
  105. return ""
  106. }
  107. override func start() {
  108. if isCancelled {
  109. return
  110. }
  111. let pathExtension = KMPDFConvert.pathExtension(self.convertType, self.isExtractTable)
  112. var fileName = outputFileName
  113. var path = outputFolderPath
  114. if convertType == .jpeg || convertType == .jpg || convertType == .png || convertType == .gif || convertType == .tga || convertType == .bmp || convertType == .jp2 || convertType == .tiff {
  115. // if (self.convertType == .jpeg || self.convertType == .png) {
  116. // self.outputFilePath = "\(path)/\(fileName).zip"
  117. // } else {
  118. path.append("/")
  119. path.append(fileName)
  120. let folderPath = getUniqueFilePath(filePath: path)
  121. try?FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false)
  122. outputFilePath = folderPath
  123. // }
  124. } else {
  125. if !pathExtension.isEmpty {
  126. fileName.append(".")
  127. fileName.append(pathExtension)
  128. path.append("/")
  129. path.append(fileName)
  130. let folderPath = getUniqueFilePath(filePath: path)
  131. outputFilePath = folderPath
  132. } else {
  133. outputFolderPath.append("/")
  134. outputFolderPath.append(outputFileName)
  135. outputFilePath = outputFolderPath
  136. }
  137. }
  138. self.pathExtension = pathExtension
  139. self.startConvert()
  140. }
  141. func getUniqueFilePath(filePath: String) -> String {
  142. var i: Int = 0
  143. var isDirectory: ObjCBool = false
  144. var uniqueFilePath = filePath
  145. let fileManager = FileManager.default
  146. fileManager.fileExists(atPath: uniqueFilePath, isDirectory: &isDirectory)
  147. if isDirectory.boolValue {
  148. var path: String = ""
  149. while fileManager.fileExists(atPath: uniqueFilePath) {
  150. i += 1
  151. path = filePath
  152. path.append("(\(i))")
  153. uniqueFilePath = path
  154. }
  155. } else {
  156. let fileURL = URL(fileURLWithPath: filePath)
  157. var path: String = ""
  158. while fileManager.fileExists(atPath: uniqueFilePath) {
  159. i += 1
  160. path = fileURL.deletingPathExtension().path
  161. path.append("(\(i))")
  162. path.append(".")
  163. path.append(fileURL.pathExtension)
  164. uniqueFilePath = path
  165. }
  166. }
  167. return uniqueFilePath
  168. }
  169. func startConvert() {
  170. if pathExtension.isEmpty {
  171. convertSuccessful(isSuccessful: false, errorInfo: nil)
  172. return
  173. }
  174. //
  175. //// if (convertType == .jpeg || convertType == .png) {
  176. //// converter = CPDFConverterImg(url: URL(fileURLWithPath: filePath), password: nil)
  177. //// converter.delegate = self
  178. //// let options = CPDFConvertImgOptions()
  179. //// if (convertType == .jpeg) {
  180. //// options.type = .JPEG
  181. //// } else if (convertType == .png) {
  182. //// options.type = .PNG
  183. //// }
  184. //
  185. //// converter.convert(toFilePath: outputFilePath, pageIndexs: pages, options: options)
  186. //// return
  187. //// }
  188. //
  189. fpPDFConverter = CPDFConverterFP()
  190. fpPDFConverter.setDelegate(self)
  191. var dpi: Int = 0
  192. if self.options != nil {
  193. dpi = self.options[KMPDFConvertOptionsKeyImageDPI] as! Int
  194. }
  195. let options: [String:Any] = [CPDFConvertOptionsKey.imageDPI.rawValue:dpi,CPDFConvertOptionsKey.allInOneSheet.rawValue:isAllInOneSheet]
  196. if self.convertType == .word {
  197. self.converter = CPDFConverterWord.init(url: URL(fileURLWithPath: filePath), password: self.password)
  198. self.converter.delegate = self
  199. let options = CPDFConvertWordOptions()
  200. options.layoutOptions = self.isAllInOneSheet ? .retainPageLayout : .retainFlowingText
  201. options.isContainAnnotations = true
  202. // options.isAllowOCR = self.isAllowOCR
  203. // if (self.isAllowOCR) {
  204. // options.isContainOCRBgImage = self.isContainOCRBgImage
  205. // if let language = self.ocrLanguage {
  206. // options.language = language
  207. // } else {
  208. // options.language = .english
  209. // }
  210. // } else {
  211. options.isContainImages = true
  212. // options.isContainOCRBgImage = false
  213. // }
  214. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  215. return
  216. }
  217. if self.convertType == .excel {
  218. self.converter = CPDFConverterExcel.init(url: URL(fileURLWithPath: filePath), password: self.password)
  219. self.converter.delegate = self
  220. let options = CPDFConvertExcelOptions()
  221. options.isContainAnnotations = true
  222. // options.isAllowOCR = self.isAllowOCR
  223. // if (self.isAllowOCR) {
  224. // options.isContainOCRBgImage = self.isContainOCRBgImage
  225. // if let language = self.ocrLanguage {
  226. // options.language = language
  227. // } else {
  228. // options.language = .english
  229. // }
  230. // } else {
  231. options.isContainImages = true
  232. // options.isContainOCRBgImage = false
  233. // }
  234. options.contentOptions = self.excelContentOption ?? .allContent
  235. options.worksheetOptions = self.excelWorksheetOption ?? .forEachPage
  236. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  237. return
  238. }
  239. if self.convertType == .ppt {
  240. self.converter = CPDFConverterPPT.init(url: URL(fileURLWithPath: filePath), password: self.password)
  241. self.converter.delegate = self
  242. let options = CPDFConvertPPTOptions()
  243. options.isContainAnnotations = true
  244. // options.isAllowOCR = self.isAllowOCR
  245. options.isContainImages = true
  246. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  247. return
  248. }
  249. if self.convertType == .csv && isExtractTable{
  250. self.converter = CPDFConverterCsv.init(url: URL(fileURLWithPath: filePath), password: self.password)
  251. self.converter.delegate = self
  252. let options = CPDFConvertCsvOptions()
  253. // options.isAILayoutAnalysis = isExtractTable
  254. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  255. return
  256. }
  257. if self.convertType == .rtf{
  258. self.converter = CPDFConverterRtf(url: URL(fileURLWithPath: self.filePath), password: self.password)
  259. self.converter.delegate = self
  260. let options = CPDFConvertRtfOptions()
  261. options.isContainAnnotations = true
  262. // options.isAllowOCR = true
  263. // if (self.isAllowOCR) {
  264. // options.isContainOCRBgImage = self.isContainOCRBgImage
  265. // if let language = self.ocrLanguage {
  266. // options.language = language
  267. // } else {
  268. // options.language = .english
  269. // }
  270. // } else {
  271. options.isContainImages = true
  272. // options.isContainOCRBgImage = false
  273. // }
  274. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  275. return
  276. }
  277. if self.convertType == .html{
  278. self.converter = CPDFConverterHtml.init(url: URL(fileURLWithPath: filePath), password: self.password)
  279. self.converter.delegate = self
  280. let options = CPDFConvertHtmlOptions()
  281. options.isContainAnnotations = true
  282. // options.isAllowOCR = true
  283. // if (self.isAllowOCR) {
  284. // options.isContainOCRBgImage = self.isContainOCRBgImage
  285. // if let language = self.ocrLanguage {
  286. // options.language = language
  287. // } else {
  288. // options.language = .english
  289. // }
  290. // } else {
  291. options.isContainImages = true
  292. // options.isContainOCRBgImage = false
  293. // }
  294. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  295. return
  296. }
  297. if self.convertType == .text{
  298. self.converter = CPDFConverterTxt.init(url: URL(fileURLWithPath: filePath), password: self.password)
  299. self.converter.delegate = self
  300. let options = CPDFConvertTxtOptions()
  301. // options.isAllowOCR = self.isAllowOCR
  302. // if (self.isAllowOCR) {
  303. // if let language = self.ocrLanguage {
  304. // options.language = language
  305. // } else {
  306. // options.language = .english
  307. // }
  308. // }
  309. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  310. return
  311. }
  312. fpPDFConverter.convertPDF(atPath: filePath, pdfPassword: self.password, pdfPageIndexs: pages, destDocType: pathExtension, destDocPath: outputFilePath, moreOptions: options)
  313. }
  314. func convertSuccessful(isSuccessful: Bool, errorInfo: Error!) {
  315. self.isSuccessful = isSuccessful
  316. self.errorInfo = errorInfo
  317. if self.converter != nil && self.converter.delegate != nil{
  318. self.converter.delegate = nil
  319. }
  320. DispatchQueue.main.async { [self] in
  321. guard let callbackBlock = callback else {
  322. return
  323. }
  324. callbackBlock(isSuccessful, errorInfo)
  325. }
  326. willChangeValue(forKey: "isFinished")
  327. isCompletion = true
  328. didChangeValue(forKey: "isFinished")
  329. }
  330. override var isFinished: Bool {
  331. return self.isCompletion
  332. }
  333. }
  334. extension KMPDFConvert: CPDFConverterDelegate {
  335. func converter(_ converter: CPDFConverter!, didStartConvert error: Error!) {
  336. }
  337. func converter(_ converter: CPDFConverter!, didEndConvert error: Error!) {
  338. if (error != nil) {
  339. convertSuccessful(isSuccessful: false, errorInfo: error)
  340. } else {
  341. convertSuccessful(isSuccessful: true, errorInfo: error)
  342. }
  343. }
  344. func converter(_ converter: CPDFConverter!, pageIndex index: UInt, pageCount count: UInt) {
  345. guard let callback = progress else {
  346. return
  347. }
  348. callback(Int(index))
  349. }
  350. }
  351. extension KMPDFConvert: CPDFConverterFPDelegate {
  352. func fppdfConverter(_ converter: Any!, didEndConversion error: Error!) {
  353. if (error != nil) {
  354. convertSuccessful(isSuccessful: false, errorInfo: error)
  355. } else {
  356. convertSuccessful(isSuccessful: true, errorInfo: error)
  357. }
  358. }
  359. func fppdfConverter(_ converter: Any!, convertPDFPageIndex pdfPageIndexA: UInt, writeWordPageIndex wordPageIndexA: UInt, finshedWordPageCount wordPageCountA: UInt) {
  360. guard let callback = progress else {
  361. return
  362. }
  363. callback(Int(wordPageIndexA))
  364. }
  365. }
  366. // MARK: - PDF 转 Word
  367. class KMPDFConvertWord: KMPDFConvert {
  368. // 框排 | 流排 [默认流排]
  369. var layoutOptions: CPDFConvertLayoutOptions = .retainFlowingText
  370. override init() {
  371. super.init()
  372. self.convertType = .word
  373. }
  374. override func startConvert() {
  375. if self.pathExtension.isEmpty {
  376. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  377. return
  378. }
  379. self.converter = CPDFConverterWord(url: URL(fileURLWithPath: self.filePath), password: self.password)
  380. self.converter.delegate = self
  381. let options = CPDFConvertWordOptions()
  382. options.layoutOptions = self.layoutOptions
  383. options.isContainAnnotations = self.isContainAnnotations
  384. // options.isAllowOCR = self.isAllowOCR
  385. // if (self.isAllowOCR) {
  386. // options.isContainOCRBgImage = self.isContainOCRBgImage
  387. // if let language = self.ocrLanguage {
  388. // options.language = language
  389. // } else {
  390. // options.language = .english
  391. // }
  392. // } else {
  393. options.isContainImages = true
  394. // options.isContainOCRBgImage = false
  395. // }
  396. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  397. }
  398. }
  399. // MARK: - PDF 转 Image
  400. class KMPDFConvertImage: KMPDFConvert {
  401. var imageType: CPDFConvertImgType = .JPEG
  402. var imageDpi: Int = 150
  403. override func startConvert() {
  404. if self.pathExtension.isEmpty {
  405. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  406. return
  407. }
  408. if (self.convertType == .jpeg || self.convertType == .png) {
  409. self.converter = CPDFConverterImg(url: URL(fileURLWithPath: self.filePath), password: self.password)
  410. self.converter.delegate = self
  411. let options = CPDFConvertImgOptions()
  412. options.type = self.imageType
  413. options.imageDpi = Int32(self.imageDpi)
  414. options.isContainAnnotations = true
  415. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  416. return
  417. }
  418. self.fpPDFConverter = CPDFConverterFP()
  419. self.fpPDFConverter.setDelegate(self)
  420. let options: [String : Any] = [CPDFConvertOptionsKey.imageDPI.rawValue : self.imageDpi]
  421. self.fpPDFConverter.convertPDF(atPath: self.filePath, pdfPassword: self.password, pdfPageIndexs: self.pages, destDocType: self.pathExtension, destDocPath: self.outputFilePath, moreOptions: options)
  422. }
  423. }
  424. // MARK: - PDF 转 PPT
  425. class KMPDFConvertPPT: KMPDFConvert {
  426. override init() {
  427. super.init()
  428. self.convertType = .ppt
  429. }
  430. override func startConvert() {
  431. if self.pathExtension.isEmpty {
  432. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  433. return
  434. }
  435. let options = CPDFConvertPPTOptions()
  436. options.isContainAnnotations = self.isContainAnnotations
  437. // options.isAllowOCR = self.isAllowOCR
  438. // if (self.isAllowOCR) {
  439. // options.isContainOCRBgImage = self.isContainOCRBgImage
  440. // if let language = self.ocrLanguage {
  441. // options.language = language
  442. // } else {
  443. // options.language = .english
  444. // }
  445. // } else {
  446. options.isContainImages = true
  447. // options.isContainOCRBgImage = false
  448. // }
  449. self.converter = CPDFConverterPPT(url: URL(fileURLWithPath: self.filePath), password: self.password)
  450. self.converter.delegate = self
  451. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  452. }
  453. }
  454. // MARK: - PDF 转 RTF
  455. class KMPDFConvertRTF: KMPDFConvert {
  456. override init() {
  457. super.init()
  458. self.convertType = .rtf
  459. }
  460. override func startConvert() {
  461. if self.pathExtension.isEmpty {
  462. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  463. return
  464. }
  465. let options = CPDFConvertRtfOptions()
  466. options.isContainAnnotations = self.isContainAnnotations
  467. // options.isAllowOCR = self.isAllowOCR
  468. // if (self.isAllowOCR) {
  469. // options.isContainOCRBgImage = self.isContainOCRBgImage
  470. // if let language = self.ocrLanguage {
  471. // options.language = language
  472. // } else {
  473. // options.language = .english
  474. // }
  475. // } else {
  476. options.isContainImages = true
  477. // options.isContainOCRBgImage = false
  478. // }
  479. self.converter = CPDFConverterRtf(url: URL(fileURLWithPath: self.filePath), password: self.password)
  480. self.converter.delegate = self
  481. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  482. }
  483. }
  484. // MARK: - PDF 转 HTML
  485. class KMPDFConvertHTML: KMPDFConvert {
  486. override init() {
  487. super.init()
  488. self.convertType = .html
  489. }
  490. override func startConvert() {
  491. if self.pathExtension.isEmpty {
  492. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  493. return
  494. }
  495. let options = CPDFConvertHtmlOptions()
  496. options.isContainAnnotations = self.isContainAnnotations
  497. // options.isAllowOCR = self.isAllowOCR
  498. // if (self.isAllowOCR) {
  499. // options.isContainOCRBgImage = self.isContainOCRBgImage
  500. // if let language = self.ocrLanguage {
  501. // options.language = language
  502. // } else {
  503. // options.language = .english
  504. // }
  505. // } else {
  506. options.isContainImages = self.isContainImages
  507. // options.isContainOCRBgImage = false
  508. // }
  509. self.converter = CPDFConverterHtml(url: URL(fileURLWithPath: self.filePath), password: self.password)
  510. self.converter.delegate = self
  511. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  512. }
  513. }
  514. // MARK: - PDF 转 Text
  515. class KMPDFConvertText: KMPDFConvert {
  516. override init() {
  517. super.init()
  518. self.convertType = .text
  519. }
  520. override func startConvert() {
  521. if self.pathExtension.isEmpty {
  522. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  523. return
  524. }
  525. let options = CPDFConvertTxtOptions()
  526. // options.isAllowOCR = self.isAllowOCR
  527. // if (self.isAllowOCR) {
  528. // if let language = self.ocrLanguage {
  529. // options.language = language
  530. // } else {
  531. // options.language = .english
  532. // }
  533. // }
  534. self.converter = CPDFConverterTxt(url: URL(fileURLWithPath: self.filePath), password: self.password)
  535. self.converter.delegate = self
  536. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  537. }
  538. }
  539. // MARK: - PDF 转 CSV
  540. class KMPDFConvertCSV: KMPDFConvert {
  541. override init() {
  542. super.init()
  543. self.convertType = .csv
  544. }
  545. override func startConvert() {
  546. if self.pathExtension.isEmpty {
  547. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  548. return
  549. }
  550. if (self.convertType == .csv && self.isExtractTable) {
  551. self.converter = CPDFConverterCsv(url: URL(fileURLWithPath: self.filePath), password: self.password)
  552. self.converter.delegate = self
  553. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: nil)
  554. return
  555. }
  556. self.fpPDFConverter = CPDFConverterFP()
  557. self.fpPDFConverter.setDelegate(self)
  558. let options: [String : Any] = [CPDFConvertOptionsKey.allInOneSheet.rawValue : self.isAllInOneSheet]
  559. self.fpPDFConverter.convertPDF(atPath: self.filePath, pdfPassword: self.password, pdfPageIndexs: self.pages, destDocType: self.pathExtension, destDocPath: self.outputFilePath, moreOptions: options)
  560. }
  561. }
  562. // MARK: - PDF 转 Excel
  563. class KMPDFConvertExcel: KMPDFConvert {
  564. override init() {
  565. super.init()
  566. self.convertType = .excel
  567. }
  568. override func startConvert() {
  569. if (self.pathExtension.isEmpty) {
  570. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  571. return
  572. }
  573. self.converter = CPDFConverterExcel(url: URL(fileURLWithPath: self.filePath), password: self.password)
  574. self.converter.delegate = self
  575. let options = CPDFConvertExcelOptions()
  576. options.isContainAnnotations = self.isContainAnnotations
  577. // options.isAllowOCR = self.isAllowOCR
  578. // if (self.isAllowOCR) {
  579. //// options.isContainOCRBgImage = self.isContainOCRBgImage
  580. // if let language = self.ocrLanguage {
  581. // options.language = language
  582. // } else {
  583. // options.language = .english
  584. // }
  585. // } else {
  586. options.isContainImages = true
  587. // options.isContainOCRBgImage = false
  588. // }
  589. if (self.isExtractText) {
  590. options.contentOptions = .onlyText
  591. } else if (self.isExtractTable) {
  592. options.contentOptions = .onlyTable
  593. if (self.extractTableIndex == 0) {
  594. options.worksheetOptions = .forEachTable
  595. } else if (self.extractTableIndex == 1) {
  596. options.worksheetOptions = .forEachPage
  597. } else if (self.extractTableIndex == 2) {
  598. options.worksheetOptions = .forTheDocument
  599. }
  600. } else {
  601. options.contentOptions = .allContent
  602. if (self.isAllInOneSheet) {
  603. options.worksheetOptions = .forTheDocument
  604. } else {
  605. options.worksheetOptions = .forEachPage
  606. }
  607. }
  608. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  609. }
  610. }