KMPDFConvert.swift 26 KB

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