KMPDFConvert.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 = path
  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 = path
  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. DispatchQueue.main.async { [self] in
  318. guard let callbackBlock = callback else {
  319. return
  320. }
  321. callbackBlock(isSuccessful, errorInfo)
  322. }
  323. willChangeValue(forKey: "isFinished")
  324. isCompletion = true
  325. didChangeValue(forKey: "isFinished")
  326. }
  327. override var isFinished: Bool {
  328. return self.isCompletion
  329. }
  330. }
  331. extension KMPDFConvert: CPDFConverterDelegate {
  332. func converter(_ converter: CPDFConverter!, didStartConvert error: Error!) {
  333. }
  334. func converter(_ converter: CPDFConverter!, didEndConvert error: Error!) {
  335. if (error != nil) {
  336. convertSuccessful(isSuccessful: false, errorInfo: error)
  337. } else {
  338. convertSuccessful(isSuccessful: true, errorInfo: error)
  339. }
  340. }
  341. func converter(_ converter: CPDFConverter!, pageIndex index: UInt, pageCount count: UInt) {
  342. guard let callback = progress else {
  343. return
  344. }
  345. callback(Int(index))
  346. }
  347. }
  348. extension KMPDFConvert: CPDFConverterFPDelegate {
  349. func fppdfConverter(_ converter: Any!, didEndConversion error: Error!) {
  350. if (error != nil) {
  351. convertSuccessful(isSuccessful: false, errorInfo: error)
  352. } else {
  353. convertSuccessful(isSuccessful: true, errorInfo: error)
  354. }
  355. }
  356. func fppdfConverter(_ converter: Any!, convertPDFPageIndex pdfPageIndexA: UInt, writeWordPageIndex wordPageIndexA: UInt, finshedWordPageCount wordPageCountA: UInt) {
  357. guard let callback = progress else {
  358. return
  359. }
  360. callback(Int(wordPageIndexA))
  361. }
  362. }
  363. // MARK: - PDF 转 Word
  364. class KMPDFConvertWord: KMPDFConvert {
  365. // 框排 | 流排 [默认流排]
  366. var layoutOptions: CPDFConvertLayoutOptions = .retainFlowingText
  367. override init() {
  368. super.init()
  369. self.convertType = .word
  370. }
  371. override func startConvert() {
  372. if self.pathExtension.isEmpty {
  373. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  374. return
  375. }
  376. self.converter = CPDFConverterWord(url: URL(fileURLWithPath: self.filePath), password: self.password)
  377. self.converter.delegate = self
  378. let options = CPDFConvertWordOptions()
  379. options.layoutOptions = self.layoutOptions
  380. options.isContainAnnotations = self.isContainAnnotations
  381. options.isAllowOCR = self.isAllowOCR
  382. if (self.isAllowOCR) {
  383. options.isContainOCRBgImage = self.isContainOCRBgImage
  384. if let language = self.ocrLanguage {
  385. options.language = language
  386. } else {
  387. options.language = .english
  388. }
  389. } else {
  390. options.isContainImages = true
  391. options.isContainOCRBgImage = false
  392. }
  393. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  394. }
  395. }
  396. // MARK: - PDF 转 Image
  397. class KMPDFConvertImage: KMPDFConvert {
  398. var imageType: CPDFConvertImgType = .JPEG
  399. var imageDpi: Int = 150
  400. override func startConvert() {
  401. if self.pathExtension.isEmpty {
  402. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  403. return
  404. }
  405. if (self.convertType == .jpeg || self.convertType == .png) {
  406. self.converter = CPDFConverterImg(url: URL(fileURLWithPath: self.filePath), password: self.password)
  407. self.converter.delegate = self
  408. let options = CPDFConvertImgOptions()
  409. options.type = self.imageType
  410. options.imageDpi = Int32(self.imageDpi)
  411. options.isContainAnnotations = true
  412. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  413. return
  414. }
  415. self.fpPDFConverter = CPDFConverterFP()
  416. self.fpPDFConverter.setDelegate(self)
  417. let options: [String : Any] = [CPDFConvertOptionsKey.imageDPI.rawValue : self.imageDpi]
  418. self.fpPDFConverter.convertPDF(atPath: self.filePath, pdfPassword: self.password, pdfPageIndexs: self.pages, destDocType: self.pathExtension, destDocPath: self.outputFilePath, moreOptions: options)
  419. }
  420. }
  421. // MARK: - PDF 转 PPT
  422. class KMPDFConvertPPT: KMPDFConvert {
  423. override init() {
  424. super.init()
  425. self.convertType = .ppt
  426. }
  427. override func startConvert() {
  428. if self.pathExtension.isEmpty {
  429. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  430. return
  431. }
  432. let options = CPDFConvertPPTOptions()
  433. options.isContainAnnotations = self.isContainAnnotations
  434. options.isAllowOCR = self.isAllowOCR
  435. if (self.isAllowOCR) {
  436. options.isContainOCRBgImage = self.isContainOCRBgImage
  437. if let language = self.ocrLanguage {
  438. options.language = language
  439. } else {
  440. options.language = .english
  441. }
  442. } else {
  443. options.isContainImages = true
  444. options.isContainOCRBgImage = false
  445. }
  446. self.converter = CPDFConverterPPT(url: URL(fileURLWithPath: self.filePath), password: self.password)
  447. self.converter.delegate = self
  448. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  449. }
  450. }
  451. // MARK: - PDF 转 RTF
  452. class KMPDFConvertRTF: KMPDFConvert {
  453. override init() {
  454. super.init()
  455. self.convertType = .rtf
  456. }
  457. override func startConvert() {
  458. if self.pathExtension.isEmpty {
  459. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  460. return
  461. }
  462. let options = CPDFConvertRtfOptions()
  463. options.isContainAnnotations = self.isContainAnnotations
  464. options.isAllowOCR = self.isAllowOCR
  465. if (self.isAllowOCR) {
  466. options.isContainOCRBgImage = self.isContainOCRBgImage
  467. if let language = self.ocrLanguage {
  468. options.language = language
  469. } else {
  470. options.language = .english
  471. }
  472. } else {
  473. options.isContainImages = true
  474. options.isContainOCRBgImage = false
  475. }
  476. self.converter = CPDFConverterRtf(url: URL(fileURLWithPath: self.filePath), password: self.password)
  477. self.converter.delegate = self
  478. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  479. }
  480. }
  481. // MARK: - PDF 转 HTML
  482. class KMPDFConvertHTML: KMPDFConvert {
  483. override init() {
  484. super.init()
  485. self.convertType = .html
  486. }
  487. override func startConvert() {
  488. if self.pathExtension.isEmpty {
  489. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  490. return
  491. }
  492. let options = CPDFConvertHtmlOptions()
  493. options.isContainAnnotations = self.isContainAnnotations
  494. options.isAllowOCR = self.isAllowOCR
  495. if (self.isAllowOCR) {
  496. options.isContainOCRBgImage = self.isContainOCRBgImage
  497. if let language = self.ocrLanguage {
  498. options.language = language
  499. } else {
  500. options.language = .english
  501. }
  502. } else {
  503. options.isContainImages = self.isContainImages
  504. options.isContainOCRBgImage = false
  505. }
  506. self.converter = CPDFConverterHtml(url: URL(fileURLWithPath: self.filePath), password: self.password)
  507. self.converter.delegate = self
  508. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  509. }
  510. }
  511. // MARK: - PDF 转 Text
  512. class KMPDFConvertText: KMPDFConvert {
  513. override init() {
  514. super.init()
  515. self.convertType = .text
  516. }
  517. override func startConvert() {
  518. if self.pathExtension.isEmpty {
  519. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  520. return
  521. }
  522. let options = CPDFConvertTxtOptions()
  523. options.isAllowOCR = self.isAllowOCR
  524. if (self.isAllowOCR) {
  525. if let language = self.ocrLanguage {
  526. options.language = language
  527. } else {
  528. options.language = .english
  529. }
  530. }
  531. self.converter = CPDFConverterTxt(url: URL(fileURLWithPath: self.filePath), password: self.password)
  532. self.converter.delegate = self
  533. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  534. }
  535. }
  536. // MARK: - PDF 转 CSV
  537. class KMPDFConvertCSV: KMPDFConvert {
  538. override init() {
  539. super.init()
  540. self.convertType = .csv
  541. }
  542. override func startConvert() {
  543. if self.pathExtension.isEmpty {
  544. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  545. return
  546. }
  547. if (self.convertType == .csv && self.isExtractTable) {
  548. self.converter = CPDFConverterCsv(url: URL(fileURLWithPath: self.filePath), password: self.password)
  549. self.converter.delegate = self
  550. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: nil)
  551. return
  552. }
  553. self.fpPDFConverter = CPDFConverterFP()
  554. self.fpPDFConverter.setDelegate(self)
  555. let options: [String : Any] = [CPDFConvertOptionsKey.allInOneSheet.rawValue : self.isAllInOneSheet]
  556. self.fpPDFConverter.convertPDF(atPath: self.filePath, pdfPassword: self.password, pdfPageIndexs: self.pages, destDocType: self.pathExtension, destDocPath: self.outputFilePath, moreOptions: options)
  557. }
  558. }
  559. // MARK: - PDF 转 Excel
  560. class KMPDFConvertExcel: KMPDFConvert {
  561. override init() {
  562. super.init()
  563. self.convertType = .excel
  564. }
  565. override func startConvert() {
  566. if (self.pathExtension.isEmpty) {
  567. self.convertSuccessful(isSuccessful: false, errorInfo: nil)
  568. return
  569. }
  570. self.converter = CPDFConverterExcel(url: URL(fileURLWithPath: self.filePath), password: self.password)
  571. self.converter.delegate = self
  572. let options = CPDFConvertExcelOptions()
  573. options.isContainAnnotations = self.isContainAnnotations
  574. options.isAllowOCR = self.isAllowOCR
  575. if (self.isAllowOCR) {
  576. // options.isContainOCRBgImage = self.isContainOCRBgImage
  577. if let language = self.ocrLanguage {
  578. options.language = language
  579. } else {
  580. options.language = .english
  581. }
  582. } else {
  583. options.isContainImages = true
  584. // options.isContainOCRBgImage = false
  585. }
  586. if (self.isExtractText) {
  587. options.contentOptions = .onlyText
  588. } else if (self.isExtractTable) {
  589. options.contentOptions = .onlyTable
  590. if (self.extractTableIndex == 0) {
  591. options.worksheetOptions = .forEachTable
  592. } else if (self.extractTableIndex == 1) {
  593. options.worksheetOptions = .forEachPage
  594. } else if (self.extractTableIndex == 2) {
  595. options.worksheetOptions = .forTheDocument
  596. }
  597. } else {
  598. options.contentOptions = .allContent
  599. if (self.isAllInOneSheet) {
  600. options.worksheetOptions = .forTheDocument
  601. } else {
  602. options.worksheetOptions = .forEachPage
  603. }
  604. }
  605. self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
  606. }
  607. }