KMPDFConvert.swift 27 KB

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