KMPDFConvert.swift 26 KB

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